简体   繁体   English

Windows 批处理文件 - 仅将最新文件上传到 FTP

[英]Windows batch file - Upload only latest file to FTP

I want to do an automatic file transfer from Windows server to my FTP.我想从 Windows 服务器到我的 FTP 进行自动文件传输。

Problem is that file is generated with timestamp in its name (the name is not fixed).问题是文件的名称中带有时间戳(名称不固定)。 So I need to upload always only the last version (newest) of file.所以我只需要上传文件的最后一个版本(最新)。 Is there any way how to do that?有没有办法做到这一点?

Running under Windows Server 2003. Thank you.在 Windows Server 2003 下运行。谢谢。

To select the newest file in a Windows batch file, see要选择 Windows 批处理文件中的最新文件,请参阅
How do I write a Windows batch script to copy the newest file from a directory?如何编写 Windows 批处理脚本以从目录中复制最新文件?

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.要获得更简单、更可靠的方法,请使用一些更强大的 3rd 方 FTP 客户端。

For example with WinSCP FTP client , you can use the -latest switch of its put command .例如,对于WinSCP FTP 客户端,您可以使用其put命令-latest开关

An example batch file ( .bat ):示例批处理文件 ( .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).您甚至可以让WinSCP 为您生成脚本/批处理文件(您只需要手动添加-latest开关)。

See WinSCP article on Uploading the most recent file .请参阅有关上传最新文件的 WinSCP 文章

(I'm the author of WinSCP) (我是 WinSCP 的作者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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