批处理脚本以移动压缩和删除文件 [英] Batch script to move compress and delete files

查看:355
本文介绍了批处理脚本以移动压缩和删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行以下操作的批处理脚本

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

问题1: 当我运行脚本时,会得到某些文件的WinRAR诊断消息.例如,如果传入目录中有不超过两天的文件,则会收到此消息."WinRAR诊断消息:没有要添加的文件".由于此消息,脚本将停止,直到我单击对话框的关闭按钮.我使用的是WinRAR的免费版本,并且尚未过期

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

问题2:在上面的脚本中,我有两个单独的命令.一种是压缩超过一周的文件,另一种是压缩原始文件后将其删除.我如何链接这两个命令,以便如果由于某种原因文件未压缩,也不应删除它们.或者,如果没有压缩文件,是否有命令来中断脚本?我只想先压缩文件,然后删除原始文件

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

推荐答案

我建议使用

@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 仅在成功压缩后才删除文件.

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.

使用开关-afzip明确通知 WinRAR 使用ZIP而不是RAR压缩.

Using switch -afzip informs WinRAR explicitly to use ZIP instead of RAR compression.

开关-ep导致从存档内文件名中删除路径.

The switch -ep results in removing path from the file names inside the archive.

任何错误或警告消息的输出都可以通过开关-inul抑制.此开关主要用于控制台版本Rar.exe(不支持ZIP压缩),以输出到stdout和stderr,但也可以用于 WinRAR .当未创建ZIP文件时,因为该文件的存在时间不超过7天,所以我从未见过诊断消息确认使用WinRAR.exe 4.20版进行测试.我已经看到了有关使用Rar.exe创建RAR归档文件而不使用-inul的警告,但是即使不使用开关-y,也不需要击键.

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.

我删除了-r开关,以进行递归归档,此处始终只将1个文件移动到ZIP存档中.

I removed switch -r for recursive archiving as not needed here with always moving only 1 file to a ZIP archive.

未经修改的开关-to7d只能归档7天以上的文件.

The unmodified switch -to7d results in archiving only files older than 7 days.

最后一次添加-y开关是为了在所有查询中假设,尽管我从未在测试中看到过.

Last the switch -y is added to assume Yes on all queries although I have never seen one on my tests.

另一个提示:
在NTFS分区上,可以在文件夹上设置属性 compressed (压缩),从而自动对在此文件夹中复制或创建的所有文件进行ZIP压缩,以节省磁盘存储空间.

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.

这篇关于批处理脚本以移动压缩和删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆