Help with menu script

Discussion in 'Scripting' started by hypedave, Dec 12, 2014.

  1. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #1 hypedave, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    I am trying to create a script that will allow me 10 various scripts to run. I want the menu selection to proceed in the current window instead of opening a new windows. What am I missing?

    Code:
    @ECHO OFF
    pushd %~dp0
    SetLocal EnableDelayedExpansion:color and title
    color 1f
    title place title here
    :Set some defaults for later
    set ERRORTEMP=0
    ECHO ===============================================================================
    ECHO.                        PLACE HOLDER FOR MENU TITLE
    ECHO ===============================================================================
    ECHO.        ^(0^) - Select This Option
    ECHO.        ^(1^) - Select This Option
    ECHO.        ^(2^) - Select This Option
    ECHO.        ^(3^) - Select This Option
    ECHO.        ^(4^) - Select This Option
    ECHO.        ^(5^) - Select This Option
    ECHO.        ^(6^) - Select This Option
    ECHO.        ^(7^) - Select This Option
    ECHO.        ^(8^) - Select This Option
    ECHO.        ^(9^) - Select This Option
    ECHO ===============================================================================
    choice /c 0123456789q /n /m "Choose a menu option, or q to Quit: "
    SET ERRORTEMP=!ERRORLEVEL!
    IF !ERRORTEMP! EQU 0 (GOTO :Selection0)
    IF !ERRORTEMP! EQU 1 (GOTO :Selection1)
    IF !ERRORTEMP! EQU 2 (GOTO :Selection2)
    IF !ERRORTEMP! EQU 3 (GOTO :Selection3)
    IF !ERRORTEMP! EQU 4 (GOTO :Selection4)
    IF !ERRORTEMP! EQU 5 (GOTO :Selection5)
    IF !ERRORTEMP! EQU 6 (GOTO :Selection6)
    IF !ERRORTEMP! EQU 7 (GOTO :Selection7)
    IF !ERRORTEMP! EQU 8 (GOTO :Selection8)
    IF !ERRORTEMP! EQU 9 (GOTO :Selection9)
    IF !ERRORTEMP! EQU q (GOTO :QUIT)
    GOTO :MAINMENU
    :Selection0
    batchfile0.cmd
    GOTO :QUIT
    :Selection1
    batchfile1.cmd
    :Selection2
    batchfile2.cmd
    :Selection3
    batchfile3.cmd
    :Selection4
    batchfile4.cmd
    :Selection5
    batchfile5.cmd
    :Selection6
    batchfile6.cmd
    :Selection7
    batchfile7.cmd
    :Selection8
    batchfile8.cmd
    :Selection9
    batchfile9.cmd
    :QUIT
    CLS
    ECHO ===============================================================================
    ECHO.                          QUIT MESSAGE GOES HERE
    ECHO -------------------------------------------------------------------------------
    ECHO.                 Press (R) to Restart PC or (S) to shutdown
    ECHO ===============================================================================
    choice /c rs /t 10 /d r /n /m "System will reboot after 10 seconds (R/S): "
    IF !ERRORLEVEL! EQU 1 WPEUTIL reboot
    WPEUTIL shutdown
    
     
  2. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #2 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Check that the single CMD files, you want to open, end with 'goto:eof' and use

    Code:
    call batchfile1.cmd
    They will open and same window.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #3 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Give Menu an adress first. Then check that the single CMD files, you want to open, end with 'goto:eof' and use

    Code:
    @ECHO OFF
    pushd %~dp0
    SetLocal EnableDelayedExpansion:color and title
    color 1f
    title place title here
    :Set some defaults for later
    :MAINMENU
    set ERRORTEMP=0
    ECHO ===============================================================================
    ECHO.                        PLACE HOLDER FOR MENU TITLE
    ECHO ===============================================================================
    ECHO.        ^(0^) - Select This Option
    ECHO.        ^(1^) - Select This Option
    ECHO.        ^(2^) - Select This Option
    ECHO.        ^(3^) - Select This Option
    ECHO.        ^(4^) - Select This Option
    ECHO.        ^(5^) - Select This Option
    ECHO.        ^(6^) - Select This Option
    ECHO.        ^(7^) - Select This Option
    ECHO.        ^(8^) - Select This Option
    ECHO.        ^(9^) - Select This Option
    ECHO ===============================================================================
    choice /c 0123456789q /n /m "Choose a menu option, or q to Quit: "
    SET ERRORTEMP=!ERRORLEVEL!
    IF !ERRORTEMP! EQU 0 (GOTO :Selection0)
    IF !ERRORTEMP! EQU 1 (GOTO :Selection1)
    IF !ERRORTEMP! EQU 2 (GOTO :Selection2)
    IF !ERRORTEMP! EQU 3 (GOTO :Selection3)
    IF !ERRORTEMP! EQU 4 (GOTO :Selection4)
    IF !ERRORTEMP! EQU 5 (GOTO :Selection5)
    IF !ERRORTEMP! EQU 6 (GOTO :Selection6)
    IF !ERRORTEMP! EQU 7 (GOTO :Selection7)
    IF !ERRORTEMP! EQU 8 (GOTO :Selection8)
    IF !ERRORTEMP! EQU 9 (GOTO :Selection9)
    IF !ERRORTEMP! EQU q (GOTO :QUIT)
    GOTO :MAINMENU
    :Selection0
    call batchfile0.cmd
    pause
    GOTO :MAINMENU
    :Selection1
    batchfile1.cmd
    :Selection2
    batchfile2.cmd
    :Selection3
    batchfile3.cmd
    :Selection4
    batchfile4.cmd
    :Selection5
    batchfile5.cmd
    :Selection6
    batchfile6.cmd
    :Selection7
    batchfile7.cmd
    :Selection8
    batchfile8.cmd
    :Selection9
    batchfile9.cmd
    :QUIT
    CLS
    ECHO ===============================================================================
    ECHO.                          QUIT MESSAGE GOES HERE
    ECHO -------------------------------------------------------------------------------
    ECHO.                 Press (R) to Restart PC or (S) to shutdown
    ECHO ===============================================================================
    choice /c rs /t 10 /d r /n /m "System will reboot after 10 seconds (R/S): "
    IF !ERRORLEVEL! EQU 1 WPEUTIL reboot
    WPEUTIL shutdown
    

    They will open and same window.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    When I press 0, it does not go to the requested file batchfile0.cmd
    It goes back to the main menu.
     
  5. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #5 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Atm it assumes the file in same directory like main script. In case it's in a subfolder you need to adapt the path :g:.

    Code:
    call "subfolder\script.cmd"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Nope it's in the same directory as the main script.
     
  7. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    What is content of that file/what is it supposed to do?
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #8 hypedave, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Work in progress. Right now it's an empty file that executes a hello command
    Here is the content

    Code:
    echo you're reached bachfile0
    goto:eof
    
     
  9. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #9 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Then it should work, calling an Office Installer script to run inside WU TOOL window that way :hmm:.

    Check without ' as it might break it :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Nope it's not working at all.
     
  11. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #11 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Reading definitely helps :doh:. There's a little 'design' error in the menu, since it checks for an errorlevel 0 which isn't available here.

    Also you can reduce the menu to use the errorlevel directly, like this ...:

    Code:
    ::===============================================================================================================
    ::QUERY WU LOG
    :QueryLog
    cls
    call :MenuHeader "   QUERY WU LOG"
    echo    [Q] QUERY SINGLE UPDATE KBs
    echo    [F] QUERY FROM TEXT FILE
    echo:
    echo    [D] DOWNLOAD THE LINK LIST
    echo:
    echo     BACK TO MAIN MENU
    call :MenuFooter
    echo:
    CHOICE /C QFDB /N /M "%username%s CHOICE ?:"
    if %errorlevel%==1 goto :QueryLogManually
    if %errorlevel%==2 goto :QueryLogTextFile
    if %errorlevel%==3 goto :DownloadData
    if %errorlevel%==4 goto:MainMenu
    goto:QueryLog
    ::===============================================================================================================
    ::MENU HEADER
    :MenuHeader
    echo ===============================================================================
    echo %~1
    echo ===============================================================================
    goto:eof
    ::===============================================================================================================
    :: MENU FOOTER
    :MenuFooter
    echo ===============================================================================
    echo:
    goto:eof
    
    
    
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #12 hypedave, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Changed the code to the following below. Now when I press 0 it foes to the quit option

    Code:
    @ECHO OFF
    pushd %~dp0
    SetLocal EnableDelayedExpansion
    :color and title
    color 1f
    title place title here
    :Set some defaults for later
    :MAINMENU
    set ERRORTEMP=0
    ECHO ===============================================================================
    ECHO.                        PLACE HOLDER FOR MENU TITLE
    ECHO ===============================================================================
    ECHO.        ^(0^) - Select This Option
    ECHO.        ^(1^) - Select This Option
    ECHO.        ^(2^) - Select This Option
    ECHO.        ^(3^) - Select This Option
    ECHO.        ^(4^) - Select This Option
    ECHO.        ^(5^) - Select This Option
    ECHO.        ^(6^) - Select This Option
    ECHO.        ^(7^) - Select This Option
    ECHO.        ^(8^) - Select This Option
    ECHO.        ^(9^) - Select This Option
    ECHO ===============================================================================
    choice /c 0123456789q /n /m "Choose a menu option, or q to Quit: "
    if %errorlevel%==0 goto :Selection0
    if %errorlevel%==1 goto :Selection1
    if %errorlevel%==2 goto :Selection2
    if %errorlevel%==3 goto :Selection3
    if %errorlevel%==4 goto :Selection4
    if %errorlevel%==5 goto :Selection5
    if %errorlevel%==6 goto :Selection6
    if %errorlevel%==7 goto :Selection7
    if %errorlevel%==8 goto :Selection8
    if %errorlevel%==9 goto:QUIT
    :Selection0
    call file0.cmd
    GOTO :QUIT
    :Selection1
    call file1.cmd
    :Selection2
    call file2.cmd
    :Selection3
    call file3.cmd
    :Selection4
    call file4.cmd
    :Selection5
    call file5.cmd
    :Selection6
    call file6.cmd
    :Selection7
    call file7.cmd
    :Selection8
    call file8.cmd
    :Selection9
    call file9.cmd
    :QUIT
    CLS
    ECHO ===============================================================================
    ECHO.                          QUIT MESSAGE GOES HERE
    ECHO -------------------------------------------------------------------------------
    ECHO.                 Press (R) to Restart PC or (S) to shutdown
    ECHO ===============================================================================
    choice /c rs /t 10 /d r /n /m "System will reboot after 10 seconds (R/S): "
    IF !ERRORLEVEL! EQU 1 WPEUTIL reboot
    WPEUTIL shutdown
    
     
  13. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #13 s1ave77, Dec 12, 2014
    Last edited: Dec 12, 2014
    As said: when using choice the first errorlevel is 1 and so on, not 0.

    Equal what choices i.e. 1,2,3,4,5 or A,B,C,D,E >> errorlevel is always from 1,2,...,5.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    8,075
    26,883
    270
    #14 abbodi1406, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    And you could directly parse errorlevel
    Code:
    if errorlevel 1 goto :Selection0
     
  15. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #15 hypedave, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Crap, I see where my problem is. If I remove the following code then it executes properly

    Code:
    :QUIT
    CLS
    ECHO ===============================================================================
    ECHO.                          QUIT MESSAGE GOES HERE
    ECHO -------------------------------------------------------------------------------
    ECHO.                 Press (R) to Restart PC or (S) to shutdown
    ECHO ===============================================================================
    choice /c rs /t 10 /d r /n /m "System will reboot after 10 seconds (R/S): "
    IF !ERRORLEVEL! EQU 1 WPEUTIL reboot
    WPEUTIL shutdown
    
    Here is the code now

    Code:
    @ECHO OFF
    pushd %~dp0
    SetLocal EnableDelayedExpansion
    :color and title
    color 1f
    title place title here
    :Set some defaults for later
    :MAINMENU
    set ERRORTEMP=0
    ECHO ===============================================================================
    ECHO.                        PLACE HOLDER FOR MENU TITLE
    ECHO ===============================================================================
    ECHO.        [A] - Select This Option
    ECHO.         - Select This Option
    ECHO.        [C] - Select This Option
    ECHO.        [D] - Select This Option
    ECHO.        [E] - Select This Option
    ECHO.        [F] - Select This Option
    ECHO.        [G] - Select This Option
    ECHO.        [H] - Select This Option
    ECHO.         - Select This Option
    ECHO.        [J] - Select This Option
    ECHO ===============================================================================
    CHOICE /C ABCDEFGHIJ /N /M "Choose a menu option, or Q to Quit: "
    if %errorlevel%==1 goto :SelectionA
    if %errorlevel%==2 goto :SelectionB
    if %errorlevel%==3 goto :SelectionC
    if %errorlevel%==4 goto :SelectionD
    if %errorlevel%==5 goto :SelectionE
    if %errorlevel%==6 goto :SelectionF
    if %errorlevel%==7 goto :SelectionG
    if %errorlevel%==8 goto :SelectionH
    if %errorlevel%==9 goto :SelectionI
    if %errorlevel%==10 goto :SelectionJ
    if %errorlevel%==11 goto:QUIT
    :SelectionA
    call file0.cmd
    GOTO :QUIT
    :SelectionB
    call file1.cmd
    :SelectionC
    call file2.cmd
    :SelectionD
    call file3.cmd
    :SelectionE
    call file4.cmd
    :SelectionF
    call file5.cmd
    :SelectionG
    call file6.cmd
    :SelectionH
    call file7.cmd
    :SelectionI
    call file8.cmd
    :SelectionJ
    call file9.cmd
    


    The problem now is I really need the removed code to work
    Code:
    CLS
    ECHO ===============================================================================
    ECHO.                          QUIT MESSAGE GOES HERE
    ECHO -------------------------------------------------------------------------------
    ECHO.                 Press (R) to Restart PC or (S) to shutdown
    ECHO ===============================================================================
    choice /c rs /t 10 /d r /n /m "System will reboot after 10 seconds (R/S): "
    IF !ERRORLEVEL! EQU 1 WPEUTIL reboot
    WPEUTIL shutdown
     
  16. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #16 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    .......................:cool2:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    #17 hypedave, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Wow man seriously, this missing factor was
    Code:
    ^(
    . Thanks man
    Do you have any examples of how to make a footer in the main menu?
     
  18. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #18 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    FYI 1: Several signs needs to be escaped when one will echo to screen or file: (),<>,|,& need '^', % needs another %.

    FYI 2: variables only need to be expressed as !var! when you assign a runtime variable inside a for-loop to the var and the use that var inside the same loop.

    Example:

    Code:
    
    @echo off
    title ... by s1ave77
    :: Code by s1ave77
    color 4F
    setlocal EnableDelayedExpansion
    ::===============================================================================================================
    echo --------------------------------------------------------------------------------
    echo RUNTIME VARIABLE USAGE IN FOR /R LOOPs
    echo --------------------------------------------------------------------------------
    echo:
    set "path=f:\win"
    set "ext=iso"
    for /r "%path%" %%f in (*.%ext%) do (
    set file=%%~f
    set filepath=%%~dpf
    set filenamewo=%%~nf
    set filename=%%~nxf
    set extension=%%~xf
    set size=%%~zf
    echo Assuming the rt variable is %%%%Y:
    echo:
    echo filepath.....^| %%%%~Y   ^| !file!
    echo folderpath...^| %%%%~dpY ^| !filepath!
    echo filename.ext.^| %%%%~nY  ^| !filename!
    echo filename.....^| %%%%~nxY ^| !filenamewo!
    echo extension....^| %%%%~zY  ^| !extension!
    echo size.[byte]..^| %%%%~zY  ^| !size!
    )
    echo:
    echo --------------------------------------------------------------------------------
    echo:
    pause
    endlocal
    exit   
    
    Will show:

    Code:
    --------------------------------------------------------------------------------
    
    RUNTIME VARIABLE USAGE IN FOR /R LOOPs
    --------------------------------------------------------------------------------
    
    
    Assuming the rt variable is %%Y:
    
    filepath.....| %%~Y   | f:\win\x64.iso
    folderpath...| %%~dpY | f:\win\
    filename.ext.| %%~nY  | x64.iso
    filename.....| %%~nxY | x64
    extension....| %%~zY  | .iso
    size.[byte]..| %%~zY  | 334073856
    
    --------------------------------------------------------------------------------
    
    
    Drücken Sie eine beliebige Taste . . .
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. s1ave77

    s1ave77 MDL Guide Dog/Dev

    Aug 15, 2012
    15,327
    21,241
    340
    #19 s1ave77, Dec 12, 2014
    Last edited by a moderator: Apr 20, 2017
    Use them as shown in #11 :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  20. hypedave

    hypedave MDL Member

    Oct 14, 2014
    129
    30
    10
    Thanks i'll have to scratch the footer idea. No time to go back and rewrite this, im on a roll now, lol.