Using a loop to rar multiple subfolders in a main folder

怎甘沉沦 提交于 2019-12-24 10:46:38

问题


I have a main folder with 3 subfolders ...

  • C:\Users\Admin\Folder
    • Folder1
    • Folder2
    • Folder3

I want that the batch script grabs the first subdirectory Folder1, copy it to another location, for example C:\Temp\Folder and than WinRAR starts to archive Folder1. After copying Folder1 to another location it can be deleted in the main folder.

After WinRAR finished archiving Folder1 can also be deleted in C:\Temp\Folder. So only the .rar files remain.

Then the script starts from new with Folder2 and do the same as with Folder1.

So far I have only this and do not really know how to implement the above.

"C:\Program Files\WinRAR\Rar.exe" a -ep1 -mt5 -v50M -r -df "NAME-OF-THE-RAR-FILE" "C:\Users\Admin\Folder\*.*"

回答1:


Open a command prompt window, type and execute for /? and read help output for this command. The option /D is explained already on first help page which is for executing commands on each subdirectory of a directory.

The batch file below archives each subfolder in C:\Users\Admin\Folder using console version of WinRAR with command m (move = archive and delete on success) instead of command a with switch -df.

@echo off
for /D %%F in ("C:\Users\Admin\Folder\*") do (
    "%ProgramFiles%\WinRAR\Rar.exe" m -cfg- -ep1 -inul -m5 -mt5 -r -tl -v50M -y "%%~F.rar" "%%~F\"
    rd "%%~F"
)

So the result is

  • C:\Users\Admin\Folder
    Folder1.rar
    Folder2.rar
    Folder3.rar

The folder can contain even more files after archiving and deletion of the folders depending on size of all files of the folders and how much 50 MB volumes are necessary to archive each folder.

The folder name Folder1 is not included in file Folder1.rar because of the backslash in last parameter "%%~F\" at end of third line.

The batch file can be even easier if folder name Folder1 should be also included in the archive file Folder1.rar

@echo off
for /D %%F in ("C:\Users\Admin\Folder\*") do (
    "%ProgramFiles%\WinRAR\Rar.exe" m -cfg- -ep1 -inul -m5 -mt5 -r -tl -v50M -y "%%~F.rar" "%%~F"
)

I don't think that copying each folder to a temporary folder just for archiving is necessary here.



来源:https://stackoverflow.com/questions/29176771/using-a-loop-to-rar-multiple-subfolders-in-a-main-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!