Batch file to compress subdirectories

Viewed 23274

I am trying to write a batch script that will run automatically compressing subdirectories using winrar or 7-zip:

Example:

 My Pictures
    Pics1 (Pics1.zip)
        File1.jpg
        File2.jpg
        File3.jpg
    Pics2 (Pics2.zip)
        File4.jpg
        File5.jpg
    Pics3 (Pics3.zip)
        File6.jpg
        File7.jpg
    ...

How do i write script.

5 Answers

Non of the provided solutions worked for me. I have created a script which packs first into 7z with maximum compression and then create rar archive for storage with recovery record (it also deletes the interim 7z archive):

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /R f:\ZA\SQL2005\ZA\ %%A IN (*.*) DO (
    SET "file_without_suffix=%%~dpnA"

    7z.exe a -t7z -m0=lzma2 -mx=9 -mfb=256 -md=1024m -ms=on "!file_without_suffix!.7z" "%%A"
    REM -ep1 do not store path in archive
    rar.exe a -m0 -rr -ep1 "!file_without_suffix!.rar" "!file_without_suffix!.7z"
    DEL /Q /F "!file_without_suffix!.7z"
    SET "file_without_suffix="
)

SETLOCAL DISABLEDELAYEDEXPANSION
Related