[Batch] Check for character presence in variable

Solved
Ekiam Posted messages 43 Registration date   Status Member Last intervention   -  
barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   -
Bonjour,

I am facing a problem because I would like to add an option such that when a certain string is entered, it goes to a label. It's quite complicated to explain, but here is the principle.
In fact, if the person puts in the variable choice: 1 /random, then after executing what is in label 1, it should go to the Random label and perform the actions. However, I can't seem to do that.
So we would have: %choix%=1 /random

@echo off

chcp 28591 > nul

:menu
cls

set /p choix="Something to say? "

if %choix%==0 goto :0
if %choix%==1 goto :1
if %choix%==end goto :end
if %choix:~1%==/random goto :Random

goto :menu

:0
echo You said 0
pause > nul
goto :end

:1
echo You said 1
pause > nul
goto :end

:Random
cls
echo %RANDOM%
pause

:end
echo It's the end
pause
exit

1 answer

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 929
     
    Good evening,

    It's very simple, there is not just IF that allows you to create a condition, but also && and ||:

    @echo off

    chcp 28591 > nul

    :menu
    cls

    set /p choice="Something to say? "

    echo %choice% | find "0" && goto :0
    echo %choice% | find "1" && goto :1
    if %choice%==end goto :end

    goto :menu

    :0
    echo You said 0
    echo %choice% | find "random" && goto :Random
    pause > nul
    goto :end

    :1
    echo You said 1
    echo %choice% | find "random" && goto :Random
    pause > nul
    goto :end

    :Random
    cls
    echo %RANDOM%
    pause

    :end
    echo It's the end
    pause
    exit


    --

    “Artificial intelligence is defined as the opposite of natural stupidity.”
    1
    1. Ekiam Posted messages 43 Registration date   Status Member Last intervention  
       
      Good evening,

      Thank you, it's exactly what I was looking for but it's strange, I couldn't find it anywhere.

      I would just like to know if it is possible for it not to return what is in the variable.
      (I would simply like it to check that the variable contains random but without writing in the console what the variable %choix% contains)

      I hope I have been clear enough.
      0
      1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 929 > Ekiam Posted messages 43 Registration date   Status Member Last intervention  
         
        It is possible to redirect the output to null, like this:

        @echo off

        chcp 28591 > nul

        :menu
        cls

        set /p choix="Anything to say? "

        echo %choix% | find "0" >nul && goto :0
        echo %choix% | find "1" >nul && goto :1
        if %choix%==end goto :end

        goto :menu

        :0
        echo You said 0
        echo %choix% | find "random" >nul && goto :Random
        pause > nul
        goto :end

        :1
        echo You said 1
        echo %choix% | find "random" >nul && goto :Random
        pause > nul
        goto :end


        :Random
        cls
        echo %RANDOM%
        pause

        :end
        echo It's the end
        pause
        exit
        0
    2. Ekiam Posted messages 43 Registration date   Status Member Last intervention  
       
      Good evening,

      Thank you very much, it's exactly what I wanted!

      Have a nice evening!
      0
      1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 929 > Ekiam Posted messages 43 Registration date   Status Member Last intervention  
         
        Thank you, have a good evening as well.
        0