1

I have the following files tree

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

I need to make a batch script to ZIP all the "blue" files into a BlueFiles.zip archive.

I'm really null at these technologies but I think I shall list the files I want to zip, or if possible, declare which files to IGNORE.

i've found this batch script which works like a charm, but it doesn't let me chose which files not/include.

@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

Thanks in advance.

Frondor
  • 3,466
  • 33
  • 45

1 Answers1

0

Check this first

Despite Makecab uses deflate algorithm the final format won't be zip but cab. MaxDiskSize=0 will set the size to its default (i.e. floppy disk) To preserve directory structure you 'll need to set DestinationDir after each directory change so you'll need additional directive file.In the link above there's a ready to use script that will cab a directory.You'll to exclude not-blue files you can edit the directive file.Check MAKECAB info.

this should add only blue files to the directive.

;@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 ;;;;;

But if you want a zip format check the shell.application solution which will allow to add items to already created zip.

Community
  • 1
  • 1
npocmaka
  • 55,367
  • 18
  • 148
  • 187