You are not logged in.
Pages: 1
I'm trying to understand what the errorlevel is.
i've been reading the articles of errorlevel cmd. i still don't get it.
can somebody explain to me what actually errorlevel is ? and what is the usage of it ?
appreciate so much in advanced.
Detour in life always surprise people.
Offline
Errorlevel is just a system variable that contains status codes. When a program or command is executed, the developer of that program or command can set the status code of their program on exit. In general if a program completed successfully, the "return"/"status"/"error" code = 0. Else if there was a problem, the program will generally return a numeric value for which the program in question associates with a particular issue.
Note that this value is all up to the developer of the application in question. Some developers do not implement these values and therefore Errorlevel is not updated.
In general ErrorLevel will be updated with the return/status/error value of the last application or command executed in command.exe
Let me know if you need any deeper explanations.
Offline
Thanks for the reply in such a short notice.
Please let me explain to you what i understand from your explanation above, to see if any discrepancy from yours.
So, let say i have a command code of copying file form one directory to another,
so if the copying is success, errorlevel=0,
and if the copying is not successful due to same file has existed in the destination directory, errorlevel=1,
and if the copying is not successful due to the destination directory isn't exist, errorlevel=2 ?
Is this something like this ? i can set the numerical value associates with the particular problem ?
but how will the batch file show me the particular code that particularly associate with the particular problem ?
can i ask for your help, how to implement errorlevel into batch file if i wanna do copying (for example) ?
Detour in life always surprise people.
Offline
An errorlevel of 0 means success, and any other number is up to the person who programmed the command that returned an exit code. For native windows commands, the codes are generally just 0=success, 1=not success.
You can set the errorlevel yourself by having the command you want to run in a separate script and then using the exit /b command followed by what you want to set the errorlevel to.
new_copy.bat
@echo off
setlocal enabledelayedexpansion
:: Gets the parameters from user input
set source=%1
set target=%2
:: Note that there is no rationale for any of the exit codes beyond "that's what I decided they should be."
if "%1"=="" exit /b 255
if "%2"=="" exit /b 255
if not exist %1 exit /b 10
if not exist %2 exit /b 20
copy %1 %2
exit /b %errorlevel%
demo.bat
@echo off
set root_dir=C:\errorcode_copy
set source_path=%root_dir%\source
set target_path=%root_dir%\target
:: Calls the new copying script incorrectly
call new_copy.bat %source_path%
echo %errorlevel%
:: Calls the new copying script with an invalid source but a valid target
call new_copy.bat %source_path%\*.badextension %target_path%
echo %errorlevel%
:: Calls the new copying script with a valid source but an invalid target
call new_copy.bat %source_path%\*.txt %root_dir%\fake_dir
echo %errorlevel%
:: Calls the new copying script with a valid source and valid target
call new_copy.bat %source_path%\*.txt %target_path%
echo %errorlevel%
Offline
Hi, Shadow Thief.
Wow, it is long explanation. I really appreciate your explanation by the way.
I guess, to truly understand and go through your explanation, it's gonna occupy me some time.
Once again, thanks for the explanation, with demo bat file even...xD
Detour in life always surprise people.
Offline
Pages: 1