Nested IF Statement, unexpected error - windows

I have created a nested if statement that just performs very simple tasks. Here is the code below:
#ECHO OFF
SET ANS=%1
IF "%ANS%"=="" ( ECHO You Entered Nothing
)
IF /i %ANS%==Y ( ECHO You entered Yes
)
IF /i %ANS%==N ( ECHO You entered NO
)
IF %ANS%==? ( ECHO I am confused
)
My problem is that when "%ANS%"=="" i get a "( was unexpected at this time" after echoing the message i have provided. Everything else works as planned but i am not sure why i am getting this message.

The error message comes from your second IF.
If %1 is empty, your first IF gives you "You entered nothing" as intended.
Then the second IFline translates to
IF /i ==Y ( ECHO You entered YES
(because %ANS% is empty)
Therefore you get a "( was unexpected at this time).
To correct this, write
IF /i "%ANS%"=="Y" ( ECHO You entered Yes

This has elements that protect it from spaces and duoble quotes and & characters.
#ECHO OFF
SET "ANS=%~1"
IF "%ANS%"=="" ( ECHO You Entered Nothing )
IF /i "%ANS%"=="Y" ( ECHO You entered Yes )
IF /i "%ANS%"=="N" ( ECHO You entered NO )
IF "%ANS%"=="?" ( ECHO I am confused )

Try this one ... it works perfectly
#ECHO OFF
SET ANS="%1"
IF %ANS%=="" ( ECHO You Entered Nothing
)
IF /i %ANS%=="Y" ( ECHO You entered Yes
)
IF /i %ANS%=="N" ( ECHO You entered NO
)
IF %ANS%=="?" ( ECHO I am confused
)

Related

Problem with batch file doesn't detect file

I have a problem with my program, I don't know how can I make it detect that the file NewFile(%i%) exists.
for /l %%i in (1, 1, 100) do (
if EXIST NewFile(%i%).txt (
echo "New file"> NewFile(%%i).txt
goto EndLoop
)
if EXIST NewFile.txt(
echo "New file"> NewFile(%%i).txt
goto EndLoop
) else (
echo "New file"> NewFile.txt
goto EndLoop
)
)
It says ") was unexpected at this time."
How can I fix this?
You currently have a few issues, of which the first is the one causing the issue you experienced, but the others will also cause the script to not function. You have a for loop with a parathesis inside the loop, for is expecting that loop as the ending parenthesis of the loop. So you would need to escape that closing parenthesis as if EXIST NewFile(%%i^).txt (
You also have an invalid %i% in the first if statement. it should be %%i
The line if EXIST NewFile.txt( should have a space before the opening parenthesis.
#echo off
for /l %%i in (1,1,100) do (
if EXIST "NewFile(%%i^).txt" (
echo "New file"> NewFile(%%i^).txt
goto :EndLoop
)
if EXIST NewFile.txt (
echo "New file"> NewFile(%%i^).txt
goto EndLoop
) else (
echo "New file"> NewFile.txt
goto :EndLoop
)
)
Then, the goto's as I am not sure what you want to achieve there, but just to make sure you know, if it finds one file that matches it will exit the loop. If that is your intention then that is fine, if you want to perform the action on all the numbers, you should not have the goto there.

If statement in batch file - user chooses option

New to batch/sql files. Thanks for you help.
What im trying to do:
user picks option 1, runs my sql file.
user picks 2, exits program.
user enters nothing or invalid option, ECHO "invalid option".
Problem: anything I type will exit my program. What am I doing wrong?
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :sql file
) ELSE (
IF "%input%"=="2" (
GOTO :Exit
)
) ELSE (
IF "%input%"=="" (
ECHO Invalid option chosen.
)
)
PAUSE
Just use the choice command, its instructions are available by entering choice /? at the Command Prompt.
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
CHOICE /C 12 /M "Your choice"
IF ERRORLEVEL 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
If you still wanted to use, what in your case I believe is the wrong Set /P input method, then something like this should be relatively robust, given that your end user is free to enter whatever they like as input, mischievous or harmful intent included:
#ECHO OFF
ECHO 1 - Show Report
ECHO 2 - Exit
:GETInput
SET "input="
SET /P "input=Your choice: "
2>&1 SET input | FINDSTR /XR "input=[12]" 1>NUL || (
ECHO Invalid option chosen
GOTO GETInput
)
IF %input% EQU 2 GOTO :EOF
REM This line is your SQL report code
PAUSE
There is one extract closing bracket [Line #12] inside nested if..else. Also, as far as i know, your label :sql file should not contain space and you must declare it inside the batch file (Same for label :Exit)
I did few changes in your code to make it work
#echo off
:Main
cls
ECHO 1 - Show Report
ECHO 2 - Exit
SET choice=""
SET /P input=Your choice:
IF "%input%"=="1" (
GOTO :SQLReport
) ELSE IF "%input%"=="2" (
GOTO :EOF
) ELSE (
ECHO.
ECHO Invalid option chosen.
pause
GOTO :Main
)
:SQLReport
ECHO.
ECHO Running SQL Report...
GOTO :EOF
:EOF

How to execute batch commands depending on user input as string

set /P id=Enter id:
echo %id%
if %id%=='a'
(
:: DO THINGS1
EXIT
)
if %id%=='b'
(
:: DO THINGS2
EXIT
)
else (
echo Input can be either a or b
)
But everytime I am running the file it is not performing any command further even though I am giving 'a' or 'b' as input, it asks for only till user input and exiting. Would appreciate your suggestions
Notice how I wrap both values either side of = in double quotes.
#echo off
set /P id=Enter id:
echo %id%
if "%id%"=="a" (
:: DO THINGS1
goto :eof
)
if "%id%"=="b" (
:: DO THINGS2
goto :eof
) else (
echo Input can be either a or b
)
This typically compares values exactly. i.e something like this.
if "a"=="a"
Would match, because you have the exact either side, where you tried to match this:
if a=='a'

windows batch syntax error in a not used if block

why the following batch script is failing with "The syntax of the command is incorrect." error on Windows 7 if I provide no argument:
IF NOT [%1]==[] (
echo "blablabla" > %1
) ELSE (
echo "please provide argument"
)
But there is no problem with this code:
IF NOT [%1]==[] (
echo "blablabla"
) ELSE (
echo "printing not existing argument: %1"
)
Thanks in advance
Encapsulating the filename by double quotations enforces the if/else block to parse and read the expression correctly.
For example
IF NOT [%1]==[] (
echo "blablabla" > "%1"
) ELSE (
echo "please provide argument"
)
The problem is that the entire if/else block is read and parsed at once. So if %1 is empty, the redirection > is still parsed and evaluated invalid.
You can work around the issue like that:
if not "%~1"=="" (
goto :Redirect
) else (
echo please provide argument
)
goto :Continue
:Redirect
> "%~1" echo blablabla
:Continue
I replaced the brackets by quotation marks, because they protect white-spaces and other special characters. The ~-symbol ensures that the returned argument appears unquoted, so there is always one pair of quotes in the expression "%~1".
Moreover, I removed quotes from the echo command lines as they were output too, and I moved the redirection part > "%~1" in front of echo in order to avoid a trailing SPACE to be returned.

How can I check if a variable contains another variable within a windows batch file?

Assuming the following batch file
set variable1=this is variable1
set variable2=is
set variable3=test
if variable1 contains variable2 (
echo YES
) else (
echo NO
)
if variable1 contains variable3 (
echo YES
) else (
echo NO
)
I want the output to be a YES followed by a NO
I've resolved this with the following
setLocal EnableDelayedExpansion
set variable1=this is variable1
set variable2=is
set variable3=test
if not "x!variable1:%variable2%=!"=="x%variable1%" (
echo YES
) else (
echo NO
)
if not "x!variable1:%variable3%=!"=="x%variable1%" (
echo YES
) else (
echo NO
)
endlocal
I got the basic idea from the following answer but it wasn't searching by a variable so it wasn't completely what I was looking for.
Batch file: Find if substring is in string (not in a file)
another way:
echo/%variable1%|find "%variable2%" >nul
if %errorlevel% == 0 (echo yes) else (echo no)
the / prevents output of Echo is ON or Echo is OFF in case %variable1% is empty.
Gary Brunton's answer did not work for me.
If you try with set variable1="C:\Users\My Name\", you will end up with an error :
'Name\""' is not recognized as an internal or external command
Adapting this answer Find out whether an environment variable contains a substring, I ended up with :
echo.%variable1%|findstr /C:"%variable2%" >nul 2>&1
if not errorlevel 1 (
echo Found
) else (
echo Not found
)
The following is based on JBEs answer in this thread. It distinguishes empty/undefined variables.
if not defined VARIABLE1 (echo VARIABLE1 undefined) & goto proceed
if not defined VARIABLE2 (echo VARIABLE2 undefined) & goto proceed
echo %VARIABLE1% | find "%VARIABLE2%" > nul
if ERRORLEVEL 1 (echo Not found) & goto proceed
echo Found
:proceed
If the value of VARIABLE1 contains parentheses, e.g. the value C:\Program Files (x86), then the parentheses may be interpreted as a distinct command rather than echoed, causing an error. Substrings like (x86) can be escaped with carets for purposes of echoing properly, e.g.:
SET V1_DISPLAY=%VARIABLE1%
if /i "%V1_DISPLAY:(x86)=%" NEQ "%V1_DISPLAY%" set V1_DISPLAY=%V1_DISPLAY:(x86)=^^(x86^^)%
echo %V1_DISPLAY% | find "%VARIABLE2%" > nul
The second statement, above, can be read as follows:
If replacing substring (x86) with nothing makes a difference, then (x86) is present, so replace any occurrence of (x86) with ^^(x86^^), where each pair of carets represent an escaped single caret.

Resources