Using FOR to check for chars in variable

Microsoft / Dos 6.1
June 29, 2010 at 08:04:11
Specs: Windows 7 Ultimate, 4GB DDR2
Hello, I need a command that will check if a variable's string already exists in another variable.


SET /A newMR=%random%
FOR /F %%R IN (%MYMR%) DO (
if exist %newMR% in %MYMR% goto :MR
:: I [kinda] know the correct syntax of FOR but I don't know
:: what to put in "DO" for my command to work.



goto :MR

See More: Using FOR to check for chars in variable

June 29, 2010 at 08:35:55
If you prefix all of your variable names with an uncommon
character, such as _, the SET command could be used in a
FOR loop, similar to this:

SET _var1=value1
SET _var2=value2
SET _other1=value3
SET _other2=value4

ECHO All program variables defined:
FOR /F "tokens=1* delims==" %%a IN ('SET _') DO (
            ECHO %%a = %%b

ECHO All "_var" variables defined:
FOR /F "tokens=1* delims==" %%a IN ('SET _var') DO (
            ECHO %%a = %%b

ECHO All "_other" variables defined:
FOR /F "tokens=1* delims==" %%a IN ('SET _other') DO (
            ECHO %%a = %%b

It's not a complete answer to your question, but add in an IF
or two, you should be able to get to the solution! :-)

Edit: Added in subset examples for "_var" and "_other"...

Insomniac at large

Report •

June 29, 2010 at 09:39:07
Thanks but I don't think that solves my question, since the IFs and how to state them are the problem =\...

Report •

June 29, 2010 at 10:12:24
Try this then:


:: Create example variables _Var1 - _Var9, with values 1-9:
FOR /L %%a IN (1,1,9) DO (
    SET _Var%%a=%%a

:: Set a different variable to a matching value:
SET _NewVar=6
ECHO _NewVar = %_NewVar%

::Check the new variable's value against the other variables value:
FOR /F "tokens=1* delims==" %%a IN ('SET _Var') DO (
    IF "%_NewVar%" EQU "%%b" (
        ECHO _NewVar = %%a = %%b
    ) ELSE (
        ECHO %%a = %%b


Insomniac at large

Report •

Related Solutions

June 29, 2010 at 17:14:09
The setup is kind of confusing. %random% is a number 0 to 32767. Appending that to a string of other randoms leaves no means to verify if the same number has already been used, since the random numbers are not padded or delimited.
If you have build from: 0,2,316 and %random generates 31,
it will think 31 is already used, or if it generates 23, it will thing it's already used. If you want unique numbers, you prob'ly have to delimit them, like: 0,2,316. Then to test each newly generated random: set test=!mymar:,%newmr%=!
if %test% equ %mymar% echo %newmar% NOT USED YET
then when you're ready to use the variable, just nix the comma-delimiters: set mymar=%mymar:,=%

Report •

July 1, 2010 at 08:39:28
Ok thanks for the replies but I guess I didn't express my self well, this is the formula for a blackjack game I'm creating, so I need to check if the computer/player has already received a card, I ALMOST figured out what I want, I just need help to check if "%newMYRAN%" already exists in %MYCARDS%".

This is the current code:

IF "%MYRAN%" LEQ "14" (SET /A newMYRAN=%random%*12/32767+1)
Type %MYCARDS% | Find %newMYRAN%
IF "%%N" EQU "%newMYRAN%" (SET /A newMYRAN=%random%*12/32767+1) & (GoTo :MYRAN)





::Could someone please explain what exactly is "%%N"? Isn't it the result of the commands inside "DO"? I guess I'm completely lost =\

Report •

July 2, 2010 at 11:41:51
no, %%n is the result of the commands inside the () after the word "in". (recommended reading: for /? , set /? , if /? , setlocal /?)
what your code has is reading a file whose name is in the var %mycards%, one line at a time, which could work if you echoed new cards out to the file, but that's prob'ly not what you had in mind.
I would back up and redo from start along this line:
:player 1 is the human, player 2 is the computer
:maybe use zero for cards dealt and used in prior hands.
:: deal hole-card (1), then deal up-card (2)
for %%b in (1 2) do
:: deal each player (1,2) one card
for %%a in (1 2) do (
call :deal %%a %%b
goto :play
set /a suit="%random% %% 4"
set /a card="%random% %% 13"
if defined deck%suit%%card% goto :deal
set deck%suit%%card%=%1%2
goto :eof
::------- end snippet
to view a hand (player is sent via parm #1):
call viewhand %player%
goto :eof
set /a lower=%1*10
set /a upper=lower+1
for /f "tokens=2 delims==" %%a in ('set deck') do if %%a geq %lower% if %%a leq %upper% echo %%a
::---- end snippet

Report •

July 11, 2010 at 15:12:31
Hey, sorry for taking so long to reply, the more I try to solve things the more errors I get lol.

All I'm trying to do is search a number inside a string using 'for'.

Q1: Was this "set /a suit="%random% %% 4"" an actual command? I tried using it and it didn't work.

Q2: I just found out I need to enclose the variable in single quotes for it to not be considered a

file, but is it possible to use it in other commands such as 'find'/'findstr'?? Do I really need to

echo it to a file to achieve that =/? Could I 'clip' it and make 'find'/'findstr' search Windows

Clipboard? Is the only way out to compare the last variable to all the other ones?

Q3: When I was adding the suits into the game I decided to give every card an specific value [1-52],

but when I tried to use "IF GTR" to compare it such as:

SET var=8
IF "%var%" GTR "13" SET SUIT=C

It returned as true?? Do I need to use the same number of characters on both strings like "08" and "13" for the command to work? That would complicate things up =/...

Report •

July 11, 2010 at 19:28:49
ok, for Q1. did you run that from inside a batch or from the commandline? if you ran it from the cmdline, you need to make it: set /a suit="%random% % 4"
(pls no offense if you already know: % is modulo)
Q2: no no no, you don't have to do all that gyrations.
in for /?, you will notice: single quotes denotes a child-process, double quotes (sometimes, thanks to MS stupidity)
means a literal string, and no quotes means a file.
Thanks to MS for allowiing spaces into dir-struct, you now have
the added confusion: files that have spaces must be dbl-quoted, so then the geniuses added the usebackq option to
diffrentiate between a string literal and a quoted filename.
1) a child-process subcommand:
for /f %%a in ('dir /b') do echo %%a
2) a text-file content:
for /f %%a in (file) do echo %%a
3) a literal:
for /f %%a in ("this is a test") do echo %%a
4) a quoted filename:
for /f "usebackq" %%a in ("spaces in filenames suck!!!") echo %%a
(gives same as #2, but allows the damn spaces to exist)
Q3: lose the dbl-quotes. this will treat the"if" comparison as numeric instead of string. Fwiw: never put zero(s) in front of numbers when doing math or "if", unless you specifically intend it to be octal (08, 09 don't exist in octal, 10=8 in octal).
always trim leading zeros before doing math (set /a).
Once again, another "genius" blessing from MS - they could easily have used another character than zero to denote octal,
but NO!!! that might make things sufferable here-below!!!

"All I'm trying to do is search a number inside a string using 'for'."

depends how you have your string set up: delimited (1,12,8) or fixed-length field ( 1 12 8)
either way is easy enough using either "for" or, even easier, replacement-testing:
set test=!cards:%thiscard%=!
if !cards! equ !test! echo not in string
Hth :-)

Report •

Ask Question