Page 1 of 1

Ms Dos Library to externalize your subroutines

Posted: 08 Feb 2014 18:28
by budhax
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.

Re: Ms Dos Library to externalize your subroutines

Posted: 08 Feb 2014 18:44
by Squashman
budhax,
You are not the first person to mention this concept on the forums. been around for a while.

Re: Ms Dos Library to externalize your subroutines

Posted: 08 Feb 2014 20:19
by carlos
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"

Re: Ms Dos Library to externalize your subroutines

Posted: 08 Feb 2014 23:48
by Aacini
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

Re: Ms Dos Library to externalize your subroutines

Posted: 24 May 2016 17:22
by sambul35
@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. :)

Re: Ms Dos Library to externalize your subroutines

Posted: 25 May 2016 01:55
by foxidrive
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.

Re: Ms Dos Library to externalize your subroutines

Posted: 25 May 2016 08:26
by Aacini
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