2
votes

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.

1

1 Answers

4
votes

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
  )