Page 1 of 1
Calling functions from a library
Posted: 24 May 2016 12:48
by Matt Williamson
I'm sure this has been discussed before so feel free to link me to topics. I've just been reading for a few hours and I'm not getting anywhere close to finding what I want to do by searching "Batch library"
I have a batch file that I store all of my frequently used functions in and I just copy and paste them from it into my scripts. What I'd like to do is just call them from my "Function library" directly. As an example:
In my function library I have the following function:
Code: Select all
:IsPingable2 comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~1" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~2=%name%
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
I'd like to call it like this:
Code: Select all
@echo off
setlocal
call fnLib :IsPingable2 "10.18.30.165" ret
echo %ret%
exit /b
Re: Calling functions from a library
Posted: 24 May 2016 16:42
by Aacini
You may use this simple trick:
Code: Select all
@echo off
setlocal
(
rem Switch the context to the library file
ren "%0" orig-main.bat
ren fnLib.bat "%0"
call :IsPingable2 "10.18.30.165" ret
rem Switch the context back to the original file
ren "%0" fnLib.bat
ren orig-main.bat "%0"
)
echo %ret%
exit /b
Further details at
this post.
Antonio
Re: Calling functions from a library
Posted: 25 May 2016 01:51
by foxidrive
This is a clever trick!
The only drawback that comes to mind is a situation where you need to abort the batch file: if it happens during the moment the batch files have been renamed then it becomes a manual task to sort out the renaming.
Aacini wrote:You may use this simple trick:
Code: Select all
@echo off
setlocal
(
rem Switch the context to the library file
ren "%0" orig-main.bat
ren fnLib.bat "%0"
call :IsPingable2 "10.18.30.165" ret
rem Switch the context back to the original file
ren "%0" fnLib.bat
ren orig-main.bat "%0"
)
echo %ret%
exit /b
Further details at
this post.
Antonio
Re: Calling functions from a library
Posted: 25 May 2016 04:40
by penpen
This
feature found by jeb may also help you:
"test.bat"
Code: Select all
@echo off
call :function1 A B
call :function2 C D
call :function3 E F
call :function4 G H
goto :eof
:function1
:function2
library1.bat %*
:function3
:function4
library2.bat %*
"library1.bat"
Code: Select all
@exit /b 0
:function1
echo :function1 %*
goto :eof
:function2
echo :function2 %*
goto :eof
"library2.bat"
Code: Select all
@exit /b 0
:function3
echo :function3 %*
goto :eof
:function4
echo :function4 %*
goto :eof
Result:
Code: Select all
Z:\>test.bat
:function1 A B
:function2 C D
:function3 E F
:function4 G H
penpen
Re: Calling functions from a library
Posted: 25 May 2016 07:45
by Matt Williamson
Thank you everyone that responded. I appreciate it.
Re: Calling functions from a library
Posted: 25 May 2016 10:30
by Thor
"fnLib.bat"
Code: Select all
@echo off
rem Limited up to 9 parameters
goto %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9
:IsPingable comp ret
setlocal
for /f "tokens=2" %%a in (
'"ping -a -n 1 -4 "%~2" | Find "Pinging" 2>nul"') do set name=%%a
endlocal & set %~3=%name%
ping -n 1 -w 1000 -4 -l 8 "%~2" | Find "TTL=">nul
exit /b
:Add num1 num2 ret
setlocal
echo.
set /a result=%~2 + %~3
echo "%~2 + %~3" = %result%
endlocal & set %~4=%result%
exit /b
:Subtract num1 num2 ret
setlocal
echo.
set /a result=%~2 - %~3
echo "%~2 - %~3" = %result%
endlocal & set %~4=%result%
exit /b
:Multiply num1 num2 ret
setlocal
echo.
set /a result=%~2 * %~3
echo "%~2 * %~3" = %result%
endlocal & set %~4=%result%
exit /b
:Divide num1 num2 ret
setlocal
echo.
set /a result=%~2 / %~3
echo "%~2 / %~3" = %result%
endlocal & set %~4=%result%
exit /b
"demo_fnLib.bat":
Code: Select all
@echo off
setlocal
call fnLib IsPingable "8.8.8.8" ret
echo return = %ret%
call fnLib IsPingable "8.8.4.4" ret
echo return = %ret%
call fnLib Add 5 10 ret
echo return = %ret%
call fnLib Subtract 30 10 ret
echo return = %ret%
call fnLib Multiply 7 10 ret
echo return = %ret%
call fnLib Divide 10 2 ret
echo return = %ret%
endlocal
exit /b