Поделиться через


How to Rar/Zip archive multiple folders into separate files using winrar

Here's the problem: I have a folder which contains hundreds of subfolders, and I wanted to make each subfolder into it's own ZIP file of the same name. Winrar doesn't have the built-in functionality to accomplish this goal, or none that I could find (feel free to correct me). And I'm not a fan of winzip or any other archival utilities, so I set out to get this accomplished via winrar.exe's command line interface.

 

I wrote a simple for loop to get the job done:

  for /F "eol= delims=" %%A IN (dir.txt) do winrar A -ep1 -r %%A.zip "y:\"%%A"\*.*"

 

Dir.txt is an ouput of a dir command listing all of the subdirectories. Use the following command to generate it without any superfluous information:

  dir /A:D /B >dir.txt

 

The for loop command breakdown (from above) is:

  for /F <set the end of line and delimiting characters, which in this case is nothing for both> <variable to use> IN <answer file> do <command>

 

The command in this example is winrar A -ep1 -r <target zip/rar file> <source files>. You can see that I was referencing files from a mapped drive, your siutation might be different.

  A = add files to archive

  -ep1 = do not add the base folder to the archive, instead add only the files inside that base subdirectory

  -r = recurse subdirectory

 

Winrar.exe has quite a few othe switches that you can play around with, but this is just a sample of how to get the command working for what I wanted to accomplish.

Comments