【问题标题】:How to compress each subfolder in a folder into a separate RAR archive using WinRAR?如何使用 WinRAR 将文件夹中的每个子文件夹压缩成单独的 RAR 存档?
【发布时间】:2014-09-15 22:38:08
【问题描述】:

我是批处理文件编码的新手,需要您的帮助。

我有这些目录:

c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg

c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg

c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg

我想把它们压缩成这个

c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar

如何使用 WinRAR 做到这一点?

【问题讨论】:

    标签: batch-file directory rar winrar


    【解决方案1】:

    这也可以通过 WinRAR 完成,而不使用批处理文件,不完全按照要求,但与想要的类似。

    1. 启动 WinRAR 并导航到文件夹 c:\rar\
    2. 选择文件夹temp1temp2temp3,然后点击工具栏中的按钮添加
    3. 现在作为存档名称指定 RAR 存档的文件夹,例如 c:\rar\
    4. 切换到标签 文件 并检查选项将每个文件单独归档
    5. 点击按钮确定

    WinRAR 现在在文件夹c:\rar\ 中创建三个RAR 压缩文件,文件名为temp1.rartemp2.rartemp3.rar,每个压缩文件都包含包含所有文件和子文件夹的适当文件夹。

    也可以在标签文件上更改要添加的文件列表,方法是在要排除的文件中输入例如*.txt以忽略三个文件夹中的文本文件创建档案。

    最后,在 无需压缩即可存储的文件下方的编辑字段中的标签 文件 上输入 *.jpg 是有意义的,因为 JPEG 文件通常包含已压缩的数据,因此 WinRAR 无法真正进一步压缩文件的数据。


    这也是一个批处理文件解决方案,用于将c:\rar\ 的所有非隐藏子文件夹及其子文件夹中的文件移动 到归档文件中,并根据要求在每个子文件夹中创建子文件夹的名称。

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "RAREXE=Rar.exe"
    
    if exist "%RAREXE%" goto CreateArchives
    if exist "%ProgramFiles%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles%\WinRAR\Rar.exe" & goto CreateArchives
    if exist "%ProgramFiles(x86)%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles(x86)%\WinRAR\Rar.exe" & goto CreateArchives
    for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
        if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
    )
    for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
        if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
    )
    for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe Rar.exe 2^>nul') do set "RAREXE=%%I" & goto CreateArchives
    
    echo ERROR: Could not find Rar.exe!
    echo/
    echo Please define the variable RAREXE at top of the batch file
    echo "%~f0"
    echo with the full qualified file name of the executable Rar.exe.
    echo/
    pause
    goto :EOF
    
    :CreateArchives
    set "Error="
    for /D %%I in ("c:\rar\*") do (
        echo Creating RAR archive for "%%I" ...
        "%RAREXE%" m -@ -cfg- -ep1 -idq -m3 -msgif;png;jpg;rar;zip -r -s- -tl -y -- "%%I\%%~nxI.rar" "%%I\"
        if errorlevel 1 set "Error=1"
    )
    if defined Error echo/& pause
    endlocal
    

    set "RAREXE=Rar.exe" 之后直到 :CreateArchives 的行可以在使用正确的完整限定文件名定义环境变量 RAREXE 时省略。

    请阅读WinRAR程序文件夹中的文本文件Rar.txt,了解RAR命令m和使用的开关。该问题根本不包含应使用哪些选项创建 RAR 档案的任何信息。

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

    • call /? ...解释%~f0 ...批处理文件的全名
    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • if /?
    • pause /?
    • reg /?
    • reg query /?
    • set /?
    • setlocal /?
    • where /?

    有关运算符& 的说明,另请参阅single line with multiple commands using Windows batch file

    阅读有关Using command redirection operators 的Microsoft 文档以了解2>nul 的解释。当 Windows 命令解释器在执行命令 FOR 在后台启动的单独命令进程中执行嵌入的regwhere 命令行。

    【讨论】:

      【解决方案2】:

      在 Python v3.x 中:

      • 在 Python v3.7 上测试
      • 在 Windows 10 x64 上测试
      import os
      
      # NOTE: Script is disabled by default, uncomment final line to run for real.
      base_dir = "E:\target_dir"
      # base_dir = os.getcwd()  # Uncomment this to run on the directory the script is in.
      
      # Stage 1: Get list of directories to compress. Top level only.
      sub_dirs_raw = [os.path.join(base_dir, o) for o in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, o))]
      
      # Stage 2: Allow us exclude directories we do not want (can omit this entire section if we wish).
      dirs = []
      for d in sub_dirs_raw:
          if "legacy" in d or "legacy_old" in d:
              continue  # Skip unwanted directories
          print(d)
          dirs.append(d)
      
      # Stage 3: Compress directories into .rar files.
      for d in dirs:
          os.chdir(d)  # Change to target directory.
          # Also adds 3% recovery record using "-rr3" switch.
          cmd = f"\"C:\Program Files\\WinRAR\\rar.exe\" a -rr3 -r {d}.rar *"
          print(cmd)
          # Script is disabled by default, uncomment this next line to execute the command.
          # os.system(cmd)
      

      注意事项:

      • 脚本只会打印命令,除非通过删除前导 # 来取消注释最后一行 os.system(cmd)
      • 运行脚本,它会打印出它将执行的 DOS 命令。如果您对结果感到满意,请取消注释最后一行以真正运行它。
      • 示例:如果有一个目录包含三个文件夹mydir1mydir2mydir3,则会创建三个.rar文件:mydir1.rarmydir2.rarmydir3.rar
      • 此演示代码将跳过名称中包含“legacy”和“legacy_old”的目录。您可以更新以添加您自己的目录以跳过。
      • 要执行脚本,安装Python 3.x,将上面的行粘贴到script.py,然后从任意目录运行DOS 命令python script.py。使用第二行设置目标目录。或者,使用 PyCharm 运行脚本。

      【讨论】:

      • 不知道为什么这个有 0 票。我总是更喜欢 Python 脚本而不是 Windows 批处理。 Python 更具可读性和功能性。我希望目标目录不会被硬编码。这样您就可以将此脚本放在您想要的目录中并运行它,它将假定目标目录与您放入脚本的目录相同。
      • @Curtwagner1984 当然,已将脚本更新为 natch。要将脚本放在您想要的目录中,然后运行它,它将假定目标目录与脚本所在的目录相同,请取消注释脚本中的第四行。
      【解决方案3】:

      这个脚本也可以工作:

      @echo off
      for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
          pushd "%%~a"
          "C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
          popd
      )
      

      【讨论】:

        【解决方案4】:

        这应该可以工作,它还会检查文件是否被正确压缩。 您可能需要根据安装 winrar 的位置更改这部分“cd Program Files\WinRAR”。

        @echo Off
        Cd\
        cd Program Files\WinRAR
            rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
                if "%ERRORLEVEL%"=="0" ( Echo Files compressed
                ) Else Echo Failed
            rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
                if "%ERRORLEVEL%"=="0" ( Echo Files compressed
                ) Else Echo Failed
            rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
                if "%ERRORLEVEL%"=="0" ( Echo Files compressed
                ) Else Echo Failed
        Pause
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-01
          • 2017-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-07
          • 2016-12-17
          • 2022-12-10
          相关资源
          最近更新 更多