Batch file find string in string - batch-file

In a batch file I want to see if %1 is in a set.
E.g., as an alternative to
if %1 equ /? goto help
if /I %1 equ -? goto help
if /I %1 equ /help goto help
etc
It seems like it should be simple, but I can't make it work. I've tried with and without FOR loops and search:string.

#echo off
setlocal enabledelayedexpansion
set "helpoptions=#/?#-?#/help#"
if not "!helpoptions:#%~1#=!"=="%helpoptions%" goto help
goto :eof
:help
echo Help
would be an option, albeit not a very pretty one.

Related

Need help understanding a short batch script (possible scam)

A friend of mine received some "tech support" from a questionable source and they left a batch file on his computer entitled "Junk Remover". I am suspicious of the batch file but I have no idea what the batch is doing. I was hoping someone could shine some light on what is going on when this batch is run.
#echo off
FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
IF (%adminTest%)==(Access) goto noAdmin
for /F "tokens=*" %%G in ('wevtutil.exe el') DO (call :do_clear "%%G")
echo.
echo goto theEnd
:do_clear
echo clearing %1
wevtutil.exe cl %1
goto :eof
:noAdmin
exit
Thanks in advance for anyone taking a look and help would be appreciated.

Batch Script_ Compare Two String variables

I am trying to compare one variable from user input("yes")
, and predefined("yes") values. However, when I run the .bat file the cmd give me the syntax error message. Please help me how to compare two string values in if statement. thank you.
Here is Code:
#echo off
goto main
:main
setlocal
set ans=yes
set /p var=do you have problem?:
echo %var%
if "%var%" == "%ans%"(
echo there is problem
)
echo.
endlocal
goto :eof
Output:
The syntax of the command is incorrect.
Try this
#echo off
goto main
:main
setlocal
set ans=yes
set /p var=do you have problem?:
if "%var%" == "%ans%" (echo there is problem) else (echo there is no problem)
endlocal
goto :eof
Except I think this is cleaner:
#echo off
:main
set /p var=do you have problem?:
if "%var%" == "yes" (echo there is problem) else (echo there is no problem && goto :main)
PAUSE

How to use set and if statements in batch files

So i'm trying to make a small batch program with some sets and ifs, seems easy enough right? Apparently, you can't use "set" and "if" like this.
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue EQU 1
Then continue the code and later in the program do this.
If %variableTrue% EQU 1 goto next
Please note I've tried it with exclamation marks as well.
With the exclamation marks, it's like it completely ignores the statement, even if it's true it will continue as usual. With the percentage signs, it says very quickly before crashing
"1" was not expected at this time.
Or something like that, like I said it barely stayed for half a second.
I always thought you could do it like this as long as there are no conflicting variables.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ 13 goto thanks
If "!fn!" EQU 13 goto setvar
:setvar
set "coolestNum==1"
:thanks
cls
If "!coolestNum!"== 1 goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
That doesn't give an error, it just ignores the statement and keeps going as usual.
UPDATE:
After fixing errors this still doesn't work.
When I use exclamation marks it ignores the line, and when I use percentage signs it says:
"
"1 was not expected at this time"
One set of problems is your IF statements. For example, If "!coolestNum!"== 1 goto cool. The quotes are included in the comparison.
You need to be symmetric - either include quotes on both sides
If "!coolestNum!" == "1" goto cool
or neither:
If !coolestNum! == 1 goto cool
The same problem exists with If "!fn!" EQU 13 goto setvar, as well as the line before it.
The other problem you have is set "coolestNum==1" has an extra, unwanted =. The second = becomes part of the value. You only want two equal signs with IF comparisons.
Here is corrected code:
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
:favnum
cls
echo What is your favorite number?
set /p fn=Favorite Number
If "!fn!" NEQ "13" goto thanks
If "!fn!" EQU "13" goto setvar
:setvar
set "coolestNum=1"
:thanks
set coolest
cls
If "!coolestNum!"=="1" goto cool
echo Thanks
pause
goto :eof
:cool
echo cool
pause
goto :eof
But your logic is needlessly convoluted. The following produces the exact same result.
:start
#echo off
setlocal enabledelayedexpansion
title test
color a
cls
echo What is your favorite number?
set /p fn=Favorite Number
if !fn! equ 13 (
set "coolestNum=1"
echo cool
) else echo Thanks
pause
exit /b
Use
set variableTrue=1
rather than
set variableTrue EQU 1
This batch script:
#echo off
setlocal enabledelayedexpansion
cls
set variableTrue=1
echo A
if %variableTrue% EQU 1 goto next
echo B
goto :EOF
:next
echo C
outputs
A
C

Needing help to modify existing batch file used to clear event viewers

I have some code here that is being used to clear the event viewers on a few pcs. Currently I have to run this batch file with administrator access. Could someone help me in trying to modify it to just run normally.
Thanks
Here is the code
#echo off
FOR /F "tokens=1,2*" %%V IN ('bcdedit') DO SET adminTest=%%V
IF (%adminTest%)==(Access) goto noAdmin
for /F "tokens=*" %%G in ('wevtutil.exe el') DO (call :do_clear "%G")
echo.
echo goto theEnd
:do_clear
echo clearing %1
wevtutil.exe cl %1
goto :eof
:noAdmin
exit

Show help message if any command line argument equals /?

I wish to write a Windows Batch script that first tests to see if any of the command line arguments are equal to /?. If so, it displays the help message and terminates, otherwise it executes the rest of the script code. I have tried the following:
#echo off
FOR %%A IN (%*) DO (
IF "%%A" == "/?" (
ECHO This is the help message
GOTO:EOF
)
)
ECHO This is the rest of the script
This doesn't seem to work. If I change the script to:
#echo off
FOR %%A IN (%*) DO (
ECHO %%A
)
ECHO This is the rest of the script
and call it as testif.bat arg1 /? arg2 I get the following output:
arg1
arg2
This is the rest of the script
The FOR loop appears be ignoring the /? argument. Can anyone suggest an approach to this problem that works?
Something like this should do the trick:
#echo off
IF [%1]==[/?] GOTO :help
echo %* |find "/?" > nul
IF errorlevel 1 GOTO :main
:help
ECHO You need help my friend
GOTO :end
:main
ECHO Lets do some work
:end
Thanks to #jeb for pointing out the error if only /? arg provided
Do not use a FOR loop, but use the following instead:
#ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
IF "%1" == "/?" (
ECHO This is the help message
GOTO:EOF
)
SHIFT
GOTO Loop
:Continue
ECHO This is the rest of the script
:EOF

Resources