1

I am creating a batch for creation of zip files from folders, I want to check if the zip file has been created succesfully. It does the logging of which zip file has been created but when it goes wrong it should goto a label and stop the operation. This does not work, the ELSE command is echoed and not executed.

SETLOCAL EnableExtensions EnableDelayedExpansion

for /d %%d in (*) do (
    "7z.exe" a -r -tzip "%%d.zip" ".\%%d\" & IF %ERRORLEVEL% EQU 0 (echo Archive "%%d.zip" created succesfully >> "Archive-log %date%.txt") ELSE (set fault="%%d.zip" goto createzip))

exit /b

:createzip
echo Failed creating archive %fault% >> "Error-log %date%.txt"
exit /b
Compo
  • 36,585
  • 5
  • 27
  • 39
Dave
  • 23
  • 1
  • 5

2 Answers2

0

I think it's because your ELSE contains 2 statements which aren't separated so the goto ends up being part of the set command. To fix that and for readability, I think you should reformat a bit:

for /d %%d in (*) do (
    "7z.exe" a -r -tzip "%%d.zip" ".\%%d\" 
    IF %ERRORLEVEL% EQU 0 (
        echo Archive "%%d.zip" created succesfully >> "Archive-log %date%.txt"
    ) ELSE (
        set fault="%%d.zip"
        goto createzip
    )
)
DSway
  • 752
  • 14
  • 33
  • `%ERRORLEVEL%` does not change in `for` loop parenthesized body as it's evaluated in parse time! You need either delayed expansion i.e. `!ERRORLEVEL!`, either perform the `for` loop body in a subroutine i.e. `call :label`, or apply `if errorlevel number (commands) else (other_commands)`. Note that `if errorlevel number` specifies a true condition if the last program run returned an exit code **equal to or greater than** the number specified. – JosefZ Oct 06 '17 at 06:19
0

Here is the batch code rewritten and extended. The batch file does not immediately stop on failing to compress a subfolder to a ZIP archive.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Get current date in region dependent format with . as delimiter.
set "FileDate=%DATE:/=.%"
set "ErrorCount=0"
set "FolderCount=0"

rem Compress each non hidden subfolder in current folder into a ZIP file.
for /D %%I in (*) do (
    set /A FolderCount+=1
    "7z.exe" a -r -tzip "%%I.zip" ".\%%I\"
    if errorlevel 1 (
        echo Failed creating archive: "%%I.zip">>"Error-log %FileDate%.txt"
        set /A ErrorCount+=1
    ) else (
        echo Archive "%%I.zip" created succesfully.>>"Archive-log %FileDate%.txt"
    )
)

rem Exit batch processing if no subfolder was found in current folder?
if %FolderCount% == 0 endlocal & exit /B

set "ErrorPluralS=s"
if %ErrorCount% == 1 set "ErrorPluralS="
set "FolderPluralS=s"
if %FolderCount% == 1 set "FolderPluralS="
echo Processed %FolderCount% folder%FolderPluralS% with %ErrorCount% error%ErrorPluralS%.>>"Archive-log %FileDate%.txt"
endlocal

if errorlevel 1 working also without usage of delayed expansion means:
IF the exit code of the previous command or executable is greater or equal 1 THEN ...
This is explained in the Microsoft support article Testing for a Specific Error Level in Batch Files and working from MS-DOS to Windows 10 in batch files and on command line.

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.

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

Read also the answers on:

Mofi
  • 46,139
  • 17
  • 80
  • 143