-1

After doing much searching around here, I have managed to put together a batch script that uses 7zip to archive a directory of files. The issue I'm running into is that instead of putting each file into it's own zip, it's archiving the entire folder of files multiple times and simply renaming them to match each file in the folder.

for %%X in (C:\test\*.txt) do "C:\Program Files (x86)\7-Zip\7z.exe" a -tzip "%%X.zip" *.txt

In the code above, the idea is to look in C:\test\ for ALL txt files, and zip them up into individual zip files. It sort of does this, but instead of having multiple zip files with 1 file inside each, I have multiple zip files with EVERY text file inside of them. Each zip being identical except for the name.

Anyone have some ideas? I'm sure I'm just sticking something in the wrong place, but I've tried just about everything I can think of at this point.

1 Answers1

2

Let's break down your command:

  1. For every text file in the folder:

    for %%X in (C:\test\*.txt) do
    
  2. Create a zip file with the same name as the text file, only with .zip added:

    "C:\Program Files (x86)\7-Zip\7z.exe" a -tzip "%%X.zip"
    
  3. And add all the files in the folder:

    *.zip
    

Point 3 there is clearly wrong. You want to add the single file that warranted the zip file instead:

"%%X"

The full command should thus look like this:

for %%X in (C:\test\*.txt) do "C:\Program Files (x86)\7-Zip\7z.exe" a -tzip "%%X.zip" "%%X"
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825