Batch script to move compress and delete files
-
Wednesday, March 06, 2013 8:48 PM
I have a batch script that does the following @ECHO OFF
REM move files older than 2 days from the incoming directory to the incoming archive directory robocopy D:\Agentrics\integration\download D:\Agentrics\integration\download\archive /MOV /MINAGE:2
REM Zip files in the Archieve directory that are older than one week FOR %%A IN (D:\Agentrics\integration\download\archive*.txt*, D:\Agentrics\integration\download\archive*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r -to7d D:\Agentrics\integration\download\archive\"%%~nA.zip" "%%A"
REM Delete Original files after they are zipped forfiles /p D:\Agentrics\integration\download\archive /s /m .txt /d -7 /c "cmd /c del /q @path" forfiles /p D:\Agentrics\integration\download\archive /s /m .cpi /d -7 /c "cmd /c del /q @path"
REM Delete files that are older than 6 months from the archive directory forfiles /p D:\Agentrics\integration\download\archive /s /m .zip /d -180 /c "cmd /c del /q @path" pause
Question 1: When i run the script i get WinRAR diagnostic messages for some files. For example if there are files in the incoming directory that are not older than two days i get this message."WinRAR Diagnostic messages: No File To Add". Because of this message the scripts stops until i click on the close button of the dialogue box. I am using the free version of WinRAR and its not expired
Question 2: I have two seprate command in the script above. One is for zipping the files older than a week and the other one is deleting the original files after they are zipped. How can i link those two commands so that if some reason the files did not get zipped they should also not get deleted. Or is is there a command to break the script if the files did not get zipped? I just want to zipp the files first and then delete the original ones.
Thanks
All Replies
-
Wednesday, March 06, 2013 9:06 PM
Question 1 is not a scripting question but a WinRAR question. You need to work out if there is an option in WinRAR that forces WinRAR.exe to continue even if no files are found. Perhaps a different version of WinRAR will do the trick.
If WinRAR generates an error level > 0 when failing to compress some files then you can use this like so to exit the batch file immediately:
winrar.exe . . . . || goto :eof
- Marked As Answer by IamMredMicrosoft Employee, Owner Monday, May 06, 2013 8:12 PM

