Compress folders by 7z

@ECHO OFF
ECHO.
ECHO Folder Compress and Organization
ECHO version: 0.1	2011-11-20

:: Number latest folders to be reserved.
SET RESERVED_COUNT=4

SETLOCAL ENABLEDELAYEDEXPANSION
SET /A COUNT=0
FOR /F %%i IN ('DIR /B /A:D /O:-D') DO (
    SET /A COUNT=COUNT+1
    IF !COUNT! GTR %RESERVED_COUNT% (
        ECHO Delete: %%i
        RMDIR /S %%i
        ECHO.
    ) ELSE (
        ECHO.
        ECHO ^<^<^< Compressing %%i
        "C:\Program Files\7-Zip\7z.exe" a %%i.7z .\%%i\*
        IF NOT "%ERRORLEVEL%" == "0" (
            CALL:reportError %%i
            GOTO:EOF
        )
    )
)
ENDLOCAL

ECHO.
ECHO Complete Success.
GOTO:EOF

:reportError
ECHO.
ECHO Error occur when compressing %~1
GOTO:EOF

保存此脚本为pack.bat,设定需要保留的最新目录数(RESERVED_COUNT),放在需要压缩的文件夹目录下。

2 thoughts on “Compress folders by 7z”

Comments are closed.