Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling function from included batch file with a parameter

Tags:

batch-file

cmd

In my main batch file I include another batch file and want to call a function defined in there, code looks like following:

@echo off
call define_wait.bat

if "%1"=="WAIT" (
    call :WAIT_AND_PRINT 5
    echo.
)

REM rest...

My define_wait.bat looks like following:

:WAIT_AND_PRINT
set /a time=%1
for /l %%x in (1, 1, %time%) do (
    ping -n 1 -w 1000 1.0.0.0 > null
    echo|set /p=.
)
goto :EOF

:WAIT
set /a time="%1 * 1000"
ping -n 1 -w %time% 1.0.0.0 > null
goto :EOF

The problem is that if I define the wait function in another batch file it does not work, calling call :WAIT_AND_PRINT 5 does not hand on the parameter correctly (Error: missing operand)... If I copy my code from my define_wait.bat int my main batch file, everything works fine...

How would I make that correctly?

like image 631
prom85 Avatar asked Jan 07 '23 10:01

prom85


1 Answers

Working function bat that forwards it's parameters to it's subfunction:

@echo off
call %*
goto :EOF

:WAIT_AND_PRINT
set /a time=%1
for /l %%x in (1, 1, %time%) do (
    ping -n 1 -w 1000 1.0.0.0 > null
    echo|set /p=.
)
goto :EOF

:WAIT
set /a time="%1 * 1000"
ping -n 1 -w %time% 1.0.0.0 > null
goto :EOF

In the main bat I now don't include the batch file anymore but call it directly like following:

call define_wait.bat :WAIT_AND_PRINT 5
like image 67
prom85 Avatar answered May 10 '23 14:05

prom85