且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批处理脚本以移动压缩和删除文件

更新时间:2023-12-05 19:52:34

我建议使用

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