11

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.

1
  • Also if there's an easier way to do this using the builtin zip feature on Windows 10 that would be fine also. Mar 13, 2017 at 23:57

3 Answers 3

6

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

0
2

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.)
3
  • I don't think this is working because I have 7zip installed on another drive? It seems to work if I do cd "Location of 7zip" as in the original answer. Of course that is only working "sometimes". It was working until I put another echo statement in under it and now it isn't. And yes I'm trying to understand more about batch files. I've been trying to find a glossary of batch commands or what not to study so I actually understand it. Trust me I'd rather understand AND have it work than just have it work. Mar 16, 2017 at 1:58
  • I tried your solution and I can't tell you what the output is because the batch file doesn't run at all. It opens briefly with the cmd window just flashing and then closes again. Yes I have the pause in there. I have 7zip installed on both my local drive and on a secondary internal drive, but it does not appear to be part of the path environment Mar 16, 2017 at 10:38
  • I'd really like to understand but searching google isn't helping much. For instance, I have no idea what that " 'dir /b " command or parameter is or what it's doing. I know dir obviously is a directory but I don't know what the /b. What I'd like is for someone to explain what the parameters are doing so I can better understand. Or if someone has a link to a site where they go over this kind of thing that would be helpful as well. I googled 'dir /b in a batch file and couldn't really find anything. I think the /b has something do with the file names but I am unsure. Mar 16, 2017 at 11:04
-1

this will actually work if you save as bat file it will find all files *.txt in the SourceDir and then zip them into DestDir and then delete them from the sourcedir

@ECHO ON

SET SourceDir=C:\Logs SET DestDir=C:\LogsZipped

CD C:\LogsZipped

FOR /f %%i IN ('dir /b %SourceDir% \ *.txt') DO "C:\Program Files\7-Zip\7z.exe" a "%%~ni.zip" "%SourceDir% \ %%i" && DEL /Q /F "%SourceDir% \ %%i"

EXIT

1
  • It is discouraged to answer old questions unless you have something significant to add. There is already an accepted answer 6 years ago. I recommend voluntarily deleted your answer but please continue to contribute. Jan 27 at 3:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .