批处理文件将文件从一个FTP文件夹移动到同一FTP中的另一个文件夹?

问题描述 投票:1回答:2

大家早上好,

我有一个任务,我需要自动化一个过程,我们从FTP站点下载文件,文件在SQL中处理,然后FTP站点上的文件被移动到同一FTP站点上的另一个文件夹。

前两个部分我已经关闭但是将FTP上的文件移动到同一FTP上的另一个文件夹的部分让我沮丧。

有人可以提供一些建议吗?

这就是我目前拥有的:

@Echo Off
Set _FTPServerName=SERVER HERE
Set _UserName=LOGIN HERE
Set _Password=PASSWORD HERE
Set _RemoteFolder=FILES FROM
Set _NewRemoteFolder=FILES TO
Set _Filename=*.*
Set _ScriptFile=ftp1

:: Create script
"%_ScriptFile%" Echo open %_FTPServerName%
"%_ScriptFile%" Echo %_UserName%
"%_ScriptFile%" Echo %_Password%
"%_ScriptFile%" Echo cd %_RemoteFolder%
"%_ScriptFile%" prompt

:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%"

如果您还有其他问题,请与我们联系!

file batch-file cmd ftp transfer
2个回答
2
投票

Windows rename command命令行客户端中有ftp.exe

rename oldname newname

所以在你的特定批处理文件中:

"%_ScriptFile%" echo rename file.txt %_NewRemoteFolder%/file.txt

虽然在你的问题中看到了_Filename=*.*,但实际上看起来你想要移动所有文件。使用Windows ftp.exe是不可能的,至少不容易。 rename命令不支持通配符。

您必须基于下载文件列表动态生成脚本文件,并为每个文件使用单独的rename命令。


或者在重命名/移动时使用支持通配符的其他命令行FTP客户端。

例如,使用WinSCP scripting,批处理文件将类似于:

winscp.com /log=winscp.log /command ^
    "open ftp://username:[email protected]" ^
    "cd %_RemoteFolder%" ^
    "mv *.* %_NewRemoteFolder%/" ^
    "exit"

详情见:

(我是WinSCP的作者)


0
投票

处理此问题的最直接方法是使用PowerShell。这将使文件复制发生在FTP主机上,而不需要在写回服务器之前将数据发送到客户端。

Invoke-Command -ComputerName FTPHOST -ScriptBlock { Move-Item -Path C:\dir1\thefile.txt -Destination C:\dir2\thefile.txt }

您可以从cmd.exe shell运行此命令。

powershell -NoProfile -Command "Invoke-Command -ComputerName FTPHOST -ScriptBlock { Move-Item -Path C:\dir1\thefile.txt -Destination C:\dir2\thefile.txt }"
© www.soinside.com 2019 - 2024. All rights reserved.