7-Zip Batch File to Create Seperate ZIP Files

Here is a batch file to use 7-Zip to create seperate ZIP files from a large list of files in a directory. I found this useful for zipping indivdual ROM files for emulators as 7-Zip doesn't have this functionality built in.

Requirements - 7-Zip (download). I've used the 64bit version for this so if you use the 32bit version or have installed 7-Zip into a different directory then replace c:\Program Files\7-Zip\ with the path where 7z.exe is installed.

Open notepad (or similar) and paste in the following code:

@ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "zip.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)


Then save the file as "zip.bat". Put zip.bat into the directory with the files you want to individually ZIP and double click on zip.bat to run.

Breakdown of the Batch file is as follows:

@ECHO OFF
Hides echo of the batch commands, @ before ECHO means don't output OFF as well

FOR %%i IN (*.*) DO (
For each file in directory (*.*) DO the following command(s) -> %i loaded with the filename

ECHO "%%i" | FIND /I "zip.bat" 1>NUL) || (
If filename (%i) is zip.bat do nothing, else

"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
Run 7z.exe for each filename. Options - a=archive, -tzip=create ZIP archive
%~ni=take filename minus extension -> e.g. for a file "rom.bin" the command will be
c:\Program Files\7-Zip\7z.exe a -tzip rom.zip rom.bin

if %ERRORLEVEL% == 0 del "%%i"
If ZIP successful then delete original file, remove this line if you don't want to delete the originals

Additional % used in filename variables %%i & %%~ni is required for the batch file only and would be omitted if this was run from the command line