Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ren command sets the ERRORLEVEL to 1 in case of failure

By: Sk8erPeter on Oct 9th, 2013  |  syntax: DOS  |  size: 1.20 KB  |  views: 63  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. rem http://stackoverflow.com/questions/2593133/batch-file-ren-commands-errorlevel-returns-0-even-on-failure/6969940#6969940
  2. rem Screenshot of the results when having an "Access Denied" error and "The system cannot find the file specified." error when using 'ren' command, both cases DO set the ERRORLEVEL to "1":
  3. rem http://i.imgur.com/BQb2Ydg.png
  4.  
  5. @echo OFF
  6.  
  7. SET directory=d:\downloads\testdir
  8.  
  9. SET rename_what_filename=bla.txt
  10. SET rename_what_path=%directory%\%rename_what_filename%
  11. SET rename_to=sad.txt
  12. echo Renaming "%rename_what_filename%" to "%rename_to%" in the following directory: "%directory%"
  13. ren %rename_what_path% %rename_to%
  14.  
  15. if not %errorlevel%==0 (
  16.   echo There was a problem renaming the file.
  17. ) else (
  18.   echo Renaming was successful.
  19. )
  20.  
  21. echo Errorlevel: "%errorlevel%"
  22.  
  23. echo.
  24.  
  25. SET rename_what_filename=test.txt
  26. SET rename_what_path=%directory%\%rename_what_filename%
  27. SET rename_to=asdsad.txt
  28. echo Renaming "%rename_what_filename%" to "%rename_to%" in the following directory: "%directory%"
  29. ren %rename_what_path% %rename_to%
  30.  
  31. if not %errorlevel%==0 (
  32.   echo There was a problem renaming the file.
  33. ) else (
  34.   echo Renaming was successful.
  35. )
  36.  
  37. echo Errorlevel: "%errorlevel%"