-1

How do I make 7-Zip recursively archive all files in a folder for all parent folders?

Currently I have this:

for /d %%X in (C:\Users\mikejoh\Desktop\Modst\Ziptest\*) do "c:\Program Files\7-Zip\7z.exe" a "%%X\*.7z" "%%X\win\*"

But that only takes the parent folders and zips them in the same location...

I believe I would be able to add something like a DEL "%%X% at the end to make it delete the files. However, in this case it would delete the whole folder :(

Example: I have these folders below:

Enter image description here

These folders contain these folders:

Enter image description here

Where I need to zip all files in the Win folder:

Enter image description here

So for every folder it should make an archive inside the Win folder with all files inside the win folder. If everything goes well, it should delete the archived files.

Would something like this be possible to create in a BAT file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Metal Mike
  • 89
  • 4
  • 13
  • @Mofi i know that i can make it go into subdirectories, the hard part for "ME", is making it like a "For" loop so it goes through all these folders and only zips the content of the Win folder, while archiving the content at the same location. that is what i am struggling with, you said this could be done in WinRAR ? would you mind elaborating on that ? – Metal Mike Feb 23 '15 at 08:56

1 Answers1

1

Let's say the folder Modst\ZipTest on your desktop contains following folders and files:

  • Test1
    • Win
      • File1.txt
      • File2.txt
      • File3.txt
    • xxx.txt
  • Test2
    • Win
      • File1.txt
      • File2.txt
      • File3.txt
    • yyy.txt
  • Test3
    • Win
      • File1.txt
      • File2.txt
      • File3.txt
    • zzz.txt

And the result should be following list of files and folder with all missing files in a Win subfolder added to the *.zip file in the same subfolder.

  • Test1
    • Win
      • Test1.zip
    • xxx.txt
  • Test2
    • Win
      • Test2.zip
    • yyy.txt
  • Test3
    • Win
      • Test3.zip
    • zzz.txt

Then this can be achieved with this batch code using WinRAR:

@echo off
for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
   "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ed -ep -ibck -inul -m5 -tl -u -y "%%~A\Win\%%~nA.zip" "%%~A\Win\*"
)

The advantage is that WinRAR deletes only files which are successfully added to the archive. All files being locked while WinRAR wants to read file contents for compression remain in the folders. Don't know if this batch file is executed ever while files to compress and delete are written currently in the folders by another application.

The 7-Zip solution requires more commands in a batch file:

@echo off
for /D %%A in ("%USERPROFILE%\Desktop\Modst\ZipTest\*") do (
    "%ProgramFiles%\7-Zip\7z.exe" u -tzip -mx=9 -y "%%~A\%%~nA_tmp.zip" "%%~A\Win\*">nul
    if not errorlevel 1 (
        del /F /Q "%%~A\Win\*"
        move "%%~A\%%~nA_tmp.zip" "%%~A\Win\%%~nA.zip"
    ) else (
        if exist "%%~A\%%~nA_tmp.zip" del "%%~A\%%~nA_tmp.zip"
    )
)

There is no test made if each file is really added to the archive before it is deleted. No file is deleted if any error occurred during compression of any file in a Win subfolder with exception of the ZIP file in parent folder if the ZIP file was created at all.

NOTE: The path to WinRAR.exe or 7z.exe can be different on your computer and must be adapted in this case in the batch code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • VERY well explained, and "WinRAR deletes only files which are successfully added to the archive" that is quite important when handling huge amounts of data that could make the system crash. Your solution worked like a charm, could you explain how this ""%%~A\Win\%%~nA.zip"" works ? i see that %%A is the path location loaded into a variable, but what roles does "~" and "n" play here ? – Metal Mike Feb 24 '15 at 08:52
  • 1
    Run in a command prompt window `for /?` and you get explained `%%~A` (string of loop variable A without surrounding double quotes) and `%%~nA` (just name of file/directory without path and extension) in help of this command output in the console window page wise. Running a command or application with parameter `/?` usually outputs on Windows the help of this command/application. – Mofi Feb 24 '15 at 09:47