Compressing hundreds of folders into individual archive files manually can be a time-consuming task. To automate the process, I modified an existing script to meet my requirements.
I chose the 7-Zip Command Line edition because it is lightweight, consists of a single executable file, and provides excellent compression. 7-Zip can be downloaded here.
The only other file required is the script below. Place both files in the same folder and update the set exe and set root variables to match your environment before running it.
@echo off
setlocal
REM Location of 7z.exe
set exe=e:\shared\7za.exe
REM Location of root folder
set root=e:\shared\data
for /F "tokens=* usebackq" %%G in (`dir "%root%" /A:D /B`) do (
"%exe%" a -tzip "%root%\%%G.zip" "%root%\%%G" -mx1 > NUL
if exist "%root%\%%G.zip" echo rd /s /q "%root%\%%G"
)
endlocal
Untested version
@echo off
setlocal
REM Location of 7z.exe
set "exe=e:\shared\7za.exe"
REM Location of root folder
set "root=e:\shared\data"
REM Verify 7-Zip exists
if not exist "%exe%" (
echo ERROR: 7-Zip executable not found: "%exe%"
exit /b 1
)
REM Verify root folder exists
if not exist "%root%" (
echo ERROR: Root folder not found: "%root%"
exit /b 1
)
for /d %%G in ("%root%\*") do (
echo Compressing "%%~nxG"...
"%exe%" a -tzip "%%G.zip" "%%G\*" -mx1 >nul
if exist "%%G.zip" (
echo Archive created: "%%G.zip"
echo To delete source folder, run:
echo rd /s /q "%%G"
) else (
echo ERROR: Failed to create archive for "%%G"
)
)
endlocal
