简体   繁体   中英

Batch file to Copy files (name_date) from FTP server to local directory?

I want to create a batch file, including the following functions:

Connection to a FTP server
Copying the files from there to a local directory (just today file named: product_yyyymmdd_hour.csv)

I haven't done that much with batch files so far, so it would be great if you could help me. I know there is the ftp command, and I know how to connect at ftp, but unfortunately I don't know how to copy those file. very day I must copy only today file. for example: Product_20120611_1233.csv Product_20120612_1115.csv etc. The source folder an destination folder are the same every day, just the file name is different. Thanks a lot for your help!

If you want to indicate the today date in batch you have to format it first. It depends on the date-format you use on your computer.

For example in an Italian format it will be dd/MM/yyyy, in the American one it will be MM/dd/yyyy.

Italian:

SET "day=%date:~-10,2%"
SET "month=%date:~-7,2%"
SET "year=%date:~-4%"
SET "dateStamp=%year%%month%%day%"

American:

SET "day=%date:~-7,2%"
SET "month=%date:~-10,2%"
SET "year=%date:~-4%"
SET "dateStamp=%year%%month%%day%"

Once you made this, you can specify the file name in a few ways, EG

SET "fileName=Product_%dateStamp%_*.csv"

Now, you can do a copy in this way:

FOR %%f IN (%yourPath%\%fileName%) DO (
    COPY %%f %whereToCopy%
)

I don't know how it works in a ftp-script, but you can always change the script you found.

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