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.
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.