简体   繁体   English

机器复制 | 只复制最新的一对文件

[英]Robocopy | copy only the newest pair of files

I've been trying writing a script that will copy a pair of files.我一直在尝试编写一个脚本来复制一对文件。 They're pretty much the same file: one in .xlsx and another in .pdf .它们几乎是同一个文件:一个在.xlsx中,另一个在.pdf中。 Being financial reports, a pair of them is created whenever it needs to be created (not to say randomly).作为财务报告,只要需要创建它们就会创建一对(不是随机创建)。 And there are many pairs for every project folder.每个项目文件夹都有很多对。

I want to我想要

  • Copy only the files that begin with "financial" - since that's a pattern between them;只复制以"financial"开头的文件——因为这是它们之间的模式;
  • Always a pair: a .xlsx and a .pdf file;总是一对:一个.xlsx和一个.pdf文件;
  • I need to copy only the newest pair.我只需要复制newest的一对。
@CHCP 65001
@echo off
robocopy "C:\source" "C:\dest" financial* (.xlsx*.pdf*) /XO /E /R:3
pause

The code I wrote above helped me.我上面写的代码帮助了我。 But it didn't fit my third condition , which is to copy only the newest pair.但它不符合我的第三个条件,即只复制最新的一对。 Does anybody know how?有人知道怎么做吗?

@echo off & >nul chcp 65001

2>nul cd/d "C:\Source\" && set "_destiny=\\to\target\folder\." || (
      timeout /t -1 | echo\Path do not exist: C:\Source && goto :eOf ) 

for /f tokens^=* %%i in ('dir/b/a-d/o-d/tc .\financial*.*^|findstr/lei "\.pdf \.xlsx"'
      )do 2>nul robocopy .\. "%_destiny%" "%%~ni.xlsx" "%%~ni.pdf" /mov /njh|find "100%%"

1. Go to the source folder, source of the files, suppress any possible error ( 2>nul ): 1. Go 到源文件夹,文件的源,抑制任何可能的错误( 2>nul ):

2>nul cd/d "C:\Source\."

2. If the folder exist, define your target, if it does not exist (if the bat is executed in the wrong location/work station), interrupt and alert your user.. 2.如果文件夹存在,定义你的目标,如果它不存在(如果在错误的位置/工作站执行bat),中断并提醒你的用户..

2>nul cd/d    ...    && set "_destiny=\\to\target\folder\." || (
 timeout -1 | echo\Path do not exist: C:\Source && goto :eOf )

3. List your files in order to get the most recent ones in your folder, and redirect that listing to Findstr to literally filter you for files ending in the desired extensions. 3.列出您的文件以获取文件夹中的最新文件,并将该列表重定向到Findstr以逐字过滤您以查找以所需扩展名结尾的文件。

dir/b/a-d/o-d/tc .\financial*.*^|findstr/lei "\.pdf \.xlsx"

4. Inform/pass the result of the looped command to robocopy (only the file name) but already limiting to copy (moving) both extensions, also the current folder ( .\. ) of source, and the target ( %_destiny% ). 4.将循环命令的结果通知/传递给robocopy (仅文件名),但已限制复制(移动)两个扩展名,以及源的当前文件夹 ( .\. ) 和目标 ( %_destiny% ) .

robocopy .\. "%_destiny%" "%%~ni.xlsx" "%%~ni.pdf" /mov /njh

5. Just a suggestion to limit the output, take the result per line where it appears 100%, redirecting the robocopy output to the find . 5.只是一个限制 output 的建议,在每行显示 100% 的地方获取结果,将robocopy output 重定向到find

robocopy    ...     |find "100%%"

Obs.: 1. You can also use the name of the current file in a loop in a previous verification of the condition to exist or not in pairs, using a dir financial* .xlsx financial* .pdf ... only if there are both, the && operator (means return 0 ) will execute the your robocopy command: dir financial* .xlsx financial* .pdf可以在先前验证条件是否存在的循环中使用当前文件的名称成对存在&&运算符(表示return 0 )都将执行您的robocopy命令:

  •  @echo off   2>nul cd/d "C:\Source\." && set "_destiny=\\to\target\folder\." || ( timeout -1 | echo\Path do not exist: C:\Source && goto:eOf )   1>nul chcp 65001 && set "_cmd=dir/b/ad/od/tc.\"financial*.*""   for /f tokens^=* %%i in ('%_cmd%^|findstr/lei "\.pdf \.xlsx"')do 2>nul dir /b.\"%%~ni.xlsx".\"%%~ni.pdf" && ( 2>nul robocopy.\. "%_destiny%" "%%~ni.xlsx" "%%~ni.pdf" /mov /njh|find "100%%")

Obs.: 2. If the objective is to move a pair, not pairs, just add & goto: eof , causing the batch/loop to end/abort immediately. Obs.: 2.如果目标是移动一对,而不是对,只需添加& goto: eof ,导致批处理/循环立即结束/中止。

  •  @echo off   2>nul cd/d "C:\Source\" && set "_destiny=\\to\target\folder\." || ( timeout -1 | echo\Path do not exist: C:\Source && goto:eOf )   1>nul chcp 65001 && set "_cmd=dir/b/ad/od/tc.\"financial*.*""   for /f tokens^=* %%i in ('%_cmd%^|findstr/lei "\.pdf \.xlsx"')do 2>nul dir /b.\"%%~ni.xlsx".\"%%~ni.pdf" && ( 2>nul robocopy.\. "%_destiny%" "%%~ni.xlsx" "%%~ni.pdf" /mov /njh|find "100%%" & goto:eOf )

Obs.: 3. If for some reason it is necessary to move files, some of which already exist in the destination folder, then you will need to overwrite it, then use/add /IS in your robocopy command. Obs.: 3.如果由于某种原因需要move文件,其中一些文件已经存在于目标文件夹中,那么您需要覆盖它,然后在您的robocopy命令中使用/添加/IS It is这是

  • @echo off   1>nul chcp 65001 & set "_cmd=dir /b/a:-d/o:-d/t:c ".\financial*.*"" 2>nul cd /d "C:\Source\." && set "_target=\\to\target\folder\." || ( timeout /t -1 | echo\Path/Source do not exist: C:\Source & goto:eOf)   for /f tokens^=* %%i in ('2^>nul %_cmd% ^|findstr/lei "\.pdf \.xlsx"')do 2>nul ( 2>nul robocopy.\. "%_target%" "%%~ni.xlsx" "%%~ni.pdf" /mov /njh /is|find "100%%")

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

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