Windows batch file - Upload only latest file to FTP

后端 未结 1 1418
终归单人心
终归单人心 2020-12-18 11:27

I want to do an automatic file transfer from Windows server to my FTP.

Problem is that file is generated with timestamp in its name (the name is not fixed). So I nee

相关标签:
1条回答
  • 2020-12-18 11:39

    To select the newest file in a Windows batch file, see
    How do I write a Windows batch script to copy the newest file from a directory?

    Based on that you can create an upload batch file like:

    @echo off
    
    FOR /F %%I IN ('DIR C:\source\path\*.* /B /O:D') DO SET NEWEST_FILE=%%I
    
    echo Uploading %NEWEST_FILE%
    
    (
        echo open ftp.example.com
        echo username
        echo password
        echo put C:\source\path\%NEWEST_FILE% /target/path/%NEWEST_FILE%
        echo bye
    ) > ftp.txt
    
    ftp.exe -s:ftp.txt
    

    For an easier and more reliable approach, use some more powerful 3rd party FTP client.

    For example with WinSCP FTP client, you can use the -latest switch of its put command.

    An example batch file (.bat):

    winscp.com /ini=nul /command ^
        "open ftp://username:password@ftp.example.com/" ^
        "put -latest C:\source\path\* /target/path/" ^
        "exit"
    

    You can even have WinSCP generate the script/batch file for you (you just need to add the -latest switch manually).

    See WinSCP article on Uploading the most recent file.

    (I'm the author of WinSCP)

    0 讨论(0)
提交回复
热议问题