Create multiple archives from a directory without the directory root name being added to the archive

Solution 1:

You simply need to use just a for loop omitting the /d parameter and it will work just as you describe creating the zip archive file(s) and not contain its parent folder.

Commands

Note: This will add a zip archive file for each file in the directory with only that file in the zip.

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

Note: This will add all files only in the directory to the one zip file you specify.

for %%X in (*) do "C:\Program Files\7-Zip\7z.exe" a "<MyZipFileName>.zip" "%%~X"

Nest Loop Command

Note: This will add files only from the directories beneath the directory which the batch file resides to a zip file matching the name of the directory.

@ECHO ON

FOR /F "TOKENS=*" %%A in ('DIR /S /B /AD "*"') DO (
  FOR %%B IN (*) DO (
      "C:\Program Files\7-Zip\7z.exe" a "%%~fA.zip" "%%~fA\*")
)
EXIT

Further Resources

  • For /F
  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
    
  • Dir
  • For
  • a (Add) command