Calling functions from a library

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Calling functions from a library

#1 Post by Matt Williamson » 24 May 2016 12:48

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

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Calling functions from a library

#2 Post by Aacini » 24 May 2016 16:42

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Calling functions from a library

#3 Post by foxidrive » 25 May 2016 01:51

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Calling functions from a library

#4 Post by penpen » 25 May 2016 04:40

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

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Calling functions from a library

#5 Post by Matt Williamson » 25 May 2016 07:45

Thank you everyone that responded. I appreciate it.

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Calling functions from a library

#6 Post by Thor » 25 May 2016 10:30

"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

Post Reply