for loop and function calls?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chonn
Posts: 9
Joined: 17 Mar 2009 13:12

for loop and function calls?

#1 Post by Chonn » 01 May 2009 16:17

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

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 03 May 2009 18:25

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

Chonn
Posts: 9
Joined: 17 Mar 2009 13:12

#3 Post by Chonn » 04 May 2009 14:16

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.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 04 May 2009 19:19

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

Chonn
Posts: 9
Joined: 17 Mar 2009 13:12

#5 Post by Chonn » 05 May 2009 12:39

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

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#6 Post by RElliott63 » 05 May 2009 14:54

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


Chonn
Posts: 9
Joined: 17 Mar 2009 13:12

#7 Post by Chonn » 05 May 2009 16:44

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#8 Post by avery_larry » 06 May 2009 08:24

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.

Post Reply