• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

How to call the function located in another script in DOS?

Have a script S1.bat and S2.bat and want to call a few functions located in S2.bat within S1.bat. How to do that with the DOS shell commands? For example,

in S2.bat
...
:GetSum A B C
if "%3"=="" (@echo S2.bat A B C ) & (goto :eof)
set /a C=A+B
...
goto :EOF

in S1.bat
...
REM want to call :GetSum  and how???
call ???GetSum 5 6 C
@echo %C%
...
end
 
0
jl66
Asked:
jl66
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
Bill PrewCommented:
There is no way to directly call a specific label in the second batch file, you can only call the batch file itself.  That being said, what you can do is call the second batch file, and pass the name of the subroutine to execute as the first parm, and work with that when you get to the top of the second batch file.  There it can do a CALL %1 to run the desired routine.  The only tricky part is passing the parms, but that can be worked out.  I'll see if I can work up a small example.

~bp
0
 
knightEknightCommented:
You can pass the function name as a parameter and call it like this:

:in S1.bat, do this:
call S2.bat GetSum A B C


:in S2.bat, do this:

@echo off
call %*
exit/b

:GetSum
if "%3"=="" (@echo S2.bat A B C ) & (goto :eof)
set /a C=A+B
...
goto :EOF
0
 
knightEknightCommented:
dang! too slow again!  :)
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
knightEknightCommented:
oops, one correction - I left out the : in the call to S2, it should be:

call S2.bat :GetSum A B C
0
 
QlemoDeveloperCommented:
Didn't "we" forget something here?
  call :%*
0
 
Bill PrewCommented:
Okay, give this a try:

S1.BAT
@echo on
call s2.bat "GetSum" 5 6 C
echo %C%

Open in new window

S2.BAT
@echo on
call :%~1 %2 %3 %4
goto :EOF

:GetSum
set /a %~3=%~1+%~2
goto :EOF

Open in new window

~bp
0
 
QlemoDeveloperCommented:
Matter of taste if you provide the colon in the   calll ....bat ...    or in    call :%*
0
 
knightEknightCommented:
both work, but I like call :%* even better.
0
 
Bill PrewCommented:
So, managing the parms as a single entity, my example becomes:

@echo off
set C=
call s2.bat :GetSum 5 6 C
echo %C%

Open in new window


@echo on
call %*
goto :EOF

:GetSum
set /a %~3=%~1+%~2
goto :EOF

Open in new window

0
 
jl66Author Commented:
Greatly appreciate for prompt response. Thanks a lot, Gurus!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now