简体   繁体   中英

Batch file to move files from one FTP folder to another folder in the same FTP?

Good morning all,

I have a task where i need to automate a process where we download files from an FTP site, the files are processed in SQL, and then the files on the FTP site are moved to another folder on that same FTP site.

The first two parts i have down but the part of moving the files on the FTP to another folder on the same FTP is tripping me up.

Can anyone please offer some advice?

This is what i currently have:

@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%"

Please let me know if you have further questions!

There's rename command in Windows ftp.exe command-line client:

rename oldname newname

So in your specific batch file:

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

Though seeing the _Filename=*.* in your question, it actually looks like you want to move all files. That's not possible with Windows ftp.exe , at least not easily. The rename command does not support wildcards.

You would have to dynamically generate a script file based on a list of downloaded files with a separate rename command for each file.


Or use a different command-line FTP client that supports wildcards when renaming/moving.

For example with WinSCP scripting the batch file would be like:

winscp.com /log=winscp.log /command ^
    "open ftp://username:password@example.com" ^
    "cd %_RemoteFolder%" ^
    "mv *.* %_NewRemoteFolder%/" ^
    "exit"

For details see:

(I'm the author of WinSCP)

The most direct way to handle this is to use PowerShell. This will make the file copy happen on the FTP host without requiring the data to come to your client before being written back to the server.

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

You can run this from a cmd.exe shell.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM