IF statment question

Hewlett-packard / Sd2100
March 8, 2010 at 12:46:27
Specs: Windows 7, 1024

what would be the best way to put these in a IF statement

FINDSTR "1" "c:\test.txt" && set A=1
FINDSTR "2" "c:\test.txt" && set B=2
FINDSTR "3" "c:\test.txt" && set C=3

then depending what is found in the text file
call a batch depending on the variable that is set

IF '%A%'=='1' Call 1.bat
IF '%B%'=='2' Call 2.bat
IF '%C%'=='3' Call 3.bat

thank you all.

See More: IF statment question

Report •

March 8, 2010 at 13:11:11
for %%j in (1 2 3) do (FINDSTR "%%j" "c:\test.txt" && (call %%j & goto :DONE))

Report •

March 8, 2010 at 13:34:57
thank you that helped alot.

one last question.

since values the file will are 123 etc, how do you handle with different named batchs that the vaules 123 etc are tied to?

thank you

Report •

March 8, 2010 at 13:55:18
From your post I guessed you want to call batches whose names are exactly the string/char you are browsing inside the file. If you have to call a set of batches on each hit, the code depends on the batches involved.

More info is needed and as a rule post always exactly what you want to achieve otherwise you get useless hints.

Report •

Related Solutions

March 8, 2010 at 14:40:26
sorry about that IVO, was just curious.

for some reason your code works but errors says it cant find 1 etc says its not an internal or external command.

does the call %%J need an extention .bat im not sure.

thank you

Report •

March 9, 2010 at 02:23:10
Based on IVO's solution, but slightly different:

set values2check=1 2 3 4
set file2check=test.txt

for %%t in (%values2check%) do (
findstr "%%t" %file2check%
if NOT errorlevel 1 call %%t

Report •

March 9, 2010 at 04:04:15
If 1.bat, 2.bat and so on is not in the current directory you have to explicitely code the batch path, i.e. e.g."C:\My Scripts\%%j". The extension .bat is not needed excluding some very uncommon cases.

About tvc's script, it suffers the same plague about the called batch path.

Report •

March 9, 2010 at 09:06:15
Scripts 1.cmd, 2.cmd, 3.cmd, ...can be in PATH
Not likely though ;)

Report •

March 9, 2010 at 15:16:20
thank you guys, one last issue.

IVO your code works great.

but from some reason the findstr says it can open the file.

i run the batch with admin rights, CHDIR to the dir where the TEST.txt is at

and in your code

for %%j in (1 2 3) do (FINDSTR "%%j" "c:\test.txt" && (call %%j & goto :DONE))

but ur code says it can open the file test.txt why is this?

Report •

March 9, 2010 at 19:06:42
sorry the error said

FINDSTR cannot open c:\test.txt.

im kinda at a lost on this one because i was in the DIR C:\ because i CHDIR. still nothing.

thank you for your help guys!

Report •

March 10, 2010 at 02:21:40
The file needs to be there ... as simple as that

Report •

March 10, 2010 at 02:30:10
Sorry, English is not my mother tongue but your posts are not a model of clear meaning.

You said the called batches can not be loaded, but I don't see the full path in front of %%j as I suggested.

If you specify full paths you do not have to change the current directory and C:\test.txt seems fully defined.

Are you sure "test.txt" exists in the root directory on drive

Anyway be aware the chdir command changes the directory NOT the current drive. To change the drive you need to issue the /D switch

CD /D C:\test.txt

or issue a drive change directive C:

I don't think however this is the cause of your trouble.

Report •

Ask Question