Hello guys ; I am trying to learn "call" command with subroutines I want to write on screen hello maya how are you ? i am fine thank you here is my script , but it doesnt work..can you help me please @echo off setlocal enableextensions disabledelayedexpansion call :sub1 hello maya call :sub2 "how are you ?" call :sub3 "i am fine thank you" :sub1 echo %~1 %~2 exit /b :sub2 echo %~1 exit /b :sub3 echo %~1 exit /b pause exit
#1. when sending several values with the call, use comma to separate them #2. first your complete script, then beneath it the calls, dont mix it. #3. still recommend to use more explicit 'goto :eof' Should work: Code: @echo off setlocal enableextensions disabledelayedexpansion call :sub1 hello, maya call :sub2 "how are you ?" call :sub3 "i am fine thank you" pause exit :sub1 echo %~1 %~2 goto :eof :sub2 echo %~1 goto :eof :sub3 echo %~1 goto :eof
Do you mean all subroutines must be after "exit " command ? I read a few articles about this but none of them told me this information Thank you so much s1ave77
This would be the easiest way. When you use calls, your script isn't linear anymore. If they were in the middle, you'd need to tell the script to jump 'over' these parts with a goto :Jumpadress. For stuff i need several times from different parts of a script, i create calls and have them all at the end of my scripts.
I want to ask a second question s1ave77 ; I have an install.cmd which is @echo off setlocal enableextensions disabledelayedexpansion pushd "%~dp0" echo Installing Adobe Air 26.0.0.112 for %%g in (adobe\air\*.msi) do (start "" /wait msiexec /i %%g /qn /norestart) popd exit It works fine when i clicked it. I tried to write a script using "call :sub" method but it didnt work again here is the script @echo off setlocal enableextensions disabledelayedexpansion echo Installing Adobe Air 26.0.0.112 call :sub1 (adobe\air\*.msi), (start "" /wait msiexec /i %%g /qn /norestart) pause exit :sub1 for %%g in %~1 do %~2 goto :eof
It actually makes not much sense to have this as call, nothing to win here. Additionally for loops are picky and using brackets can be a nightmare .
Since he brackets after the do aren't needed, you could try: Code: @echo off setlocal enableextensions disabledelayedexpansion echo: Installing Adobe Air 26.0.0.112 set "command1=adobe\air\*.msi" set "command2=start /wait msiexec /i %%g /qn /norestart" call :sub1 "%command1%", "%command2%" pause exit :sub1 for %%g in (%~1) do %~2 goto :eof
Thank you I copied your script , saved it as soft.cmd and run it , but it didnt install adobe air I know that there is no need to use "call" in my example , but i am just trying to learn
Wooow it worked now , but why and how ? I dont want to disturb you anymore let me search for it , i think i can find the answer Thank you very much s1ave77 You are a great guy !
In set and echo commands '%' chars need to be escaped. to escape '%%' it needs '%%%%', confusing at first.
ohh i see now why we use %%%%g finally i recreated the script and it works fine now @echo off setlocal enableextensions disabledelayedexpansion echo echo: Installing Adobe Air 26.0.0.112 call :sub1 "adobe\air\*.msi", "start /wait msiexec /i %%%%g /qn /norestart" pause exit :sub1 for %%g in (%~1) do %~2 goto :eof Thanks again for your precious help