简体   繁体   English

批处理文件仅将今天的文件上传到 S3(获取文件名作为列表,循环,执行 cp 命令)

[英]Batch file to upload only today's files to S3 (get filenames as list, loop through, execute cp command)

I need to create a script to copy only the files created today in Z:\\ drive to AWS S3 using aws s3 cp command at a scheduler basis.我需要创建一个脚本,以在调度程序的基础上使用aws s3 cp命令仅将今天在Z:\\驱动器中创建的文件复制到 AWS S3。

For example in Z:\\, currently I have these files:例如在 Z:\\,目前我有这些文件:

Filename                                            Date Modified
BELSIZE.0.DB2.NODE0000.CATN0000.20160313090011.001  3/13/2016 9:00 AM
BELSIZE.0.DB2.NODE0000.CATN0000.20160314090015.001  3/14/2016 9:00 AM
BELSIZE.0.DB2.NODE0000.CATN0000.20160315090010.001  3/15/2016 9:00 AM
BELSIZE.0.DB2.NODE0000.CATN0000.20160315100012.001  3/15/2016 10:00 AM

I need to copy only the last 2 files dated 3/15/2016 to AWS S3.我只需要将日期为 3/15/2016 的最后 2 个文件复制到 AWS S3。

What I have now is:我现在拥有的是:

  1. List the path + filename created today:列出今天创建的路径+文件名:

     forfiles /P "Z:\\" /s /m *.* /d +0 /c "cmd /c echo @path
  2. Copy file to AWS S3:将文件复制到 AWS S3:

     aws s3 cp Z:\\BELSIZE.0.DB2.NODE0000.CATN0000.20160315090010.001 s3://backup

I am thinking of getting 1) as a list, loop through 1) and execute 2).我正在考虑将 1) 作为列表,循环 1) 并执行 2)。 How can I do this?我该怎么做?

I tried the below command.我尝试了以下命令。 The file created today doesn't get copied to S3.今天创建的文件不会被复制到 S3。

forfiles /P "Z:\" /s /m *.001 /d +0 /c "cmd /c aws s3 cp @path s3://backup"

This is easy with WinSCP.使用 WinSCP 这很容易。 To upload only today's files, use a batch file like:要仅上传今天的文件,请使用批处理文件,例如:

winscp.com /log=S3.log /ini=nul /command ^
    "open s3://S3KEY:S3SECRET@s3.amazonaws.com/" ^
    "put -filemask=>=today Z:\* /bucket/" ^
    "exit"

The >=today keyword is supported by WinSCP 5.15 and newer only. >=today关键字仅受 WinSCP 5.15 及更新版本支持。 In older versions, you can use %TIMESTAMP% syntax :在旧版本中,您可以使用%TIMESTAMP%语法

    "put -filemask=>=%%TIMESTAMP#yyyy-mm-dd%% Z:\* /bucket/" ^

You need to URL-encode special characters in the credentials .您需要对凭据中的特殊字符进行URL 编码 WinSCP GUI can generate an S3 script template , like the one above, for you. WinSCP GUI 可以为您生成一个 S3 脚本模板,就像上面的那样。

Alternatively, since WinSCP 5.19, you can use -username and -password switches, which do not need any encoding:或者,从 WinSCP 5.19 开始,您可以使用-username-password开关,它们不需要任何编码:

    "open s3://s3.amazonaws.com/ -username=S3KEY -password=S3SECRET" ^

References:参考资料:

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

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

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