views:

284

answers:

1

This is related to my earlier question.

ren "C:\Temp\%%A" "%%A"
if errorlevel 0 (
          "C:\Program Files\7-Zip\cmdline\7za.exe" a -tzip -mx9 "C:\temp\Zip\%%A.zip" "C:\temp\%%A"
           Move "C:\temp\%%A" "C:\Temp\Archive"
                )

In the above, the IF evaluates to true always, even if REN command fails.

The idea is to check if a file is not locked by any other application, if not then Archive it and move it elsewhere.

How best to do this?

Thank you.

+2  A: 

Type help if on the command line to get some information on the errorlevel handling.

The problem with your code is, that the expression IF ERRORLEVEL N is evaluated to true for any number equal to or greater than N

Usually only ERRORLEVEL 0 indicates success, any other (greater) value is a sign of some error. To simply check, if nor error occurred, reverse your check to:

IF NOT ERRORLEVEL 1 (
   REM your code here
)

or as an alternative, exit the script:

IF ERRORLEVEL 1 EXIT /B
Frank Bollack