Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

windows batch scripting help

Status
Not open for further replies.

f0rmat

IS-IT--Management
Jun 2, 2004
102
US
I've been banging my head for hours and realize I need some help :)

I'm generally a linux guy, so scripting in windows gets me all foggy with syntax.

Here's what I need to do.
I have a text file called approved.txt. Within it, it has the following
group1
group2
group3

I need to parse this file, and check every line against an if/then block to assign a new variable. I then need to feed those new values to a different command. I tried the following
set tfgroup=""

for /F "delims=" %%a in (%approvallist%) do (
if &&a == "group1"
(
set tfgroup=group1002
%newcommand% %tfgroup%
)
if %%a == "group2"
(
set tfgroup=group1003
%newcommand% %tfgroup%
)
)

however the results I was getting seemed to indicate that there's a syntax problem with my if statement. (using @echo on)

---------
The syntax of the command is incorrect.

C:\> IF %a == "group1"

C:\>

If I comment out the 2 IF statements and simply do a echo %%a I get the results of my text file, so I assume my for loop is ok.

What is frustrating, is that I've used IF statements in batch scripts before, and previously IF %variable% == somestring DO something.

I've never actually tried to set the %%a from a for loop to a new variable, I've used it as iterations to complete other tasks.

Does anyone know where I'm going wrong? The fact that the error is calling out my IF statement really makes me suspect it
IF %a == "group1"

 
Hi,

I'm by no means an expert in batch, but I think you may need to use the delayed variable expansion.

Details can be found at:

I know I've had issues with this in my own batch files when nesting IFs inside FORs.

On a second point if %approvallist% contains quotes then you will need to escape them in the FOR loop with usebackq.

On a third point &&a doesn't mean anything to me, did you mean %%a on your first IF?

Hope this helps at least a little.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top