Archiving Folders With 7ZIP

Recently I was carrying out a bit of house keeping on my work laptop’s hard drive. I have a folder in Windows that contains daily folders of temp files and every so often I compress them and archive them off to free up some space. Usually it just takes me a few minutes to run through the folders and compress them into archive files. Unfortunately over the past year I’d not gotten around to doing it so I had rather a lot of folders to archive and I decided to quickly put together a little script to help me.

DOS batch files may be rather “old school”, but I frequently find that a set of command line instructions is actually still a great way to quickly carry out a set of straight forward file operations. So my solution script is a BAT file that makes use of the command line interface to the 7ZIP compression utility.

The script is passed one or more folders (e.g. by drag & drop, or by adding a shortcut to the script to your send to menu and using that) to compress and each one is compressed into a separate 7Z archive (an alternative to a ZIP file which tends to have better compression). This generation of one folder (and any files and folders it contains) being compressed into its own archive file is a key factor for me as I didn’t want to create a single huge “snapshot” archive, but to be able to archive each individual day folder to its own archive. The archive files appear in the parent folder of the folders being passed in to the script.

The script is shown below and includes numerous comment lines to help you understand what it is doing. From a high level it works its way through the list of folders it is passed one by one. For each folder path it derives the folder name and then from the parent folder it runs a compression command to create the archive.

The one line you may need to change is line 4 which defines the path to the 7ZIP.EXE file. You could also amend the archive command on line 28 to create the archive in ZIP file format rather than as a 7Z file.

@ECHO off

REM Set this variable to the path to the 7ZIP.EXE you wish to use.
SET PATH_7ZIP="C:\Program Files\7-Zip\7z.exe"

REM If we have no folder (/parameter) passed in, jump to the end of the file and exit the script.
REM File paths containing spaces will be automatically wrapped in double quotes.
IF [%1]==[] GOTO :eof

REM Get the folder name for the head of the list of parameters.
REM Switch to the first folder passed in.
CD /d "%1"
REM Get the folder name from the path -> this will be the base name for our zip file.
FOR /f "delims=" %%A IN ('CD') DO (
     SET CompressFolderNamed=%%~nxA

REM Output the folder name to the screen.
ECHO Compressed Folder Name=%CompressFolderNamed%
REM Switch working directory to the parent folder.
CD ..

REM Run 7Zip to compress the folder into a 7Z file in the parent folder.
REM NOTE: We need to remove any double quotes from the passed in path (2nd parameter) in order 
REM       to add a closing slash.  The quotes are then added around that new path.
%PATH_7ZIP% a "%cd%\%CompressFolderNamed%.7z" "%~1\"

REM Knock the first parameter (head) off of the parameter list.  This will leave the remaining parameters (tail) in place.

REM Return to the top of the processing loop
GOTO Processing_Loop
Author: Stephen Millard
Tags: | shell |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read