0

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.

FMFF
  • 1,652
  • 4
  • 32
  • 62
  • You should accept [Frank Bollack's answer](http://stackoverflow.com/questions/2593133/batch-file-ren-commands-errorlevel-returns-0-even-on-failure/2601154#2601154) instead of the currently accepted [answer of rossmcm, which is WRONG](http://stackoverflow.com/questions/2593133/batch-file-ren-commands-errorlevel-returns-0-even-on-failure/6969940#6969940). (See my comment under that.) – Sk8erPeter Oct 10 '13 at 21:24

3 Answers3

5

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
  • 24,478
  • 5
  • 49
  • 58
-1

you can also make a rem.bat that'll make the error level calling something like this IF ERRORLEVEL==300 call rem.bat or you can just make every error level unlockable by using a level of 0. you can varrie on things not only will it make the app run more smoothy but itll creat no lagging in the way ur fan speed will stay the same as the errorlevel will use more cpu usage.

thomas
  • 1
-1

REN is an internal command and does not set ERRORLEVEL (I'm looking for the same answer here)

Community
  • 1
  • 1
rossmcm
  • 5,493
  • 10
  • 55
  • 118
  • 3
    In which environment? I tested it under Windows 7 (x64), and it DOES WORK: an unsuccessful `ren` command sets the `ERRORLEVEL` to `1`. Here is a really simple code: http://pastebin.com/z1EBZ2Em. I created 2 test files, renaming them leads to either "Access Denied" or "The system cannot find the file specified." error, both of them DO set the `ERRORLEVEL` to `1`. Look at this screenshot (for the first time, `asdsad.txt` could be successfully renamed, setting ERRORLEVEL to 0): http://i.imgur.com/BQb2Ydg.png. So where did you test it? `REN` should set `ERRORLEVEL` to 1 in case of a failure. – Sk8erPeter Oct 09 '13 at 12:33
  • @Sk8erPeter I tested your batch code under XPSP3 and yes, it seems to work fine. I honestly don't know what I was basing my comments on. I hay have been working in a trashed environment. – rossmcm Oct 10 '13 at 19:30
  • I really don't understand why @FMFF accepted this answer... :D – Sk8erPeter Oct 10 '13 at 21:03