Menu

7-Zip Batch Script

Help
Jay
2015-06-25
2015-06-26
  • Jay

    Jay - 2015-06-25

    I have installed 7-zip w/64-bit version for Windows 2012 R2. I currently writing a batch job to do do some zipping, basically do some clean up space once a week. I got it done, which I have listed the run commands below. I am not a developer or claim to be one, so the codes are not that sophisticated.. So please don't laugh at my coding . Please review my syntax and let me know if I can do a better and cleaner batch script than the one that I presented here.

    Thank you in advance.

    Jay

    @echo off

    REM set hr=%TIME:~0,2%
    REM if %hr% lss 10 SET hr=0%hr:~1,1%
    set TODAY=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%

    ECHO.
    ECHO * Zipping all the files in C:\Temp\Archived and moving it to C:\Temp\Zipped
    CD C:\Temp\Archived
    ECHO.
    7z.exe a -tzip "C:\Temp\Zipped\MyZip_%TODAY%.zip" "C:\Temp\Archived*." -mx5
    CD ..
    ECHO.
    ECHO Delete the files in original folder
    Del C:\Temp\Archived*.
    /s /q
    RD /S /Q C:\Temp\Archived\

    MD C:\Temp\Archived

    @pause

     

    Last edit: Jay 2015-06-25
  • Shell

    Shell - 2015-06-26

    .zip extension selects -tzip by default, -mx5 is also the default. You may, however, include those switches to make the code more evident. The del /s /q command is almost equivalent to rd-md pair, so you may omit last 2 lines before pause:

    cd C:\Temp
    7z a ..\Zipped\MyZip_%TODAY%.zip Archived
    if errorlevel 1 (
      echo Zipping failed with code %ERRORLEVEL%
      exit /b
    )
    echo Deleting the files in original folder
    del /s /q Archived
    

    Also, if you have 7-Zip 9.30 or newer, you may use the -sdel switch instead of del (however, you may also need md).

     

Log in to post a comment.