Ms Dos Library to externalize your subroutines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
budhax
Posts: 63
Joined: 09 Oct 2006 12:25

Ms Dos Library to externalize your subroutines

#1 Post by budhax » 08 Feb 2014 18:28

Hello,
If you use the same subroutine(s) in many batch files, it could be useful to put them all in one file (Library.cmd) then include (call) this library from your batch files. So, you can update your subroutine(s) once in the Library.

a script (caller.cmd) calling the subroutine LENTS in the Library:

Code: Select all

@SETLOCAL ENABLEDELAYEDEXPANSION
Call Library.cmd LENTS "WinRAR\WinRAR.exe" "LEN"
Echo.Chars: [%LEN%]
Pause
Exit


And the Library.cmd:

Code: Select all

@ECHO OFF
   IF /i NOT "%~1"=="" (
   Call:%~1 "%~2" "%~3" "%~4" "%~5" "%~6" "%~7" "%~8"
   ) ELSE (ECHO.Missing SubRoutine Name^! Press To End&Pause>Nul&EXIT)
GOTO:END


:LENTS
   SET s_=%~1
   SET /a %~2=0
   :LOOP
   SET /a L_+=1
   SET s_=%s_:~0,-1%
   IF "%s_%"=="" (SET %~2=%L_%&GOTO:EOF) ELSE GOTO:LOOP
GOTO:EOF

:END


Any comment, improvement are welcome.
Thanks and regards.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Ms Dos Library to externalize your subroutines

#2 Post by Squashman » 08 Feb 2014 18:44

budhax,
You are not the first person to mention this concept on the forums. been around for a while.

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Ms Dos Library to externalize your subroutines

#3 Post by carlos » 08 Feb 2014 20:19

the only problem is that you are limiting the number of arguments to 7.

Maybe is better have every function in a single file, then instead call a label, call a file:

example:

Code: Select all

call lents.cmd "WinRAR\WinRAR.exe" "LEN"

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

Re: Ms Dos Library to externalize your subroutines

#4 Post by Aacini » 08 Feb 2014 23:48

All these methods have the disadvantage that the original code that call a subroutine in the same file needs to be changed in order to call the subroutine in the library file; if a program is large, these changes may be problematic. Also, calling a subroutine in an external file through a "caller" code is slightly slower than directly calling it in the same file.

There is a simple trick that allows to call the subroutines in the same way no matters if they are placed in the original file or in the library file, so the original program needs not to be changed; the only code needed to achieve this trick is an open parentheses and two ren commands at beginning, and two ren commands and a closing parentheses at end of the code that call the library subroutines. The trick consist in "change the context" of the running Batch file so the library file takes its place and allows that its subroutines can be directly called.

Code: Select all

(
   rem Switch the context to the library file
   ren "%0" orig-main.bat
   ren library.bat "%0"
   rem From this point on, any library subroutine can be called

   . . . .

   rem Switch back the context to the original file
   ren "%0" library.bat
   ren orig-main.bat "%0"
)


For further details and a working example, see this post.

Antonio

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Ms Dos Library to externalize your subroutines

#5 Post by sambul35 » 24 May 2016 17:22

@Aacini

I love batch tricks, they make it so much more fun to program complex batches by avoiding repetitive plain and lengthy routines. When it comes to batch shortcuts, I'm always glad to learn more of those. :)

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

Re: Ms Dos Library to externalize your subroutines

#6 Post by foxidrive » 25 May 2016 01:55

It is clever - I just posted and said so here: viewtopic.php?f=3&t=7171#p46723

There is only one a drawback, that I noted there also.

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

Re: Ms Dos Library to externalize your subroutines

#7 Post by Aacini » 25 May 2016 08:26

sambul35 wrote:@Aacini

I love batch tricks, they make it so much more fun to program complex batches by avoiding repetitive plain and lengthy routines. When it comes to batch shortcuts, I'm always glad to learn more of those. :)


@sambul35,

I invite you to review my contributions at this thread.

Antonio

Post Reply