Midox
Midox

Reputation: 3

Batch file to copy newer folder to other location

I'm trying to write a batch file that will copy newest folder from one location to another.

In folder name DIR1 every day system is making backup and creating new folder in it (for example ABC backup 01_10_2019) with 2 files in it. I have over 100 folders in that map (DIR1) so i need to copy only newest folder to another location.

I need to script copy only newest folder from DIR1 and to DIR2

So far I have this but it doesn't work:

FOR /F "delims=|" %%I IN ('DIR "C:\DIR1" /B /AD /O:D') DO SET NewestFile=%%I
xcopy "C:\DIR1\%NewestFile%" "C:\DIR2" /E /H /K /Y
pause

Script for copying folder works great but I can't make script delete files older than 3 days because script is coping file from NAS1 to NAS2.

In script:

for /F "delims=" %%i IN ('DIR "\\NAS1\" /B /AD /O-D') DO SET "Latest=%%~I" & goto :done
:done
robocopy "\\NAS01\%Latest%" "\\nas02\backup\%Latest%" /MIR
Rem till here crups works
Rem tried
forfiles -p "\\nas02\backup" -s -m *.* -D 3 -C "cmd /c del @path"
Rem error UNC paths are not supported.
Rem tried
forfiles -p "\\nas02\backup" -d -3 -c "cmd /c IF @isdir == TRUE rd /S /Q @path"
Rem error UNC paths are not supported
Rem Can anyone help me please
Pause

Upvotes: 0

Views: 466

Answers (1)

user7818749
user7818749

Reputation:

Set it to latest folder first, set variable and exit the loop, then use robocopy to do the work.

for /F "delims=" %%i IN ('DIR "C:\DIR1" /B /AD /O-D') DO SET "Latest=%%~I" & goto :done
:done
robocopy "C:\DIR1\%Latest%" "C:\DIR2\%Latest%" /MIR
pause

Upvotes: 1

Related Questions