1.

Solve : For loops and If?

Answer»

I'm not quite sure if this will work, but it works in theory.

Here is what I have:
Code: [Select]:loop
set /p name=Name^:
for /f %%a in ("I\_Common\Unknown_Folder\Names") do (
if %name%==%%a echo Name exists! & pause > nul & goto loop
)
rest of code here
Now, I think this will work, but I got one of those feelings that it doesnt...can someone prove one thought right and the other wrong?Looks good.

Set /p is fine.
And your for loop is looking good.

No Problems that I found.QUOTE from: macdad- on April 16, 2009, 05:23:41 PM

Looks good.

Set /p is fine.
And your for loop is looking good.

No Problems that I found.
Thanks!

But now I have a different problem, I have a list of (possibly over) 30 names (first and last).

The user is supposed to enter their first name and last name. If the first and last names match, then the script continues, but if the first and last name matches NONE of the name combinations(each name combonation has it's own line and it needs to match both) then it displays a message. But if I use if and else, then will it repeat for each line?First how do you have the files in the Names folder laid out.

Like this:
Sam Sample.txt?

Or are all the names in one FILE?
Quote from: macdad- on April 16, 2009, 08:02:36 PM
First how do you have the files in the Names folder laid out.

Like this:
Sam Sample.txt?

Or are all the names in one file?

All the names are in one file. It's called list.txt.alright, here's your code:

Code: [Select]@echo off
cd <location of the list.txt file>
echo First Name?
set /p first=?
echo Last Name?
set /p last=?
findstr /I %first%" "%last% list.txt
pause

You can drop the /I switch, I just added it in there in the case that the name's case was wrong(Like: sAM)

Hope this helps
,Nick(macdad-)Quote from: macdad- on April 18, 2009, 03:35:14 PM
alright, here's your code:

Code: [Select]@echo off
cd <location of the list.txt file>
echo First Name?
set /p first=?
echo Last Name?
set /p last=?
findstr /I %first%" "%last% list.txt
pause

You can drop the /I switch, I just added it in there in the case that the name's case was wrong(Like: sAM)

Hope this helps
,Nick(macdad-)
Ok...It doesn't SEEM to work...It just keeps on asking for the first and last names...But I think I could do this with 2 for loops...I'll post the answer if I get it.

My code is, and I think it should work...but it's not not accepting wrong names...

Code: [Select]@echo off
set num=0
set num2=0
set fname=list.txt
:loop
cls
set /p first=First name^:
set /p second=Second name^:
for /f "delims= " %%a in ("%fname%") do set /a num+=1
for /f "tokens=1-2 delims= " %%b in ("%fname%") do (
if /i "%first%"=="%%b" if "%second%"=="%%c" goto correct
set /a num2+=1
if %num%==%num2% echo Your name is not REGISTERED^!& pause > nul & goto loop
pause
)
:correct
echo Your name is registered, please continue.
pauseDid you replace:
Code: [Select]<location of the list.txt file>
with the actual location of the file?Quote from: macdad- on April 18, 2009, 04:35:44 PM
Did you replace:
Code: [Select]<location of the list.txt file>
with the actual location of the file?
They're both in the same directory, so I just REMOVED the CD part.


Discussion

No Comment Found