[Solved] Help needed for "call" commandline

Discussion in 'Scripting' started by tefor, May 29, 2017.

  1. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    #1 tefor, May 29, 2017
    Last edited: May 29, 2017
    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
     
  2. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    #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
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    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
     
  4. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    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
     
  6. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    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 :cool2:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    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 
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    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
     
  9. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    use:
    Code:
    set "command2=start /wait msiexec /i %%%%g /qn /norestart"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    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 !
     
  11. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    In set and echo commands '%' chars need to be escaped. to escape '%%' it needs '%%%%', confusing at first.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. tefor

    tefor MDL Member

    Apr 5, 2017
    138
    59
    10
    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
     
  13. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,368
    21,513
    340
    You're welcome :good3:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...