3

I want to zip everything in a folder, EVERYTHING, but into individually named archives. For some reason every solution on the internet only zips folders, or fails to work at all.

Currently, I have

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"

Which I interpret to mean

  • for = initiate a loop
  • /d = I don't know what this means
  • %%X = I don't know what this means
  • in = not sure, I think it means current directory
  • (*) = I don't know what this means
  • do = execute the next thing in "..."
  • "C:\Program Files\7-Zip\7z.exe" = the thing I want done.
  • a = add to archive
  • -m9 = max compression
  • "%%X.zip" = make it a zip file, though I still don't know what %%X is.
  • "%%X\" = even if I knew what "%%X\" meant I don't know why it's here.

I have figured out replacing %%X gives the archive a name, so it seems to copy the name of the thing being targeted.

So if I guess, I think /d is "target folders" and %%X is the name.

So

for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"

Says in English as I understand it: for every folder name in the current directory, use 7z to max compress it into a zip of the same name... except I don't know what to replace /d with to make it target files instead of folders. And targeting specific extensions would be even better.

I tried googling what does "/d in cmd mean," "what does %%X mean", etc. I don't seem to be getting correct results in the search, I think I'm being too vague for google.

UPDATE:

for %%i in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%i.zip" "%%i\*.*"

seems to make a zip file NAMED each item in the folder, but does not actually add any files to them. I tried adding /f, but it didn't work at all when I did that.

Additionally, the first time I posted this it was closed as a duplicate of Batch script loop which has almost nothing to do with my problem. Yes, I have a loop, yes that addresses batch loops, but no, it does not come close to solving my problem since my problem isn't with the loop itself., or if it does I have absolutely no idea why or how. So please, explain it to me. I did see the section where it says %%X is the variable, but that just means I suppose X could be anything I want it to be, and since in my update I indicated a secondary issue, I think the problem I'm having is with 7z and not the bat file.

Durry42
  • 401
  • 3
  • 10
Elie Platt
  • 39
  • 3
  • 2
    Type `for /?` into a command prompt window and read the output text... – aschipfl Mar 11 '20 at 12:07
  • 1
    Or type `help for` and hit enter at that same command prompt. Either of them will work to get you information on `for` and it's use. – Ken White Mar 11 '20 at 12:35

1 Answers1

0

The for command can be very confusing at first, however there is many videos and sites that show examples of how to use it. To start with try for /? to see the built in help information.

The below code should be what you are after if i understand your requirements correctly.. if not then this should help you understand the principles and adjust for your needs.

for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (
    echo Processing File: C:\Test Folder\%%a
    "C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"
    echo.
)
pause

Now lets go through it line by line...

for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (

/F Tells it to work with files instead of directories(/D)

in (' ') Contains the command that finds the filenames. Note the ' marks at each end of the command...they are important.

%%a Represents each value of a filename that the in() command passes it.

do ( ) What it should do with each %%a value it receives.


Now we get to the command that is finding the filenames and passing to the do () section as %%a

('DIR /A-D /B /ON "C:\Test Folder\"')

You can find more info on the this by running dir /?

/A-D Tells it to ignore Directories... only list files.

/B Output just the file names and not anything else like sizes or it will confuse the for command.

/ON Feed the filenames to the do() command in alphabetical order

"C:\Test Folder\" The folder that contains all the files we want to list/perform actions on. Make sure you include the trailing slash. Also use the "" marks or it wont work if a folder has a space in its name.


Write(echo) to the screen a message showing which file it is about to do.

echo Processing File: C:\Test Folder\%%a

You can find more information on how to use the 7zip command line functions at https://sevenzip.osdn.jp/chm/cmdline/syntax.htm

"C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"

"C:\Program Files\7-Zip\7z.exe" Run this program.... and pass it the following information (arguments/switches)

a Add files rather than extract, etc.

-mx1 Use the fastest compression method...but less space efficient.

-tzip Use the ZIP compression format

"C:\Test Folder\%%a.zip" Save the new "zip" file as this. (%%a will be replaced with the name of the current file... aka ChuckNorris.mpg which would end up as ChuckNorris.mpg.zip)

"C:\Test Folder\%%a" This is the actual file you want to zip.

Durry42
  • 401
  • 3
  • 10