简体   繁体   中英

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 . 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;
  • Always a pair: a .xlsx and a .pdf file;
  • I need to copy only the newest pair.
@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 ):

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>nul cd/d    ...    &&  || (
 )

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.

dir

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% ).

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 .

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* financial* ... only if there are both, the && operator (means return 0 ) will execute the your robocopy command: financial* 可以在先前验证条件是否存在的循环中使用当前文件的名称成对存在&&运算符(表示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.

  •  @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.: 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. 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%%")

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