batch-filezip7zip

Zipping files with 7z.exe


I have a folder that contains a batch file named zip.bat with the following code inside:

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

The issue that i'm having with this bat is that it zips any folder inside the folder that contains the zip.bat but what I really want it to do is to zip text files not folders.

Can anyone guide me on what i'm doing wrong?

Thank you.


Solution

  • Your question is not very clear to me.

    If you want to have each folder containing txt-files in a separate zip-file you can use:

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

    If you want a single zip-file named containing all txt-files (with folder structure) you can use:

    "C:\Program Files (x86)\7-Zip\7z.exe" a -r txtfiles-%date%.zip "*.txt"
    

    If you want a single zip-file named containing txt-files of the current folder where your cmd/batch file is located you can use:

    "C:\Program Files (x86)\7-Zip\7z.exe" a txtfiles-%date%.zip "*.txt"