#1 06 Jan 2018 23:12

Dheath
Member
Registered: 06 Jan 2018
Posts: 11

Ren's Errorlevel.

Hello, I've just started learning batch scripting and this site has been hugely helpful. Thanks a lot!

I currently have a problem with ren/rename setting %errorlevel%==0 when it fails due to a duplicate filename existing. Googling suggests that some versions of Windows' ren/rename simply don't provide error codes because the command is internal, but my Windows 10 CMD does set the error level to 1 when a duplicate exists, only my batch files seem to have this problem... Does anyone have any idea what might be causing this?

Offline

#2 07 Jan 2018 15:16

Aacini
Member
Registered: 05 Dec 2012
Posts: 143

Re: Ren's Errorlevel.

Accordingly to the Table 2 at this answer, the REN command always sets the errorlevel to 1 when an error occurs. If you post your code perhaps we could review it...

Antonio

Offline

#3 07 Jan 2018 16:57

Dheath
Member
Registered: 06 Jan 2018
Posts: 11

Re: Ren's Errorlevel.

Sure, I've found that the ren error levels actually do work in batch scripts, they just don't seem to work in my for loops. Here's a fun testing script I put together for comparison. Feel free to let me know if I'm doing anything else wrong. tongue

@echo off
setlocal enabledelayedexpansion

:Menu
  set /p selection=(1) does work, (2) does not work, (3) clean up and quit: 
  if %selection%==1 goto :DoesWork
  if %selection%==2 goto :DoesNotWork
  if %selection%==3 goto :CleanUp
  exit /b 10


:DoesWork
  if not exist uselessfile echo. > uselessfile
  if not exist uselessfile1 echo. > uselessfile1
  if exist uselessfile2 del /q uselessfile2
  
  ren uselessfile uselessfile1
  echo Case duplicate exists, errorlevel==%errorlevel%
  echo.
  
  ren uselessfile uselessfile2
  echo Case duplicate does not exist, errorlevel==%errorlevel%
  echo.
  
  goto :Menu


:DoesNotWork
  if not exist uselessfile1 echo. > uselessfile1
  if not exist uselessfile1.ext echo. > uselessfile1.ext
  if not exist uselessfile2 echo. > uselessfile2
  
  for /f "delims=" %%a in ('dir /b /a-d ^| findstr /v /r ".*\..*" ') do (
    set /a counter+=1
    ren %%a %%a.ext
    if !counter! equ 1 echo Case duplicate exists, errorlevel==%errorlevel%
    if !counter! equ 2 echo Case duplicate does not exist, errorlevel==%errorlevel%
    if !counter! gtr 2 echo Case processing %%a, errorlevel==%errorlevel%
    echo.
    )
  
  goto :Menu


:CleanUp
  for /f "delims=" %%a in ('dir /b /a-d ^| findstr /r "uselessfile.*" ') do del %%a
  exit /b

Offline

#4 07 Jan 2018 19:47

Dheath
Member
Registered: 06 Jan 2018
Posts: 11

Re: Ren's Errorlevel.

Guess I probably should have just cleaned up with "del uselessfile*" huh.

Offline

#5 08 Jan 2018 05:02

Aacini
Member
Registered: 05 Dec 2012
Posts: 143

Re: Ren's Errorlevel.

In your code you started the Batch file with "setlocal enabledelayedexpansion" command and inside the FOR loop you used "!counter!" instead of "%counter%"... Why?

If your answer is "because counter variable changes its value inside the FOR loop and hence it is necessary to use Delayed Expansion, otherwise the value displayed by "%counter%" would be the same value before the loop", then...

You just need to realize that "errorlevel" is also a variable (the same than "counter")...

Antonio

Offline

#6 08 Jan 2018 22:51

Dheath
Member
Registered: 06 Jan 2018
Posts: 11

Re: Ren's Errorlevel.

Yup, that was exactly my problem. Thank you! It seems pretty stupid in retrospect but I probably never would have thought of that myself.

Offline

Board footer

Powered by FluxBB