Solved

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

Posted on 2011-09-07
10
462 Views
Last Modified: 2012-05-12
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
Comment
Question by:jl66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36498117
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
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 210 total points
ID: 36498125
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 36498130
dang! too slow again!  :)
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 33

Expert Comment

by:knightEknight
ID: 36498151
oops, one correction - I left out the : in the call to S2, it should be:

call S2.bat :GetSum A B C
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 40 total points
ID: 36498162
Didn't "we" forget something here?
  call :%*
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36498172
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 36498176
Matter of taste if you provide the colon in the   calll ....bat ...    or in    call :%*
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 36498186
both work, but I like call :%* even better.
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36498209
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
 

Author Closing Comment

by:jl66
ID: 36499094
Greatly appreciate for prompt response. Thanks a lot, Gurus!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question