Search code examples
stringbatch-fileif-statementcmdfindstr

Check if userinput contains substring


Hello please help me with this one! I would like to check if the user input URL contains the defined SUBSTRING or not. If yes I would like to GOTO LONG else GOTO SHORT

Thank you!

 @echo off


setlocal enabledelayedexpansion enableextensions

SET /P "URL= Input the link of the video: "



SET "SUBSTRING=?filter=archives&sort=time"




ECHO !URL! | FINDSTR /C:"!SUBSTRING!">nul
IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG


:LONG
SET LINK=1
ECHO THIS IS A LONG LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END



:SHORT
SET LINK=0
ECHO THIS IS A SHORT LINK
ECHO "THE LINK NUMBER IS %LINK%"
ECHO !URL!
GOTO END


:END
pause

Solution

  • So this is the working code. I will now mention the problems I had so others can learn from it

    1. I must not enable setlocal enabledelayedexpansion
    2. I have to use quotes around variables for the SET command
    3. Because I have delayedexpansion disabled I must use percentages instead of exclamation marks when I want to use a variable in a command later

    Correct me if I am wrong but thats what I figured out with these guys' help and on my own by trial and error I made this list and updated the code so if a newbie like me gets stuck they can have a look at this post. Once again thank you guys!

    @echo off
    
    SET /P "URL=Input the link of the video:"
    
    SET "SUBSTRING=?filter=archives"
    
    ECHO "%URL%"|FINDSTR /C:"%SUBSTRING%">nul
    IF ERRORLEVEL 1 (GOTO SHORT) ELSE GOTO LONG
    
    :LONG
    SET LINK=1
    ECHO THIS IS A LONG LINK
    ECHO "THE LINK NUMBER IS %LINK%"
    GOTO END
    
    :SHORT
    SET LINK=0
    ECHO THIS IS A SHORT LINK
    ECHO "THE LINK NUMBER IS %LINK%"
    GOTO END
    
    :END
    pause