【问题标题】:Windows batch file - Upload only latest file to FTPWindows 批处理文件 - 仅将最新文件上传到 FTP
【发布时间】:2017-02-19 15:49:29
【问题描述】:

我想将文件从 Windows 服务器自动传输到我的 FTP。

问题是生成的文件名称中带有时间戳(名称不固定)。所以我只需要上传文件的最后一个版本(最新)(基于实际的文件时间戳,而不是名称中的时间戳)。有什么办法可以做到吗?

在 Windows Server 2003 下运行。谢谢。

【问题讨论】:

    标签: windows batch-file ftp


    【解决方案1】:

    要选择 Windows 批处理文件中的最新文件,请参阅
    How do I write a Windows batch script to copy the newest file from a directory?

    基于此,您可以创建一个上传批处理文件,如:

    @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
    

    为了更简单、更可靠的方法,请使用功能更强大的 3rd 方 FTP 客户端。

    例如使用WinSCP FTP client,您可以使用其put command-latest switch

    一个示例批处理文件 (.bat):

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

    您甚至可以拥有WinSCP generate the script/batch file for you(您只需手动添加-latest 开关)。

    WinSCP article on Uploading the most recent file

    (我是 WinSCP 的作者)

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多