Sena
Sena

Reputation: 31

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

@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?

Upvotes: 1

Views: 1367

Answers (1)

Io-oI
Io-oI

Reputation: 2565

@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    ...    && 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.

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

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*.xlsx financial*.pdf... only if there are both, the && operator (means return 0) will execute the your robocopy command:

  • @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/a-d/o-d/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/a-d/o-d/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. 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%%")

Upvotes: 1

Related Questions