问题
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
回答1:
I sugest to use
@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 Move each file in the archive directory that is older than one week into a ZIP archive.
FOR %%A IN (D:\Agentrics\integration\download\archive\*.txt*, D:\Agentrics\integration\download\archive\*.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" m -afzip -ep -inul -to7d -y "D:\Agentrics\integration\download\archive\%%~nA.zip" "%%A"
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"
The entire process can be simplified by using command m
which means move to archive instead of command a
which means add to archive. WinRAR removes the file only after successful compression.
Using switch -afzip
informs WinRAR explicitly to use ZIP instead of RAR compression.
The switch -ep
results in removing path from the file names inside the archive.
The output of any error or warning message can be suppressed with switch -inul
. This switch is mainly for the console version Rar.exe
(not supporting ZIP copmression) for output to stdout and stderr, but may work also for WinRAR. I have never seen a diagnostic message to confirm on my tests using WinRAR.exe
version 4.20 when the ZIP file was not created because the file was not older than 7 days. I have seen the warning on using Rar.exe
creating a RAR archive and not using -inul
, but also with no need for a key hit even without using switch -y
.
I removed switch -r
for recursive archiving as not needed here with always moving only 1 file to a ZIP archive.
The unmodified switch -to7d
results in archiving only files older than 7 days.
Last the switch -y
is added to assume Yes on all queries although I have never seen one on my tests.
One more hint:
On NTFS partitions the attribute compressed can be set on a folder resulting in an automatic ZIP compression of all files copied or created in this folder to save disk storage.
来源:https://stackoverflow.com/questions/15257650/batch-script-to-move-compress-and-delete-files