#1 27 Nov 2016 18:14

triopium
Member
Registered: 27 Nov 2016
Posts: 6

Using call on specific label in batch file

I have a batch file myfunct.bat for storing user defined functions. It has following format:

:funct1
:: My function1 here
SET par1=%%1
SET par2=%%2
SET par3=%%2
GOTO:EOF

:funct2
:: My function2 here
SET par1=%%1
SET par2=%%2
SET par3=%%2
GOTO:EOF

:funct3
:: My function2 here
SET par1=%%1
SET par2=%%2
SET par3=%%2
GOTO:EOF

I would like to use my functions in another batch file. (Something like loading package or library.)
I'm trying to use variations of of following but without success:
CALL myfunct.bat :label <par1> <par2> <par3>

I know it works if the functions are pasted in the same bat file from which they are called.
Is there a simple way to do it?

One solution which occurred to me is to append all functions from myfunct.bat. (myfunct.bat >> mycurrent.bat)
I would have to check if the functions are already appended or delete them before appending.

Offline

#2 27 Nov 2016 19:28

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 157

Re: Using call on specific label in batch file

The easiest way would be to have your library script take in parameters and have the first line be

call :%1 %2 %3 %4

where %1 is the function and %2-4 are the parameters of that function

Then, when your main script calls the library script, you just use

call library.bat function param1 param2 param3

Offline

#3 27 Nov 2016 20:31

triopium
Member
Registered: 27 Nov 2016
Posts: 6

Re: Using call on specific label in batch file

Shadow Thief wrote:

The easiest way would be to have your library script take in parameters and have the first line be

call :%1 %2 %3 %4

where %1 is the function and %2-4 are the parameters of that function

Then, when your main script calls the library script, you just use

call library.bat function param1 param2 param3

Nice thank you very much. I have just started to learn cmd and was losing hope. This certainly gave me new energy. Can I pass unlimited number of parameters to call?

Offline

#4 28 Nov 2016 02:11

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 157

Re: Using call on specific label in batch file

You can only refer to %1 through %9 at any given time, but when you use the shift command, you can pass in significantly more (I'm having trouble finding the exact number, but it seems to be over 100).

Offline

#5 28 Nov 2016 02:29

Aacini
Member
Registered: 05 Dec 2012
Posts: 125

Re: Using call on specific label in batch file

Just use:

call :%*

Offline

#6 29 Nov 2016 09:11

triopium
Member
Registered: 27 Nov 2016
Posts: 6

Re: Using call on specific label in batch file

Aacini wrote:

Just use:

call :%*

I use GOTO %1 in the header of my function library. Whith this I don't have to write call <all the parametrs> in the header.

Offline

#7 29 Nov 2016 15:33

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 157

Re: Using call on specific label in batch file

If you do that, your arguments will be off by one.

Also, call returns to the line it was called from when it finishes, while goto does not.

Offline

Board footer

Powered by FluxBB