Page 1 of 1

for loop and function calls?

Posted: 01 May 2009 16:17
by Chonn
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

Posted: 03 May 2009 18:25
by RElliott63
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

Posted: 04 May 2009 14:16
by Chonn
Thank you. That's working. However, the error code from a function call is not returning it so that it can exit. Thoughts?

I echoed the error level in the function and it echos that there is a 1 and not 0. But when it returns to the For loop the error is not passed back.

Thanks ahead of time.

Posted: 04 May 2009 19:19
by avery_larry
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

Posted: 05 May 2009 12:39
by Chonn
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

Posted: 05 May 2009 14:54
by RElliott63
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


Posted: 05 May 2009 16:44
by Chonn
Thank you RElliott63 that helps

:D

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

Posted: 06 May 2009 08:24
by avery_larry
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.