我对Bash更熟悉,在Bash中,我在这里做的很容易。
我想创建一个Windows批处理脚本,从FTP服务器下载N个最新文件,然后将文件名作为命令行参数提供给EXE。
代码将执行以下操作:
open ftp test.com
username
password
"get/download N latest files from the ftp folder"
"list N latest files just downloaded"
"for each filename execute 'mytest.exe filename' as a background job (in Bash: &)"
我认为要异步运行一个进程,我可以使用START命令。
如何使用Batch脚本或命令行实现这一目标?
答案 0 :(得分:1)
您可以使用WinSCP .NET assembly使用以下PowerShell脚本:
param (
$sessionUrl = "ftp://user:mypassword@example.com/",
$localPath = "c:\\downloaded\\",
$remotePath = "/home/user/",
$count = 10
)
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl);
$session = New-Object WinSCP.Session
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latestFiles =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First $count
# Download the latest files
foreach ($latestFile in $latestFiles)
{
Write-Host "Downloading $latestFile..."
$sourcePath = [WinSCP.RemotePath]::EscapeFileMask($remotePath + $latestFile.Name)
$session.GetFiles($sourcePath, $localPath).Check()
}
# Disconnect, clean up
$session.Dispose()
这是基于官方示例Downloading the most recent file。
(我是WinSCP的作者)
答案 1 :(得分:0)
您可以从批处理文件中创建FTP脚本开始:
this.defaultShowErrors()
到目前为止,您有一个带文件列表的ftp.log。 现在,您可以使用批处理文件完成剩下的工作,但如果您使用真正的编程语言,则会更容易。
返回批处理文件...