Home > Enterprise >  Zipping using 7-zip in a batch script
Zipping using 7-zip in a batch script

Time:06-08

I'm trying to make a script in Batch to make a zip inside every folders containing files with a type of extension but the problem is that 7-zip zips anyways. Even when he finds no files with the good extension, he makes an empty zip. You have below what I already did.

setlocal enableDelayedExpansion
set /p ext="Write with the . which extension you want to zip. : "
set "currentdir="

for /f "delims=" %%b in ('dir /b /s /a-d "C:\Users\546802\Desktop\Test"') do (
 if "!currentdir!" neq "%%~dpb" ( 
  set "currentdir=%%~dpb"
  for /d %%c IN ("%%~dpb.") do "c:\Program Files\7-Zip\7z.exe" a -mx "%%~dpb%%~nxc.zip" %%c\*%ext% )
)

How can I fix that ? Does 7-zip have a command to avoid this ? I read multiple topics speaking about 7-zip commands but I didn't find a command that makes what I need.

CodePudding user response:

A batch file for this ZIP archive files creation task is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FileExtension="

rem Prompt the user in a loop until entering really a file extension.
rem Pressing just RETURN or ENTER results in prompting the user again.
rem Double quotes are always removed from input string and there must
rem be entered something else than just straight double quotes one or
rem more times. A dot at beginning of the file extension is always
rem removed and there must be entered more than just one dot. The file
rem extension entered by the user cannot contain / or \ or . or any
rem other character not allowed in a file extension according to the
rem definition by Microsoft.

:PromptUser
set /P "FileExtension=Enter the file extension to zip: " || goto PromptUser
set "FileExtension=%FileExtension:"=%"
if not defined FileExtension goto PromptUser
if "%FileExtension:~0,1%" == "." set "FileExtension=%FileExtension:~1%"
if not defined FileExtension goto PromptUser
set "FailedSyntaxCheck=1"
for /F "delims=*./:<>?\|" %%I in ("%FileExtension%") do if not "%%I" == "%FileExtension%" (goto PromptUser) else set "FailedSyntaxCheck="
if defined FailedSyntaxCheck goto PromptUser

for /F "delims=" %%I in ('dir "%USERPROFILE%\Desktop\Test" /AD-L /B /S 2^>nul') do if exist "%%I\*.%FileExtension%" "%ProgramFiles%\7-Zip\7z.exe" a -bso0 -bsp0 -mx=9 -r- -tzip -y -- "%%I\%%~nxI.zip" "%%I\*.%FileExtension%"
endlocal

The batch file is not 100% fail-safe. The file extension syntax verification is not 100%. So the user can still enter a string which is not valid for a file extension as described by Microsoft on the documentation page about Naming Files, Paths, and Namespaces.

If there is a directory with name Test.txt and the user enters .txt or just txt as file extension, the simple IF condition used in this code is true although Test.txt is a folder and not a file and therefore 7-Zip is nevertheless executed. The code could be improved to make the condition more accurate if such use cases should be handled too.

The used 7-Zip switches are described in the help of 7-Zip. Double click on the file 7-zip.chm in the program files folder of 7-Zip to open the help, click on the first tab Contents on the list item Command Line Version and read all referenced help pages about the Command Line Syntax, the Commands and the Switches.

A ZIP file is created inside a folder containing files with the specified file extension. It is possible to create the ZIP file in the parent directory of the directory containing files with the specified file extension on replacing "%%I\%%~nxI.zip" by "%%I.zip". The question does not contain a clear information with a directory tree with files before and after execution of the batch file with user input txt or .cmd to really know what are all the requirements for this ZIP archive files creation task.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • rem /?
  • set /?
  • setlocal /?
  • Related