Check if userinput contains substring - string

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

Using double quotes properly helps fix some of your problems. There shouldn't be any need to use delayed expansion if you use quotes for the echo as well.
#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
Update Showing execution of batch file.
C:\Users\Squashman\Desktop>test.bat
Input the link of the video:https://www.twitch.tv/videos/578427308?filter=archives&sort=time
THIS IS A LONG LINK
"THE LINK NUMBER IS 1"
Press any key to continue . . .

So this is the working code. I will now mention the problems I had so others can learn from it
I must not enable setlocal enabledelayedexpansion
I have to use quotes around variables for the SET command
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

Related

I'm looking to convert my MS-DOS .bat file into the equivalent linux .sh file

Basically I have to make my linux program do the same thing as my MS-DOS. Could someone help with pointers and things like that?
All the program currently does, is pull a menu for basic, advanced account creation and close.
Advanced has nothing in atm, and close does what you expect. Basic then asks you for:
Full name
Username
Password
It then saves it all in a .log file (after checking for usernames already entered) along with the exact time of creation. For example:
Badja
John Doe
123
23/04/2015 15:07:32.61
#echo off
c:
:1
set curdir=%test%
echo Welcome to OP-SYS Account creation. Please choose which mode you would like to continue in.
echo.
echo [1] Basic Account Creation
echo [2] Advanced Account Creation
echo [3] Exit
echo.
REM Menu choices
set /p cat=
if %cat%==1 (
goto 2
) else if %cat%==2 (
goto 3
) else if %cat%==3 (
goto 5
) else (
goto 4
)
:2
REM Basic account creation
echo Welcome to basic account creation.
REM user enters details
REM Username
echo Please Enter a Username
set /p username=
echo.
REM Real Name
echo Please enter your full name
set /p fullname=
echo.
REM Password
echo Please enter a password
set /p password=
echo.
REM Real name
REM Save to file
if exist %username%.log (
echo User name already exists, please enter a new user name to create an account, or return to the log in screen
goto 1
) else (
echo %username% >> %username%.log
echo %fullname% >> %username%.log
echo %password% >> %username%.log
echo %date% %time% >> %username%.log )
timeout /t 3 /nobreak > NUL
REM pause
goto end
:3
REM Advanced account creation
echo Welcome to advanced account creation.
echo This is not complete, please return to main menu.
Pause
goto 1
:4
REM Error
echo error
echo.
goto 1
:5
REM Exit
echo Goodbye.
goto end
:end
exit​
I know there are a few things that can't be copied over to linux, but I just don't know where to start, this is my basis:
PS3='Please enter your choice: '
options=("cat 1" "cat 2" "end")
select opt in "${options[#]}"
do
case $opt in
"cat 1")
echo "Basic"
;;
"cat 2")
echo "Advanced"
;;
"end")
break
;;
*) echo invalid option;;
esac
done
Any help would be amazing guys!
function choice1 {
echo "Do stuff here to create an account"
}
function choice2 {
echo "Get the point?"
}
function choice3 {
exit
}
#### Main
echo "Welcome to OP-SYS Account creation. Please choose which mode you would like to continue in."
echo
echo "[1] Basic Account Creation
[2] Advanced Account Creation
[3] Exit"
echo
read CHOICE #### This loads your choice into a variable
eval choice"$CHOICE" ### This is evaluation awesomeness
If the eval seems strange, you can go with a more traditional case...esac statement to call the various functions. I would suggest getting your menu cycling and escaping the way that you want before adding in your special function.
clear
echo "Welcome to OP-SYS Account creation. Please choose which mode you would like to continue in."
echo
echo "[1] Basic Account Creation
[2] Advanced Account Creation
[3] Exit"
echo

Loop Variable checking

Just trying to check if a function exists before trying to execute that Function.
I fear I may be trying to go to simple with this, I should probably be checking the string prior to executing the Process loop.
SET /P Selection=Please Select Options?
echo You chose: %Selection%
Call :Process
pause
goto :EOF
:Process
for %%A in (%Selection%) do (
if not exist :Opt%%A (Call :Redo) ELSE (Call :Opt%%A)
)
GOTO :EOF
:Redo
Echo %Selection%
SET /P Selection=Selection was Invalid, Please choose a Valid Option:
Call :Process
GOTO :EOF
:Opt1
ECHO Option 1's code
GOTO :EOF
So the problem I'm getting is that I seam to get stuck in if not exist statement and well is not allowing the functions to run correctly.
Thanks in advance
When a CALL command is executed with a non-existent label an error message is displayed, but the process continue with an ERRORLEVEL = 1 after the call. You may made good use of this point in order to avoid to check if the label exists:
EDIT: Code modified as reply to the comment
SET /P Selection=Please Select Options?
echo You chose: %Selection%
:Process
for %%A in (%Selection%) do (
Call :Opt%%A 2>NUL
if errorlevel 1 (
SET /P Selection=Selection was Invalid, Please choose a Valid Option:
goto :Process
)
)
pause
goto :EOF
:Opt1
ECHO Option 1's code
exit /B 0

Batch file: How to get variable's value using variable's name already stored in a string

1) Quick example:
set a="Hello"
set d="a"
Now, how do I get the value of "a" using (the value of) variable d? For example, the variable name could have been entered by a user using a prompt, or the name of the variable could have been sent to a function.
None of these ideas work:
set e=%%d%%
set e=%%%d%%%
set e=set e=%%d%%
%e%
After an hour of brainstorming and Googling, I have come up with this, but it seems too complicated and clumsy - is there really no other/easier way?:
set a="Hello"
set b="Good day"
set c="Good night"
set /p d="Give me a variable name"
call :GetVarVal %%%d%%% "e"
REM This now gives the correct value:
echo %e%
goto :eof
:GetVarVal
set "%~2=%~1"
goto :eof
2) Also, sort of similar, is there a better way to do this (ideally without a custom function):
set a="C:\Users\Blah\Documents\MP4Box\MP4Box.exe"
call :get_drive_and_path a
echo %b%
goto :eof
:get_drive_and_path
set b=%~dp1
goto :eof
Thanks!
example without delayed expansion (preserves exclamation marks):
#echo off &setlocal
set "a=Hello!"
set "d=a"
call echo %%%d%%%
output:
Hello!
If I understood you correctly, the trick called "delayed expansion" should work:
SETLOCAL ENABLEDELAYEDEXPANSION
SET a=Hello
SET b=a
ECHO This is the value of a: !%b%!
SETLOCAL DISABLEDELAYEDEXPANSION
Check how it works here
Part 2
set file="C:\Users\Blah\Documents\MP4Box\MP4Box.exe"
for %%a in (%file%) do echo %%~dpa
its just easy:
as you type e.g.
SET VarName=VarValue
Then the SET-function gets given a string looking like "VarName=VarValue" - it does not matter whether you use the marks there or not, the following commands do the same:
SET VarName=VarValue
SET "VarName=VarValue"
so now reflecting, what tools we have to build a string, we come up to the idea to use this:
SET "Name=VarName"
SET "%VarName%=VarValue"
Echo %Varname%
Better late than never - have a good time

Match a variable to part of another variable in batch

I want to match a variable to part of the contents of another variable in batch. Here is some pseudo code for what I want to do.
set h= Hello-World
set f= This is a Hello-World test
if %h% matches any string of text in %f% goto done
:done
echo it matched
Does anybody know how I could accomplish this?
Based off of this answer here, you can use the FINDSTR command to compare the strings using the /C switch (modified from the linked answer so you don't have to have a separate batch file for comparing the strings):
#ECHO OFF
set h=Hello-World
set f=This is a Hello-World test
ECHO Looking for %h% ...
ECHO ... in %f%
ECHO.
echo.%f% | findstr /C:"%h%" 1>nul
if errorlevel 1 (
ECHO String "%h%" NOT found in string "%f%"!
) ELSE (
ECHO String "%h%" found in string "%f%"!
)
ECHO.
PAUSE
If the following conditions are met:
The search is case insensitive
The search string does not contain =
The search string does not contain !
then you can use:
#echo off
setlocal enableDelayedExpansion
set h=Hello-World
set f=This is a Hello-World test
if "!f:*%h%=!" neq "!f!" (
echo it matched
) else (
echo it did not match
)
The * preceding the search term is only needed to allow the search term to start with *.
There may be a few other scenarios involving quotes and special characters where the above can fail. I believe the following should take care of such problems, but the original constraints still apply:
#echo off
setlocal enableDelayedExpansion
set h=Hello-World
set f=This is a Hello-World test
for /f delims^=^ eol^= %%S in ("!h!") do if "!f:*%%S=!" neq "!f!" (
echo it matched
) else (
echo it did not match
)
Here's another way:
#echo off
set "h=Hello-World"
set "f=This is a Hello-World test"
call set "a=%%f:%h%=%%"
if not "%a%"=="%f%" goto :done
pause
exit /b
:done
echo it matched
pause

2 batch string questions

1) Is there any built-in which can tell me if a variable's contents contain only uppercase letters?
2) is there any way to see if a variable contains a string? For example, I'd like to see if the variable %PATH% contains Ruby.
For part 1, findstr is the answer. You can just use the regex feature along with errorlevel:
> set xxokay=ABC
> set xxbad=AB1C
> echo %xxokay%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
0
> echo %xxbad%|findstr /r "^[A-Z]*$" >nul:
> echo %errorlevel%
1
It's important in this case that you do not have a space between the echo %xxokay% and the pipe character |, since that will result in a space being output which is not one of your acceptable characters.
For part 2, findstr is also the answer (/i is ignore case which may be what you want - leave it off if case must match):
> set xxruby=somewhere;c:\ruby;somewhere_else
> set xxnoruby=somewhere;somewhere_else
> echo %xxruby%|findstr /i ruby >nul:
> echo %errorlevel%
0
> echo %xxnoruby%|findstr /i ruby >nul:
> echo %errorlevel%
1
You can then use:
if %errorlevel%==1 goto :label
to change the behaviour of your script in both cases.
For example, the code segment for the ruby check could be something like:
:ruby_check
echo %yourvar%|findstr /i ruby >nul:
if %errorlevel%==1 goto :ruby_check_not_found
:ruby_check_found
:: ruby was found
goto :ruby_check_end
:ruby_check_not_found:
:: ruby was NOT found
:ruby_check_end
this is not a batch solution (cmd.exe), but an easier alternative using vbscript, which by default is already installed on your system.
Set objArgs = WScript.Arguments
var=objArgs(0)
check=var
If check=UCase(var) Then
WScript.Echo "String contains all uppercase"
Else
WScript.Echo "String doesn't contain all uppercase"
End If
' to check string contains substring
mystring="This is my ruby string"
check="ruby"
If InStr(mystring,check)>0 Then
WScript.Echo "string contains ruby"
End If
save the file as myscript.vbs and run it like this
C:\test>cscript //nologo myscript.vbs abC
String doesn't contain all uppercase
string contains ruby
C:\test>cscript //nologo myscript.vbs ABCD
String contains all uppercase
string contains ruby
using batch(cmd.exe) for string manipulation is the last thing you would want to do, unless you are absolutely restricted. Otherwise, use the more appropriate tool for the job.

Resources