Skip to content

Mass Zip, RAR to 7Zip Recompression Batch File

So, I forayed into creating a batch file to recompress lots of zip or rar files into the slightly higher compressed 7zip format. This is was to more tightly compact lots of random zip files I have in various places, and to stop Windows not helpfully searching them slowly whenever I did a search (I bet I can disable it, but it is useful for some vary limited amount of zip files). You’ll need the 7zip program to do this, and most of the examples here should be easily modifiable to suit needs. Yes, batch files are old ๐Ÿ˜€ but I don’t have another working scripting language handily installed. ๐Ÿ˜‰

These are also very basic batch files. They don’t contain confirmations or much in the way of error checking besides what I describe below, so be careful if you do use them. Run the files from the directory you want the script to work on, in Windows XP you can just double click on the .bat files to do this (it’ll also pause at the end of execution just in case you want to re-read anything).

Basic Compression

Relearning the batch scripts, here we go :) First, I got this up and running:

REM Test1.bat Batch file to run 7zip on a folder to 7zip it
"C:\Program Files\7-Zip\7z" a -t7z testarchive.7z testarchive\
pause

It did what it says on the tin – my sample folder was compressed. I wanted to check what it compressed – in this case, it created a 7zip file with the testarchive folder in addition to it’s contents. This is what I want to avoid :)

For extracting files, there was a good example in the actual 7zip help file:

REM Test2.bat - extracting files
"C:\Program Files\7-Zip\7z" x *.zip -o*
pause

Worked fine on my sample of zip files, for instance, my ASTROFC.zip file with one ASTROFC.exe file in it got extracted to the subdirectory ASTROFC (ending up with the file ASTROFC\ASTROFC.exe available). I need a specific file, so I test this line too:

REM Test3.bat - testing extracting one file
"C:\Program Files\7-Zip\7z" x ASTROFC.zip -o"ASTROFC.zip contents"
pause

Works perfectly – I want to make sure it is some folder with a post or prefix, since other folders might have already been extracted (I tend to leave them around sometimes). This means no accidental deletes will happen.

The For Loop

So, onto the For loop. The page here was pretty useful. I’ve only made a few necessary batch files before, and not needed many loops.

My For loop at first should print out the filenames that I want to work on, as I’m going for rar and zip, we have:

REM Test4.bat - tell me what rar and zip files are in the current directory.
for %%X in (*.rar *.zip) do echo %%X
pause

Worked fine! Now a final thing is to get just the filename with no extension for the final 7zip file itself. The Microsoft site helps with this (with that, I also rename the variable "X" with "F" for file):

REM Test5.bat - tell me what rar and zip files are in the current directory with no extension.
for %%F in (*.rar *.zip) do echo %%~nF
pause

This just omitted any .rar or .zip extensions – will be useful in a bit. Finally, we want it to extract these folders using 7zip to the "*filename* contents" folders:

REM Test6.bat - unzip all the zip and rar files into "*filename* contents" folders
for %%F in (*.rar *.zip) do "C:\Program Files\7-Zip\7z" x "%%F" -o"%%F contents"
pause

This produced the required folders (eg: "ASTROFC.zip contents" as described above). Now to do this along with compressing the files:

REM Test7.bat - unzip all zips and rars. Create new 7z files from the same names.
for %%F in (*.rar *.zip) do "C:\Program Files\7-Zip\7z" x "%%F" -o"%%F contents"
for %%F in (*.rar *.zip) do "C:\Program Files\7-Zip\7z" a -t7z "%%~nF".7z "%%F contents\"
pause

Now, this successfully created the right archives, with the right files (with default compression settings, which I’ll get to changing in a bit), but there is still the problem of it also adding the top level folder "filename contents" to the archive. I need to fix this! Currently it uses simply the archive "a" command’s "this folder and it’s contents" parameter.

REM Test9.bat - loop, try with no subfile directory
for %%F in (*.rar *.zip) do "C:\Program Files\7-Zip\7z" x "%%F" -o"%%F contents"
	
for %%F in (*.rar *.zip) do (
CD "%%F contents"
"C:\Program Files\7-Zip\7z" a -t7z "../%%~nF.7z" *
CD ..
)
	
pause

I learned how to do multiple things in a do command – ( ) seems to work. Silly how I didn’t find that until some random page on call, which I could have used I guess, showed me.

With that, I can start to merge some bits. But first, I need to delete both the original zip file, and the "filename contents" folder. To do this, using DEL and RMDIR. See this code for notes, and the links for more.

REM Test10.bat - del and deltree to remove the archive
	
REM /F is "Force if read only" and /Q is "Quiet" - no confirmation
DEL testarchive.zip /F /Q
REM /S is also "Subdirectories" and /Q is "Quiet" - no confirmation
RMDIR testarchive /S /Q
pause

So now I can use this, pretty easily. Putting this all together we have:

REM Test11.bat - putting it all together.
for %%F in (*.rar *.zip) do (
REM Extract...
"C:\Program Files\7-Zip\7z" x "%%F" -o"%%F contents"
REM Change directory, create 7zip of contents of directory...
CD "%%F contents"
"C:\Program Files\7-Zip\7z" a -t7z "../%%~nF.7z" *
CD ..
REM Delete the old files
DEL "%%F" /F /Q
RMDIR "%%F contents" /S /Q
)
pause

It works! It creates identically named files but 7zip archives, using the standard routines. Then deletes the old files and the temp directory. At first, it di