简体   繁体   中英

How can I write batch file for archiving files being in subdirectories?

I want to write batch script which will be add to archives files which being in subdirectories. Achieve's name should be as subdirectories name.

For example, I have following structure:

file.bat
dir1
-- file1.txt
-- file2.csv
dir2
-- file3.txt
-- file4.csv

I want to run file.bat and get two files:

dir1.zip
-- file1.txt
-- file2.csv
dir2.zip
-- file3.txt
-- file4.csv

I wrote such:

FOR /D %%i IN (.\*) DO "C:\Program Files\7-Zip\7za920\7za.exe" a -tzip "%%~ni.zip" "%%i"

But I got

 dir1.zip
--dir1
--- file1.txt
--- file2.csv
--dir2.zip
--- file3.txt
--- file4.csv

It's wrong.

How I can I get right result using 7-zip?

Thanks.

Test this:

FOR /D %%i IN (*) DO "C:\Program Files\7-Zip\7za920\7za.exe" a -tzip "%%~ni.zip" "%%i\*"

This will create the ZIP files without the folder name.

@echo off
FOR /D %%i IN (*) DO (
   pushd "%%i"
      "C:\Program Files\7-Zip\7za920\7za.exe" a -tzip "..\%%~ni.zip" *
   popd
)
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM