can you show me how a sub syntax in a batch works

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

can you show me how a sub syntax in a batch works

#1 Post by nnnmmm » 29 Aug 2017 03:00

Code: Select all

aa.bat has

set a=1 2
set b=3 4

call anyname(a,b,c,d)
echo %a%
echo %b%
echo %c%
echo %d%

:eof
--------------------
ans=
1 2
3 4
1 2 --- 3 4
1 2 ggg 3 4
---------------------------------

bb.bat has
sub anyname(a, b, c, d)
   set c=%a% --- %b%
   set d=%a% ggg %b%
end sub



can you show me how a sub syntax in a batch works?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: can you show me how a sub syntax in a batch works

#2 Post by penpen » 29 Aug 2017 05:05

The first line of a sub is a label containig its name, param declaration is not needed, so this might help you:

Code: Select all

@echo off
echo start of main
call :sub1 "param1" "param2" "param3"
call :sub2 a b
echo end of main
goto :eof

:sub1
echo start of sub1
echo param1="%~1"
echo param2="%~2"
echo param3="%~3"
echo end of sub1
goto :eof

:sub2
echo start of sub2
echo param1="%~1"
echo param2="%~2"
echo end of sub2
exit /b


penpen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: can you show me how a sub syntax in a batch works

#3 Post by aGerman » 29 Aug 2017 10:35

You really should follow penpen's suggestions because you still try to write Batch like Basic. Your approach to have subroutines in another file is quite uncommon in Batch although it's possible.

aa.bat

Code: Select all

@echo off &setlocal
set "a=1 2"
set "b=3 4"

call "bb.bat" anyname a b c d
echo %a%
echo %b%
echo %c%
echo %d%

pause


bb.bat

Code: Select all

call :%~1 %*
exit /b

:anyname
setlocal EnableDelayedExpansion
set "x=!%~2! --- !%~3!"
set "y=!%~2! ggg !%~3!"
endlocal &set "%~4=%x%" &set "%~5=%y%"
exit /b

Steffen

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: can you show me how a sub syntax in a batch works

#4 Post by nnnmmm » 31 Aug 2017 04:49

>you still try to write Batch like Basic.
i must trace the variables to understand at this point, once i see how, i can adapt fast
Last edited by nnnmmm on 31 Aug 2017 08:26, edited 1 time in total.

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: can you show me how a sub syntax in a batch works

#5 Post by nnnmmm » 31 Aug 2017 07:04

Code: Select all

:anyname
   setlocal EnableDelayedExpansion

   set a=!%2!
   set b=!%3!

   set x=!a! --- !b!
   set y=!a! ggg !b!

   endlocal &set %4=%x% &set %5=%y%

exit /b


this is my conclusion, your actual variable must start from %2 and since it has 4 variables, %2 %3 %4 %5
it seems that :anyname gets one variable %1 too, probably used for a different sub name in the same aa.bat

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: can you show me how a sub syntax in a batch works

#6 Post by nnnmmm » 31 Aug 2017 08:19

SUB-Libray.BAT has

Code: Select all

CALL :%1 %*
EXIT /B

:ANYNAME
(@ECHO OFF
 Setlocal EnableDelayedExpansion

   SET A=!%2!
   SET B=!%3!

   SET X=!A! --- !B!
   SET Y=!A! GGG !B!
)
(Endlocal
   SET %4=%X%
   SET %5=%Y%
)
EXIT /B


:STRLEN
(@ECHO OFF
 Setlocal EnableDelayedExpansion
    SET "S1=!%~2!"

    SET "STR=A!S1!"
    SET "LEN=0"

    FOR /L %%A IN (12,-1,0) DO (
        SET /A "LEN|=1<<%%A"
        FOR %%B IN (!LEN!) DO IF "!STR:~%%B,1!"=="" SET /A "LEN&=~1<<%%A"
    )
)
(Endlocal
    IF "%~3" NEQ "" SET /A %~3=%LEN%
)
EXIT /B


now i know what that AA= is
it is the initialization of building a library file with many sub names in it.

:name1
:name2
:name3
.
.
.
this is so good that from now on, all my variables in SUB-LIBirary will start with %2 and i can easily book keeping so many bat files.

so good...... thanks for this AA=

Code: Select all

AA=
CALL :%1 %*
EXIT /B

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: can you show me how a sub syntax in a batch works

#7 Post by aGerman » 31 Aug 2017 08:36

Actually it's quite simple. Look at how we call the file:
call "bb.bat" anyname a b c d

call "bb.bat" tells the interpreter to run bb.bat in the same cmd.exe process as from where it was called. Thus, aa.bat and bb.bat run in only one process which is the reason why leaving out @echo off has no effect and which is the reason why both files share the same scope for variables.

anyname a b c d are the 5 (space-separated) arguments we pass to bb.bat. In bb.bat you can access them with the parameters %1 (anyname) till %5 (d). I used the tilde (~) that expand the values without surrounding quotation marks (if any). (This is best practice as well as surrounding an assignment with quotation marks. Both are you chronically ignoring ...)

All passed arguments can be accessed at once by using %* that expands to anyname a b c d in this case.

Looking at what happens in bb.bat. First line is
call :%~1 %*
which expands to
call :anyname anyname a b c d

As you can see we again pass anyname a b c d to the label where it can be accessed via %1 ... %5 just as above.

the main works even without AA= in the bb.bat
What do you expect such a line should do? I never saw something like that in a batch code and I assume it will rather cause an error message.


Steffen

Post Reply