Batch file script to zip files

后端 未结 7 1430
夕颜
夕颜 2020-12-09 03:22

i have a folder structure in this pattern. I\'ve just shown two sub directories and 2 files in each but generally I have n number of subdirectories at a single level and sin

相关标签:
7条回答
  • 2020-12-09 04:28

    This is link by Tomas has a well written script to zip contents of a folder.

    To make it work just copy the script into a batch file and execute it by specifying the folder to be zipped(source).

    No need to mention destination directory as it is defaulted in the script to Desktop ("%USERPROFILE%\Desktop")

    Copying the script here, just incase the web-link is down:

    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    SET sourceDirPath=%1
    IF [%2] EQU [] (
      SET destinationDirPath="%USERPROFILE%\Desktop"
    ) ELSE (
      SET destinationDirPath="%2"
    )
    IF [%3] EQU [] (
      SET destinationFileName="%~n1%.zip"
    ) ELSE (
      SET destinationFileName="%3"
    )
    SET tempFilePath=%TEMP%\FilesToZip.txt
    TYPE NUL > %tempFilePath%
    
    FOR /F "DELIMS=*" %%i IN ('DIR /B /S /A-D "%sourceDirPath%"') DO (
      SET filePath=%%i
      SET dirPath=%%~dpi
      SET dirPath=!dirPath:~0,-1!
      SET dirPath=!dirPath:%sourceDirPath%=!
      SET dirPath=!dirPath:%sourceDirPath%=!
      ECHO .SET DestinationDir=!dirPath! >> %tempFilePath%
      ECHO "!filePath!" >> %tempFilePath%
    )
    
    MAKECAB /D MaxDiskSize=0 /D CompressionType=MSZIP /D Cabinet=ON /D Compress=ON /D UniqueFiles=OFF /D DiskDirectoryTemplate=%destinationDirPath% /D CabinetNameTemplate=%destinationFileName%  /F %tempFilePath% > NUL 2>&1
    
    DEL setup.inf > NUL 2>&1
    DEL setup.rpt > NUL 2>&1
    DEL %tempFilePath% > NUL 2>&1
    
    0 讨论(0)
提交回复
热议问题