script menu

Discussion in 'Scripting' started by Thomas Dubreuil, Jan 11, 2019.

  1. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    maybe a dumb question...I'm making a menu for a .bat script to run separate parts, like for ex:
    1 full script
    2 only reg part
    3 only task part

    I wonder if is there a way to run only part of the full script? for now I just copy parts and "goto :" from main menu but that makes a lot of redundant code...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Imp Eached

    Imp Eached MDL Novice

    May 30, 2013
    10
    6
    0
    You mean something like this:

    Code:
    @echo off
    
    
    :top
    cls
    echo   ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
    echo   º  Sample Menu  º
    echo   ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
    echo   º 1 Full Script º
    echo   º 2 Registry    º
    echo   º 3 Tasks       º
    echo   ÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹
    echo   º X Exit        º
    echo   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
    echo.
    set /p select="Choose one: "
    if /i %select% ==x goto exit
    if %select% ==3 goto task
    if %select% ==2 goto reg
    if %select% ==1 goto full
    goto top
    
    :full
     setlocal
     set full=on
     goto reg
    
    :reg
    Your registry commands go here
     if exist %full% ==on (goto task) else (goto cleanup)
    
    :task
    Your task commands go here
     if exist %full% ==on (goto comp) else (goto cleanup)
    
    :comp
     endlocal
     goto cleanup
    
    :cleanup
    Any nessecary cleanup should be done here
     echo.
     echo DONE!
     echo.
     pause
     goto top
    
    :exit
    
     
  3. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    yes, exactly! thanks a lot
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    To add to the answer, the right command is like:

    Code:
    set "full=full_is_on"
    if  "%full%" == "full_is_on" ( goto :task) else ( goto :cleanup)
    if exist it seems it is for searching path.
    Note: parenthesis is mandatory with else command, and you need a space between first parenthesis and goto
    Doesn't really need the quotes, unless you have a space in your variable.
    Thank you, used it a lot in my optimization script!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. Imp Eached

    Imp Eached MDL Novice

    May 30, 2013
    10
    6
    0
    That's weird, I've been using the commands exactly as I listed and have never had any problems. Out of curiosity, what OS are you running your script under?

    Edit: Something else I just thought of, are you running this under Powershell?
     
  6. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    If you define a variable, you usually either check if it's defined or what value it carries. The exist command is meant to check for files :g:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    To avoid redundand code, check out call command options ;).

    In opposite to goto it can jump back to the place where it was called from and additionally carry variables/info. That way you can create flexible 'functions' which can be used from different places within the script.

    An example from SVF2ISO for the hash checks to get an idea:
    Code:
    .
    .
    .
    call :ISOHashCheck "%siehash%", "%siename%"
    .
    .
    .
    :================================================================================================================
    ::===============================================================================================================
    :: ISO HASH CHECK
    :ISOHashCheck
    echo [ INFO ] Checking ISO Hash.
    echo [ INFO ] Hash  : %~1
    xcopy "files\busybox.exe" /s ".\" /Q /Y >nul 2>&1
    for /f "tokens=1 delims= " %%a in ('busybox.exe sha1sum %~2.iso') do set "dhash=%%a"
    if not !dhash! equ %~1 (
        busybox echo -e "\033[31;1m[ WARN ] Hash Mismatch!\033[0m"
        echo [ INFO ] Hash  : !dhash!
        if exist "*.txt" del /f /q "*.txt" >nul 2>&1
        if exist "*.exe" del /f /q "*.exe" >nul 2>&1
        if exist "*.SMRT" del /f /q "*.SMRT" >nul 2>&1
        if exist "*.sh" del /f /q "*.sh" >nul 2>&1
        pause
        goto:SVFISOMainMenu
    )
    if !dhash! equ %~1 (
        busybox echo -e "\033[32;1m[ INFO ] ISO Hash matching.\033[0m"
        echo [ INFO ] Hash  : !dhash!
    )
    goto:eof
    
    %~1 is expanded to first sent value, %~2 to the second and so on...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    ah....very nice! I already implemented all the goto functions in my script, also have a few call there...
    but I understand much better now with your example :) thanks a lot!
    ps If you have some time feel free to check my script for any "unorthodox code" or things I can improve, as I just started recently and like to learn. ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. s1ave77

    s1ave77 Has left at his own request

    Aug 15, 2012
    16,104
    24,378
    340
    Not really atm :D. Need to update SVF2ISO with latest Jan Refresh ISOs (4 download scripts, 4 encryption scripts, 1 database file for all ISOs and new encryption general part), not to mention the downloads to assure all is working :cool2:.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. Imp Eached

    Imp Eached MDL Novice

    May 30, 2013
    10
    6
    0
    The way I understand the exist command is that it can check for a variable (and how its set), a file or even a folder. The key is to make sure your variable names don't match (either partially or completely) any potential file or folder names.
    For the heck of it I modified one of the sections in my batch to what Thomas listed and it ran just the same as my way. I'm not going to worry about it anymore as both methods work.

    Thomas, when there is no file (extension) or folder (backslash) designation the exist command automatically searches everything.
     
  11. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    Thanks, I finally replaced most of the goto with call commands in latest version (here), I have lots of subroutines, and at the end it's easier for me to setup the calls from the beginning than to add many redirections in the middle of the script, also better for future development...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...