Hi guys,
I'm looking to loop to some function calls as well as check the error level after returning from the function call.
Right now I have this (archaic - I know - but that's why I'm here )
call :funcconfig
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :funclog
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :func1
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :funclog
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :func2
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :funclog
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :func3
@ IF %ERRORLEVEL% GTR 0 GOTO :EXIT
@ call :funclog
@GOTO :EOF
If someone has a better way to check the error level after running a function please let me know.
Also, if someone can suggest ideas on how to output a log file of the error code on the local machine and a log server that would be great!
Thanks,
CH
for loop and function calls?
Moderator: DosItHelp
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
Not knowing the specifics of what you're looking for ... you could do something like the following:
Code: Select all
@Echo Off
For /D %%F in (FuncConfig Func1 Func2 Func3) Do (
Echo Calling Function %%F
Call :%%F
If %ErrorLevel% Gtr 0 Goto Exit
Call :LogError
)
:Exit
Echo We're through playing now...
Goto:EOF
:LogError
Echo Log your error here
Goto:EOF
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Can't check %errorlevel% inside a for loop without using delayedexpansion. Use the special "errorlevel" if statement instead:
Code: Select all
@Echo Off
For /D %%F in (FuncConfig Func1 Func2 Func3) Do (
Echo Calling Function %%F
Call :%%F
If errorlevel 1 Goto Exit
Call :LogError
)
:Exit
Echo We're through playing now...
Goto:EOF
:LogError
Echo Log your error here
Goto:EOF
Here is the latest code snippet. the errorlevel is still not working right inside the for loop even with the "IF ERRORLEVEL" change.
Thoughts? Improvements?
:FUNCTIONCALLS
::======================================
For /D %%F in (FuncConfig funclog) Do (
Call :%%F
echo %errorlevel%
If errorlevel 1 goto :exit
)
@GOTO :eof
:funcconfig
::=========================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do set DATETIME=%%C%%A%%B%%D%%E%%F%
@SET TOOLSDIR=%CD%
@SET STRINGLIST=%CD%\OSLIST.TXT
@IF EXIST %STRINGLIST% (
ECHO ok
) else (
set /A ErrorLevel+=1
ECHO %STRINGLIST% is Missing!!!
)
echo %errorlevel% funcconfig
@GOTO :EOF
:funclog
::==========================================
: LOGGING
@ECHO %DATETIME%.%ERRORLEVEL%
@GOTO :EOF
Thoughts? Improvements?
:FUNCTIONCALLS
::======================================
For /D %%F in (FuncConfig funclog) Do (
Call :%%F
echo %errorlevel%
If errorlevel 1 goto :exit
)
@GOTO :eof
:funcconfig
::=========================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do set DATETIME=%%C%%A%%B%%D%%E%%F%
@SET TOOLSDIR=%CD%
@SET STRINGLIST=%CD%\OSLIST.TXT
@IF EXIST %STRINGLIST% (
ECHO ok
) else (
set /A ErrorLevel+=1
ECHO %STRINGLIST% is Missing!!!
)
echo %errorlevel% funcconfig
@GOTO :EOF
:funclog
::==========================================
: LOGGING
@ECHO %DATETIME%.%ERRORLEVEL%
@GOTO :EOF
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
OK.. why not do something like ....
Code: Select all
:FUNCTIONCALLS
::======================================
SetLocal EnableExtensions
SetLocal EnableDelayedExpansion
Set "Error=0"
For /D %%F in (FuncConfig funclog) Do (
Call :%%F
echo !Error!
If /I [!Error!] EQU [1] goto :exit
)
@GOTO :eof
:funcconfig
::=========================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do set DATETIME=%%C%%A%%B%%D%%E%%F
@SET TOOLSDIR=%CD%
@SET STRINGLIST=%CD%\OSLIST.TXT
@IF EXIST %STRINGLIST% (
ECHO ok
) else (
set /A Error=Error+1
ECHO %STRINGLIST% is Missing!!!
)
echo !Error! funcconfig
@GOTO :EOF
:funclog
::==========================================
: LOGGING
@ECHO %DATETIME%.!Error!
@GOTO :EOF
Thank you RElliott63 that helps
I came up with this solution. And I can add functions as it develops.
Thank you all for your help. I greatly appreciate it. I knew this was going to be a good site!
Chonn
@set ELEVEL=%ERRORLEVEL%
:FUNCTIONCALLS
::======================================
For /D %%F in (FuncConfig) Do (
Call :%%F
@ echo !ELEVEL!
@ ECHO !DATETIME!.!ERRORLEVEL!>>!DATELOG!.!computername!.log
)
@GOTO :eof
:funcconfig
::======================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do @set DATETIME=%%C%%A%%B%%D%%E%%F%
@SET DATELOG=%DATETIME:~0,-6%
@IF NOT EXIST %STRINGLIST% (set ELEVEL=1)
@GOTO :EOF
I came up with this solution. And I can add functions as it develops.
Thank you all for your help. I greatly appreciate it. I knew this was going to be a good site!
Chonn
@set ELEVEL=%ERRORLEVEL%
:FUNCTIONCALLS
::======================================
For /D %%F in (FuncConfig) Do (
Call :%%F
@ echo !ELEVEL!
@ ECHO !DATETIME!.!ERRORLEVEL!>>!DATELOG!.!computername!.log
)
@GOTO :eof
:funcconfig
::======================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do @set DATETIME=%%C%%A%%B%%D%%E%%F%
@SET DATELOG=%DATETIME:~0,-6%
@IF NOT EXIST %STRINGLIST% (set ELEVEL=1)
@GOTO :EOF
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Chonn wrote:Here is the latest code snippet. the errorlevel is still not working right inside the for loop even with the "IF ERRORLEVEL" change.
Thoughts? Improvements?
:FUNCTIONCALLS
::======================================
For /D %%F in (FuncConfig funclog) Do (
Call :%%F
echo %errorlevel%
If errorlevel 1 goto :exit
)
@GOTO :eof
:funcconfig
::=========================================
: CONFIGURING VARIABLES
@for /f "tokens=2-7 delims=/:. " %%A in ("%date%:%time: =0%") do set DATETIME=%%C%%A%%B%%D%%E%%F%
@SET TOOLSDIR=%CD%
@SET STRINGLIST=%CD%\OSLIST.TXT
@IF EXIST %STRINGLIST% (
ECHO ok
) else (
set /A ErrorLevel+=1
ECHO %STRINGLIST% is Missing!!!
)
echo %errorlevel% funcconfig
@GOTO :EOF
:funclog
::==========================================
: LOGGING
@ECHO %DATETIME%.%ERRORLEVEL%
@GOTO :EOF
I would expect the "if errorlevel" code to work, but your echo %errorlevel% won't because of delayedexpansion. Switching to delayedexpansion seems more logical to you, so RElliott's solution looks like it's great. I almost always use delayedexpansion myself.