2013-03-06 67 views
0

我有一个批处理脚本,做以下批处理脚本移动压缩和删除文件

@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 

问题1: 当我运行该脚本,我得到了一些文件,WinRAR的诊断信息。例如,如果传入目录中的文件不超过两天,我会收到此消息。“WinRAR诊断消息:无文件添加”。由于这个消息,脚本停止,直到我点击对话框的关闭按钮。我正在使用免费版本的WinRAR并且它没有过期

问题2:我在上面的脚本中有两个seprate命令。一个是压缩一周以上的文件,另一个是压缩后删除原始文件。我如何链接这两个命令,以便如果文件没有被压缩的原因,他们也不应该被删除。或者,如果文件没有被压缩,是否有命令来中断脚本?我只是想先旗牌文件,然后删除原有的

回答

0

我sugest使用

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

整个过程可以通过使用命令m这意味着此举可以简化归档而不是命令a这意味着添加到档案WinRAR只有在成功压缩后才会删除文件。

使用开关-afzip通知WinRAR显式使用ZIP而不是RAR压缩。

开关-ep导致从档案内的文件名删除路径。

使用开关-inul可以抑制任何错误或警告消息的输出。此开关主要用于输出到stdout和stderr的控制台版本Rar.exe(不支持ZIP压缩),但可能也适用于WinRAR。当我未使用WinRAR.exe版本4.20进行测试时,由于该文件不超过7天,我从来没有看到过诊断消息来确认我的测试。我看到了使用Rar.exe创建RAR档案而不是使用-inul的警告,但是即使没有使用开关-y,也不需要重点击中。

我删除了开关-r递归存档,因为这里不需要,总是只将1个文件移动到ZIP存档。

未修改的开关-to7d导致仅存档超过7天的文件。

最后交换机-y被添加到假设对所有查询,虽然我从来没有见过我的测试。

还有一个提示:
NTFS分区上压缩可以导致复制或创建该文件夹中保存的磁盘存储的所有文件的自动ZIP压缩的文件夹上设置的属性