【问题标题】:Batch script to move compress and delete files用于移动压缩和删除文件的批处理脚本
【发布时间】:2013-03-06 20:46:07
【问题描述】:

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

@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:我在上面的脚本中有两个单独的命令。一种是压缩一周前的文件,另一种是在压缩后删除原始文件。我如何链接这两个命令,以便如果由于某种原因文件没有被压缩,它们也不应该被删除。或者如果文件没有被压缩,是否有命令来破坏脚本?我只想先压缩文件,然后删除原始文件

【问题讨论】:

    标签: winrar


    【解决方案1】:

    我建议使用

    @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 抑制任何错误或警告消息的输出。此开关主要用于控制台版本Rar.exe(不支持 ZIP 压缩)用于输出到 stdout 和 stderr,但也可能适用于 WinRAR。当由于文件不超过 7 天而未创建 ZIP 文件时,我从未见过使用 WinRAR.exe 版本 4.20 对我的测试进行确认的诊断消息。我已经看到有关使用Rar.exe 创建RAR 存档而不使用-inul 的警告,而且即使不使用开关-y 也不需要按键。

    我删除了用于递归存档的开关 -r,因为这里不需要,始终只将 1 个文件移动到 ZIP 存档中。

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

    最后一个开关-y 被添加以在所有查询中假设Yes,尽管我从未在我的测试中看到过。

    更多提示:
    在 NTFS 分区上,可以在文件夹上设置属性 compressed,从而自动 ZIP 压缩在该文件夹中复制或创建的所有文件,以节省磁盘存储空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多