2 batch string questions - string

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.

Related

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
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

How do I end an echo string in Batchfile?

I'm trying to created a Batch program that has multiple echo commands and if commands in one line. The problem is when I type a command after an echo command, it thinks it's part of the echo string and prints it to the screen instead of executing it.
For example:
if %var1% == 1 echo %var2% (right here I need to end the echo) if %var3% == 1 echo %var4%
echo.
if %var5% == 1 echo %var6% (right here I need to end the echo) if %var7% == 1 echo %var8%
I'm sure it's fairly simple, but I need to know if there's some character or command that will end a line without being interpreted as part of the message. Thanks in advance!
You need to write a string without a linefeed. echowon't to that. Instead you can use this workaround:
<nul set /p"=first "
<nul set /p"=second "
echo third
for your example:
if %var1% == 1 <nul set /p=%var2%
if %var3% == 1 echo %var4%
You can use && to separate pieces of code:
if %Var1% equ 12 echo %Var1% && if %Var2% neq 12 echo %Var2%
I tested it, it works.
I posted this answer here because it seems that my previous comment go unnoticed.
Usually an & is used to separate several individual commands in the same line; however, if the & is placed after an if or for (in the same line), then the & groups all commands in the same if or for. If you want to put several individual if or for commands (in the same line), you need to isolate each one enclosing they in parentheses:
(if %var1% == 1 echo %var2%) & if %var3% == 1 echo %var4%
(if %var5% == 1 echo %var6%) & (if %var7% == 1 echo %var8%) & echo After two previous if's
(if %varX% == 1 echo This one & echo This also) & echo Independently of previous if

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

Batch: How to append the name of a variable instead of the value of that variable

I am trying to append strings to a text file. So far, my code looks like this:
echo -%appointment% >>C:\Users\Evan\Desktop\Programming\Events.txt
echo set /a h=(%hours%*3600)+(%minutes%*60) >>C:\Users\Evan\Desktop\Programming\Events.txt
echo set /a i=%day%-%dbf% >>C:\Users\Evan\Desktop\Programming\Events.txt
echo if %g% leq %h% if %b% equ %day% echo %appointment% %time% Today >>C:\Users\Evan\Desktop\Programming\Events.txt
echo if %b% lss %day% if %b% geq %i% echo %appointment% %time% %date% >>C:\Users\Evan\Desktop\Programming\Events.txt
The problem is, the variables %b% and %g% are dependent on the specific occurance. %b% and %g% change with date, so while their values will be accurate when I append them into the text file, their values will NOT be accurate when I actually want to convert the text file into a batch file and use it. How do I literally append the variable %b% as text and not its current value so that it can change every time I run the text file as a batch? If I try to append "%b%" in code with those quotations, in the occuring file, it is simply output as "".
Thank you.
Double the % for the % you wish to output literally.
% escapes % (ie causes it to be interpreted as an ordinary, not a special character.)
^ escapes most awkward characters like | - but not %

search for if a specific text (group of string) is present into a file using a batch file

I am searching for if a specific text (group of string) is present into a file using a batch file.
Here is what I write but it's not working for text. It's just working for strings (not searching for a text).
rem %1 is name of the file whose text is being found
FindStr /C:%2 %1
If %ERRORLEVEL% EQU 0 echo text %2 is Present
If %ERRORLEVEL% EQU 1 echo text %2 is not Present
For example If the text "I have breakfast" must be searched into a file which contains "I have breakfast every day", command echo must print this one:
" text I have breakfast is present ".
Any help! please.
This file works perfectly.
The command line you need to find your text is
yourbatch yourtextfile.txt "I have breakfast"
If you execute
yourbatch yourtextfile.txt I have breakfast
then it will search simply for I because SPACE is a delimiter, and in order to search for a space-separated string, you need to "quote the string"
Same, for that matter, with the filename if your filename contains spaces.
yourbatch "your text file.txt" "I have breakfast"
%%1 is "your text file.txt"
%%2 is "I have breakfast"
INCLUDING the quotes.
To remove the quotes, if you want to, you'd use %~2
so - you could ECHO
ECHO with quotes:%2 and without: %~2
ALSO: be careful about
If %ERRORLEVEL% EQU 0 echo text %2 is Present
If %ERRORLEVEL% EQU 1 echo text %2 is not Present
ECHO is one of the few commands that does NOT change errorlevel. Many DO change it, so for instance if you were to write
If %ERRORLEVEL% EQU 0 echo Y|find "x" >nul
If %ERRORLEVEL% EQU 1 echo text %2 is not Present
then if the echo Y|find "x" >nul was found because errorlevel from the previous step was 0, then because echo Y|find "x" >nul sets errorlevel to 1, the second line would ALSO be executed.
This should work, if you call the script script.bat "file name" "search string":
#echo off&setlocal
rem %1 is name of the file whose text is being found
FindStr /C:"%~2" "%~1" >nul
If %ERRORLEVEL% EQU 0 echo text %2 is Present
If %ERRORLEVEL% EQU 1 echo text %2 is not Present
You could do the following:
FindStr /C:"%~2" "%~1" >nul && echo text "I have breakfast is present".

Resources