How to zip files separately (each file) in a folder using 7-zip?

5,255

Below is what you need to do to complete a loop to zip each file to its own zip file with a naming convention of <orignalfilename>.<extension>.zip, and then delete the original if the zip of the previous file is successful. You can change %%~NXA.zip to %%~NA.zip to not include the original file name extension in the new zip file if needed.

You should also ensure that the destination directory is created if it does not already exist so the archive commands work as expected in the loop, so I added that to the script logic, and I also set the source and destination directories as variables up top.

I also used batch substitutions via the FOR loop to take care of the individual zip and file names. Look over the Further Resources from some learning material on this topic (FOR /?).


Script Example

@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%

SET SrcDir=C:\zipush
SET DestDir=C:\new-drive-%TODAY%
IF NOT EXIST "%DestDir%" MD "%DestDir%"

ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new and then delete from C:\zipush
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE

Further Resources

  • FOR
  • Conditional Execution
  • MD

  • FOR /? from command prompt

    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:
    
    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    
Share:
5,255

Related videos on Youtube

Sophie Kobzantsev
Author by

Sophie Kobzantsev

Updated on September 18, 2022

Comments

  • Sophie Kobzantsev
    Sophie Kobzantsev over 1 year

    I'm using 7-zip in Windows 7 and trying to create a batch for an automated zip file in a folder.

    I want each one of the files in the folder to be zipped separately.

    Can you look in my code and suggest on how to do it best?

    I succeeded on zipping it all from one folder to another but need also to do separately each file:

    @ECHO OFF
    SET hr=%time:~0,2%
    IF %hr% lss 10 SET hr=0%hr:~1,1%
    
    Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
    
    ECHO.
    
    ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
    ECHO.
    7za a -tzip "C:\new-drive-%TODAY%.zip" "C:\zipush*" -mx5
    ECHO.
    
    ECHO Delete the files in orginal folder 
    DEL "C:\zipush\*.*"
    PAUSE
    
  • Sophie Kobzantsev
    Sophie Kobzantsev over 7 years
    Thank you so much it worked perfectly! I'll test on now on one of the servers and create a scheduled task for it.