Equal variables - Batch - windows

So, I've the following batch script:
#echo off
set /p name=
rem a random number, don't care about it.
set complete_name=%name%.Creepy
Goto STEP1
:STEP1
echo %complete_name%|findstr /C:"9000" >nul 2>&1
if not errorlevel 1 (
goto 9000
) else (
GOTO CHECK2
)
:CHECK2
echo %complete_name%|findstr /C:"930" >nul 2>&1
if not errorlevel 1 (
goto 930
) else (
GOTO CHECK3
)
:CHECK3
echo %complete_name%|findstr /C:"310" >nul 2>&1
if not errorlevel 1 (
goto 310
) else (
ECHO PROBLEM
)
:9000
ECHO 9000
PAUSE
:930
ECHO 930
PAUSE
:310
ECHO 310
PAUSE
I want it to check if "9000" is in the variable or not, same for "930" and "310". And if none of these numbers are in the variable Echo problem. But everytime i run this script it goes to ECHO PROBLEM even if 9000/920/310 is in %complete_name%. So, is this the right way to check if a variable is in another one or there is an easier way to do it?
So I've tried this code:
#echo off
set name=310
set complete_name=%name%.Creepy
Goto STEP1
:STEP1
setlocal
if "%complete_name:9000=%"=="%complete_name%" (
if "%complete_name:930=%"=="%complete_name%" (
if "%complete_name:310=%"=="%complete_name%" (
echo PROBLEM
) else (
goto 9000
)
) else (
goto 930
)
) else (
goto 310
)
goto :eof
but I'm stuck at echo problem...

Save next code snippet, possibly named blabla.bat:
#Echo OFF
setlocal
set complete_name=%1
if "%complete_name:9000=%"=="%complete_name%" (
if "%complete_name:930=%"=="%complete_name%" (
if "%complete_name:310=%"=="%complete_name%" (
echo problem
) else (
echo valid 310
)
) else (
echo valid 930
)
) else (
echo valid 9000
)
Exit /B
and watch the output from
blabla x9000y
blabla x930y
blabla x310y
blabla x9a0b0c0y
That's a way of 1. nested IF ... ( ... ) ELSE ( ... ) and 2. using "Edit/Replace" a variable
Or, if GOTOs considered necessary, there is something similar with For loop (a good thought topic as well..).
#Echo OFF
setlocal EnableDelayedExpansion
set complete_name=%1
for %%G in (9000 930 310) DO (
if /I "!complete_name:%%G=!" neq "%complete_name%" GOTO :%%G
)
echo problem %complete_name%
GOTO :commonEnd
:9000
Echo valid 9000 %complete_name%
GOTO :commonEnd
:930
Echo valid 930 %complete_name%
GOTO :commonEnd
:310
Echo valid 310 %complete_name%
GOTO :commonEnd
:commonEnd

Related

How to reference the first parameter of a Windows batch file in an IF condition?

I'm very new in cmd batch files. I have code:
#echo off
if {%1} =="1" (
goto 1cmd
)
if {%1} =="2" (
goto 2cmd
)
if {%1} =="3" (
goto 3cmd
)
if {%1} =="" (
echo qwerty
)
:1cmd
call D:\test\1\1.cmd
goto end
:2cmd
call D:\test\2\2.cmd
goto end
:3cmd
call D:\test\3\3.cmd
goto end
:end
File is named a.bat. No matter what parameter I type, a.bat always calls 1.cmd.
What is the reason?
Is this working ?
#ECHO OFF
if "%~1" =="1" (
goto 1cmd
)
if "%~1" =="2" (
goto 2cmd
)
if "%~1" =="3" (
goto 3cmd
)
if {%1} =="" (
echo qwerty
)
exit /b 0
:1cmd
call D:\test\1\1.cmd
goto end
:2cmd
call D:\test\2\2.cmd
goto end
:3cmd
call D:\test\3\3.cmd
goto end

Windows batch if-else not working

[simple program that recieves an integer as input and prints if that number is trivial or not]
when i run this i get an error "( was unexpected at this time"
#echo off
set /a i=2
set /p input="enter an integer: "
set /a n=input
set /a t=n/2
:loop1
if %t% LSS %i% (
goto trivial
) else (
set /a t0=n%i
if %t0%==0 (
goto notTrivial
) else (
set /a i=i+1
goto loop1
)
)
:trivial
echo %n% is trivial
goto endd
:notTrivial
echo %n% is not trivial
:endd
pause > nul
but when I remove else statement in loop1 (which is btw unnecessary (because of goto command in if block)) it works
:loop1
if %t% LSS %i% (
goto trivial
)
set /a t0=n%i
if %t0%==0 (
goto notTrivial
) else (
set /a i=i+1
goto loop1
)
(how) is this possible?
When you remove the else clause, the code inside it is now out of any block.
Why does it matter? Because in batch files, lines or blocks of lines (code inside parenthesis) are first parsed and then executed. While parsed variable read operations are removed, being replaced with the value inside the variable at parse time, before starting to execute the command (more here).
So, in this code
) else (
set /a t0=n%i
if %t0%==0 (
goto notTrivial
) else (
set /a i=i+1
goto loop1
)
)
you change the value of the variable t0, but you can not retrieve this changed value inside the same block. But if you remove the else clause the code is not inside a block and everything works as intended (except syntax errors, try with set /a "t0=n %% i").
Firstly, you need to state the modulo operator % as %% in batch files.
Secondly, just move the command set /a t0=n%%i up before the if block begins, then it will work:
:loop1
set /a t0=n%%i
if %t% LSS %i% (
goto trivial
) else (
if %t0% EQU 0 (
goto notTrivial
) else (
set /a i+=1
goto loop1
)
)
So the change of variable t0 is moved outside of a command block ().
Alternatively, you could also enable delayed expansion:
setlocal EnableDelayedExpansion
rem INITIAL CODE PORTION...
:loop1
if %t% LSS %i% (
goto trivial
) else (
set /a t0=n%%i
if !t0! EQU 0 (
goto notTrivial
) else (
set /a i+=1
goto loop1
)
)
rem REMAINING CODE PORTION...
endlocal
You will notice the !t0! type expansion which, in contrast to %t0%, will expand t0 at execution time rather than parse time.
See also setlocal /? and endlocal /? for more information about these commands.

How to branch if-statements in batch

I've just started learning Batch, although I can't figure out the syntax for branching if-statements inside other if-statements. As in,
#echo off
set first=
set second=
set /P first=first?: %=%
if /I "%first%"=="y" (
echo a.1
set /P second=second?: %=%
if /I "%second%"=="y" (
echo b.1
) else if %second%=="n" (
echo b.2
) else (
echo b.3
)
) else if /I "%first%"=="n" (
echo a.2
) else (
echo a.3
)
pause
Any help with that would be appreciated.
Your IF logic is perfectly fine. Your problem is you need delayed expansion. %second% is expanded when the statement is parsed, and the entire complex IF logic is parsed in a single pass. So %second% expands to the value that existed before the IF statement is executed, which in your case is an empty string.
The solution is to enable delayed expansion and then use !second! to expand the value. Delayed expansion occurs after the statement is parsed.
#echo off
setlocal enableDelayedExpansion
set first=
set second=
set /P first=first?: %=%
if /I "%first%"=="y" (
echo a.1
set /P second=second?: %=%
if /I "!second!"=="y" (
echo b.1
) else if "!second!"=="n" (
echo b.2
) else (
echo b.3
)
) else if /I "%first%"=="n" (
echo a.2
) else (
echo a.3
)
pause

Batch script not running

can you help me please?
This piece of script is not running and i can't figure why.
#echo off
ver | findstr /i "5\.1\."
if %ERRORLEVEL% EQU 0 (
set os_ver="xp"
)
ver | findstr /i "6\.1\." > nul
if %ERRORLEVEL% EQU 0 (
set os_ver="7"
)
if %os_ver% == "xp" (
set os_bits="32"
)
if %os_ver% == "7" (
if %PROCESSOR_ARCHITECTURE% == "x86" (
set os_bits="32"
) else (
set os_bits="64"
)
echo %os_bits%
)
pause
it doesn't echo anything despite "ECHO IS DEACTIVATED" or "ECHO IS ACTIVATED"
update:
I posted the entire code, beacause people are saying that it is working
Update2:
I'm on a Windows 7 64 bits
The batch script appears to work fine. It will not echo if your machine is not Windows 7 though.
Try the following:
#echo off
set os_ver="unknown!"
set os_bits="unknown!"
ver | findstr "5\.1" > nul
if %ERRORLEVEL% EQU 0 (
set os_ver="xp"
)
ver | findstr "6\.1" > nul
if %ERRORLEVEL% EQU 0 (
set os_ver="7"
)
if %os_ver% == "xp" (
set os_bits="32"
)
if %os_ver% == "7" (
if %PROCESSOR_ARCHITECTURE% == "x86" (
set os_bits="32"
) else (
set os_bits="64"
)
)
echo os_ver = %os_ver%
echo os_bits = %os_bits%
pause
Update: commenter eryksun has provided the correct reason why this code works while the ops does not even though he is on Windows7. (Good catch)
In the OP's version echo %os_bits% is executed within the same command that sets os_bits. At the time the command is parsed and dispatched os_bits doesn't exist (unless it was already set), so it's just executing echo, which prints whether echo is on or off. – eryksun
Try this:
#echo off
setlocal
if "%PROCESSOR_ARCHITECTURE%" equ "x86" (
set os_bits="32"
) else (
set os_bits="64"
)
echo %os_bits%

Strange behavior of batch file

I have two cmd files.
child.cmd:
#echo off
exit 1
parent.cmd:
#echo off
cmd /C child.cmd
if %errorlevel% EQU 0 (
echo OK
) else (
echo ERROR
)
If to run parent.cmd, then ERROR will be printed.
But if a little change parent.cmd, then OK will be printed:
#echo off
if "YES" EQU "YES" (
cmd /C child.cmd
if %errorlevel% EQU 0 (
echo OK
) else (
echo ERROR
)
)
Why OK is printed in the second example?
inside a code block you need delayed expansion to access %variables%:
#echo off &setlocal enabledelayedexpansion
if !errorlevel! EQU 0 (
You can also use this syntax without delayed expansion:
if errorlevel 1 if not errorlevel 2 ( echo error )

Resources