【问题标题】:Batch script to create a ZIP file out of a list of stated files and folders从声明的文件和文件夹列表中创建 ZIP 文件的批处理脚本
【发布时间】:2015-03-25 11:45:24
【问题描述】:

我有以下文件树

|
|--/BlueFolder1/
|----Blue.txt
|----Blue2.exe
|--/BlueFolder2/
|----Blue.exe
|-Blue2.exe
|-Red.md
|-Blue.txt
|-MySuperAwesomeScript.bat

我需要制作一个批处理脚本来将所有“蓝色”文件压缩到 BlueFiles.zip 存档中。

我对这些技术真的一无所知,但我想我应该列出我想要压缩的文件,或者如果可能的话,声明哪些文件要 IGNORE。

我发现这个批处理脚本就像一个魅力,但它不允许我选择哪些文件不/包含。

@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

提前致谢。

【问题讨论】:

    标签: windows batch-file zip batch-processing zipfile


    【解决方案1】:

    查看this first

    尽管 Makecab 使用了 deflate 算法,但最终的格式不是 zip,而是 cab。 MaxDiskSize=0 将大小设置为其默认值(即软盘) 为了保留目录结构,您需要在每次目录更改后设置DestinationDir,因此您需要额外的指令文件。在上面的链接中,有一个可以使用的脚本,可以创建一个目录。您将排除非蓝色文件你可以编辑指令文件。检查MAKECAB信息。

    这应该只将蓝色文件添加到指令中。

    ;@echo off
    
    ;;;;; rem start of the batch part  ;;;;;
    ; if "%~2" EQU "" (
    ;   echo invalid arguments.For help use:
    ;   echo %~nx0 /h
    ;)
    ;for %%a in (/h /help -h -help) do ( 
    ;   if "%~1" equ "%%~a" (
    ;       echo compressing directory to cab file  
    ;       echo %~nx0 directory cabfile
    ;       echo to uncompress use:
    ;       echo EXPAND cabfile -F:* .
    ;   )
    ; )
    ;
    ; set "dir_to_cab=%~f1"
    ;
    ; set "path_to_dir=%~pn1"
    ; set "dir_name=%~n1" 
    ; set "drive_of_dir=%~d1"
    ; set "cab_file=%~2"
    ;
    ; if not exist %dir_to_cab%\ (
    ;   echo no valid directory passed
    ;   exit /b 1
    ;)
    
    ;
    ;break>"%tmp%\makecab.dir.ddf"
    ;
    ;setlocal enableDelayedExpansion
    ;for /d /r "%dir_to_cab%" %%a in (*) do (
    ;   
    ;   set "_dir=%%~pna"
    ;   set "destdir=%dir_name%!_dir:%path_to_dir%=!"
    ;   (echo(.Set DestinationDir=!destdir!>>"%tmp%\makecab.dir.ddf")
    ;   for %%# in ("%%a\*") do (
    ;       (echo("%%~s#"  /inf=no>>"%tmp%\makecab.dir.ddf")
    ;   )
    ;)
    ;(echo(.Set DestinationDir=!dir_name!>>"%tmp%\makecab.dir.ddf")
    ;   for %%# in ("%~f1\*blue*") do (
    ;       
    ;       (echo("%%~s#"  /inf=no>>"%tmp%\makecab.dir.ddf")
    ;   )
    
    ;makecab /F "%~f0" /f "%tmp%\makecab.dir.ddf" /d DiskDirectory1=%cd% /d CabinetNameTemplate=%cab_file%.cab
    ;del /q /f "%tmp%\makecab.dir.ddf"
    ;exit /b %errorlevel%
    
    ;;
    ;;;; rem end of the batch part ;;;;;
    
    ;;;; directives part ;;;;;
    ;;
    .New Cabinet
    .set GenerateInf=OFF
    .Set Cabinet=ON
    .Set Compress=ON
    .Set UniqueFiles=ON
    .Set MaxDiskSize=1215751680;
    
    .set RptFileName=nul
    .set InfFileName=nul
    
    .set MaxErrors=1
    ;;
    ;;;; end of directives part ;;;;;
    

    但如果您想要一个 zip 格式,请检查 shell.application 解决方案,该解决方案将允许将项目添加到已创建的 zip 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多