Using a batch file to zip files as individual zip files

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

  • FOR /F
  • 7Zip - Command Line Commands

Your original script does the following, line by line:

@echo on

Tells the command prompt to print back each command in the batch file before executing them. This is just so you can "see" what it's really doing before it does it.

cd "C:\Users\MVD21\Desktop\test"

Changes Directory into what I hope is the path where you want to zip up all the files.

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

The parameter to the IN here is (C:\Users\MVD21\Desktop\test). This will cause the loop "body" (the action behind the keyword DO) to get executed exactly once, with the %i parameter assigned the value C:\Users\MVD21\Desktop\test. It's no surprise that this doesn't do what you want! It looks like this would zip the entire directory into a single .7z file.

If it does nothing, it's probably because it can't find the path of 7z.exe. If that file isn't in the current directory (that test directory mentioned above), then it'll have to be on the PATH (in the %PATH% environment variable; try echo %PATH% in a shell to see what it's set to currently). Otherwise you have to copy 7z.exe to the current directory or specify an absolute path to where it is located (such as, for instance, "C:\Program Files\7-zip\7z.exe" ...).

pause

When the script is finished, this keyword just tells the shell to wait for you to press enter or close the command prompt instead of exiting it immediately.


When you're building a batch script (or, indeed, anything else that deals with code), you should stop treating the whole script as a monolithic copy-paste that you don't (attempt to) understand, and instead break down your problem into sub-parts that you can figure out individually, then hook them together. This will enable you to solve problems independently and create solutions instead of asking someone else to do all your work for you. I highly recommend it.

In your case, you have the following sub-components of your problem:

  1. Iterate over (loop through) each file contained within a given directory.
  2. For each given file, put it in a .7z file by invoking the 7z utility.

The chances of someone having an answer out there already that solves your specific need is less than the chance of being able to find the more general pieces individually.

This question, 'How to do something to each file in a directory in a batch script' solves half your problem.

The thrust of the answer is that you just start with this part from that StackOverflow answer:

for /f "usebackq delims=|" %%f in ('dir /b "c:\program files"') do echo %%f

and change the part where it's doing something (the echo %%f) to do what you want to do. I know that you need to change that part, because I've analyzed the text of the script and understood what each character and parameter means, so I can reason about where I want to insert my own code.

Here's some "air code" (untested!):

for /f "usebackq delims=|" %%f in ('dir /b "C:\Users\MVD21\Desktop\test"') do 7z.exe a "%~ni.7z" "%i"

The @echo on and pause statements above are good practice, so you can leave them in. And you'll probably want to keep that cd statement too, if your files are located in that path.

So what does that leave us?

@echo on
cd "C:\Users\MVD21\Desktop\test"
for /f "usebackq delims=|" %%f in ('dir /b "C:\Users\MVD21\Desktop\test"') do 7z.exe a "%~nf.7z" "%f"
pause

If you reply with "It doesn't work!", I WILL NOT help you. If you try this and it doesn't work, you MUST tell me in the comments the following information:

  • What exact error message do you get in the command prompt when you run these commands or put them in a batch file and run them? Don't abbreviate; copy and paste the output directly (put it on http://pastebin.com if it's a lot).
  • What do you think the error message is telling you that the problem is?
  • Where is your 7-zip installation located (the file path)?
  • Is the file location of your 7-zip installation included within the contents of the PATH environment variable? (check this, as mentioned above, with echo %PATH% in a cmd prompt.)

Tags:

Batch

7 Zip