【问题标题】:Compress subdirectories压缩子目录
【发布时间】:2016-04-18 03:54:54
【问题描述】:

我正在尝试用这样的结构压缩我的子目录

Files1\files11\...(files)
Files1\files12\...(files)
Files2\files21\...(files)
Files2\files22\...(files)

我想把它压缩成这样

Files1\files11.rar
Files1\files12.rar
Files2\files21.rar
Files2\files22.rar

不删除原始文件夹。

我试过这个脚本

for /d %%d in (*\*) do ("C:\Program Files\WinRAR\Rar.exe" a -m5 -s "%%d.rar" "%%d")

但它只适用于一个级别。 我也试过Batch file to compress subdirectories,但由于某种原因它不起作用。它会抛出文件未找到错误。

谢谢

【问题讨论】:

    标签: windows batch-file compression rar


    【解决方案1】:

    Windows 命令行不支持文件/文件夹路径中的文件夹的通配符。 *\* 因此无效。

    需要两个嵌套的 FOR 循环,外部循环在当前目录中运行,处理目录Files1Files2,内部循环处理目录files11files12,@987654326 @、files22 并在这些子目录上运行 Rar

    第一批代码创建存档文件

    Files1\files11.rar
    Files1\files12.rar
    Files2\files21.rar
    Files2\files22.rar
    

    每个存档中都包含存档目录files11files12files21files22

    @echo off
    set "CompressionError=0"
    
    rem Set directory of batch file as working directory.
    pushd "%~dp0"
    
    rem For each directory in working directory run a second loop which
    rem compresses each subdirectory of current directory to a RAR archive
    rem with deleting all files and subdirectories as well as the archived
    rem directory itself and name the archive file like the archived directory.
    
    for /D %%D in (*) do (
        for /D %%F in ("%%D\*") do (
            "%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -df -ep1 -idq -m5 -md4m -r -s -y "%%F.rar" "%%F"
            if errorlevel 1 set "CompressionError=1"
        )
    )
    
    rem Restore the previous working directory.
    popd
    
    rem Halt batch processing if any error occurred on any compression.
    if "%CompressionError%" == "1" pause
    set "CompressionError="
    

    第二个批处理代码也创建了 4 个存档文件,但存档目录 files11files12files21files22 未添加到存档中。

    @echo off
    set "CompressionError=0"
    
    rem Set directory of batch file as working directory.
    pushd "%~dp0"
    
    rem For each directory in working directory run a second loop which
    rem compresses each subdirectory of current directory to a RAR archive
    rem with deleting all files and subdirectories as well as the archived
    rem directory itself and name the archive file like the archived directory.
    
    for /D %%D in (*) do (
        for /D %%F in ("%%D\*") do (
            "%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -df -ep1 -idq -m5 -md4m -r -s -y "%%F.rar" "%%F\"
            if errorlevel 1 (
                set "CompressionError=1"
            ) else (
                rd "%%F"
            )
        )
    )
    
    rem Restore the previous working directory.
    popd
    
    rem Halt batch processing if any error occurred on any compression.
    if "%CompressionError%" == "1" pause
    set "CompressionError="
    

    查看WinRAR 程序文件夹中的文本文件Rar.txt 了解使用Rar 开关的详细信息,这是WinRAR 控制台版本的手册 em>。

    两个批处理文件在执行过程中不显示任何内容,如果没有出现错误,则会自动退出。但是如果发生任何压缩错误,批处理会在最后停止,以便批处理用户可以查看Rar输出到控制台窗口的错误消息。

    要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • call /? 解释 %~dp0(参数 0 的驱动器和路径 - 批处理文件)
    • echo /?
    • for /?
    • if /?
    • pause /?
    • popd /?
    • pushd /?
    • rd /?
    • rem /?
    • set /?

    【讨论】:

    • 谢谢。虽然有些东西我还是不明白。 1. 为什么要在批处理文件的最后一步设置CompressionError为空?
    • 2. %%D 和 %%F 是指目录和文件吗?
    • set "CompressionError=0" 也可能只是 set "CompressionError=" 保证没有环境变量 CompressionError 在此批处理脚本之外偶然定义的值 1 导致在结束时停止批处理这个批处理脚本。 %%D%%F 是目录的循环变量。 %%D 用于目录 Files1Files2%%F 用于具有当前目录相对路径的子目录,例如 Files1\files11。添加到外循环echo %%D 和内循环echo %%F 可以让您看到循环变量值。
    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 2012-12-02
    • 2011-12-31
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    相关资源
    最近更新 更多