简体   繁体   中英

7zip recursively archive files in folders

I need to archive all files but not folders that are inside a complex directory structure in a special way. Every subfolder (no matter how deep in the structure) will contain a zip file containing all files that used to be there. If I use 7zip a -r -tzip files.zip \\*.\\* this will put all the structure in a single root file (files and folders). Not good.

The solution is a batch script to recurse this structure, and for every folder found to create a zip file and delete the original files. But I only know how to make simple loops, not unlimited, and not in an unknown directory structure.

For example: for /F "tokens=1" %%u in ('dir /b /ad parentfolder') do ( only lists the first level subdirectories.

use (1) FOR command to iterate (2) over all IN (*) (3) directories /D , (4) recursively /R
and (5) for each directory found %%a , (6) apply DO (7) the 7z command to create a zip (8) named as the directory %%~na.zip (9) including all the contents of the directory %%a\\*

for /r /d %%a in (*) do (
  7z a %%a\%%~na.zip %%a\*
)

optionally , you might want to erase the directory contents except the recently created zip file, place another FOR inside the previous FOR , after the 7z

  for %%b in (*) do (
    if not %%b==%%~na.zip erase %%a\%%b
  )

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