Windows批处理脚本从FTP下载无最新的文件并把它们喂到一个EXE文件 [英] Windows Batch script to download N latest files from FTP and feed them to an EXE file
问题描述
我比较熟悉Bash和Bash中我在做什么在这里会很容易。
I am more familiar with Bash and in Bash what I am doing here would be easy.
我想创建一个Windows批处理脚本从FTP服务器上下载文件名N个最新的文件,然后输送到一个EXE作为命令行参数。
I would like to create a Windows batch script to download the N latest files from an FTP server and then feed the filenames to an EXE as command line parameters.
在code会做以下几点:
The code would be doing the following:
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命令。
I think that to run a process asynchronously, I can use the START command.
这怎么使用批处理脚本或命令行来实现?
How can this be achieved using a Batch script or the command line?
推荐答案
您可以使用以下使用PowerShell脚本的WinSCP .NET程序集:
You can use following PowerShell script using WinSCP .NET assembly:
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 {0}..." -f $latestFile)
$session.GetFiles($session.EscapeFileMask($remotePath + $latestFile.Name), $localPath).Check()
}
# Disconnect, clean up
$session.Dispose()
这是基于一个正式的例子下载最新的文件。
This is based on an official example Downloading the most recent file.
的(我的WinSCP的作者)的
这篇关于Windows批处理脚本从FTP下载无最新的文件并把它们喂到一个EXE文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!