Batch File – Zip Files Individually Using 7-Zip

7-zipbatch

So I'm trying to use 7zip in a batch file to take the files in a folder and zip them as individual files. So the first file gets it's own zip archive, and the next and so on. I've been looking on the internet and tried a bunch of different codes. So far I've only successfully managed to zip them as a single zip file and it output to my desktop.

Unfortunately I lost the code that did that, and now I'm stuck with this which doesn't seem to do anything.

@echo on
cd "C:\Users\MVD21\Desktop\test"
FOR %%i IN (C:\Users\MVD21\Desktop\test) DO 7z.exe a "%~ni.7z" "%i"
pause

I want each file to have it's own zip archive, and for the zip archive to share the name of the file, minus the extension of course.

Any help is greatly appreciated.

Best Answer

I want each file to have it's own zip archive, and for the zip archive to share the name of the file, minus the extension of course.

Batch Script

(Set the source and destination full explicit folder paths up top and then just run as needed. The source is where the files exist you want to zip and the destination is where you want those zipped to.)

@ECHO ON

SET SourceDir=C:\folder\source
SET DestDir=C:\folder\destination

CD /D "C:\Program Files\7-Zip"
FOR /F "TOKENS=*" %%F IN ('DIR /B /A-D "%SourceDir%"') DO (
    7z.exe a "%DestDir%\%%~NF.zip" "%SourceDir%\%%~NXF"
)
EXIT

Command Line

FOR /F "TOKENS=*" %F IN ('DIR /B /A-D "C:\Folder\Source"') DO 7z.exe a "C:\Folder\Dest\%~NF.zip" "C:\Folder\Source\%~NXF"

Further Resources

Related Question