Having an issue with for /F do

May 13, 2010 at 02:08:35
Specs: Windows 7
Hey guys I'm writing a batch script and i'm having a problem parsing a txt file and runing some if else statements. Hope I can get some help on this. The goal is to sort through a text file that looks litke this:
internet,bla;
internet,bla2;
media,bla;
media,bla2;

and put it in the correct variables stringed like this: bla,bla2,bla3,bla4, and so on...

Heres my code:

FOR /F "tokens=1,2 delims=," %%A IN (%workingdir%\packages.cfg) DO (
IF "%%A"=="internet" (
set orig=%inputinternet%
set inputinernet=%orig%%%B,
) ELSE (
IF "%%A"=="media"(
set orig=%inputmedia%
set inputmedia=%orig%%%B,
)
::Start Else Closes
)
:: Do Close
)


See More: Having an issue with for /F do

Report •

#1
May 13, 2010 at 03:15:43
You have to enable the "Delayed Expansion" to define then access environment variables inside For loops and parenthrsis. So start your script by coding

@echo off & setlocal EnableDelayedExpansion

Then mark the environment variables using the ! symbol instead of the conventional %, e.g.

set inputinternet=!orig!%%B,

Another advice: GOTO referring labels inside a For loop are forbidden as they break the control flow.

Report •

#2
May 13, 2010 at 03:55:57
Thanks for the fast reply, I made the changes, however i'm still getting the same error : ) was unexpected at this time.

Thanks


Report •

#3
May 13, 2010 at 06:23:28
Try removing the "comments" (which are actually labels) from
inside the loops.

Report •

Related Solutions

#4
May 13, 2010 at 06:27:17
It looks like the ( and ) are matching, but maybe the system decides not to read them the way you want 'em to ... maybe that's a hint to use better looking code ? Such code would mean less (or no) of those annoying spanning characters ... Batch code was never intended to be used C-style ...

Report •

#5
May 13, 2010 at 08:46:13
More specifically, the Command Processor doesn't like to see a closing bracket straight after a label.

Solution:

Either: replace "::" with "REM" which is the proper way to introduce a comment:

set inputmedia=%orig%%%B,
)
rem Start Else Closes
)
rem Do Close
)


Or: add a command on its own line after each label. It could be a command that does nothing, e.g.

set inputmedia=%orig%%%B,
)
::Start Else Closes
cd .
)
:: Do Close
cd .
)


Report •

#6
May 13, 2010 at 08:59:36
Hi klint,

Right on, as usual. I learned the hard way that :: or : would trip up a for loop. Never occurred to me to use REM.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#7
May 13, 2010 at 20:00:31
@klint: Y not just an empty "rem"? easier...

here's a weird one I stumbled onto:
For /L %%a in (1 1 5) do (
echo %L
@:huh?
)


you can "goto" these labels as well.
but wait, it gets weirder (hell, i was bored...)

For /L %%a in (1 1 5) do (
call :huh?
echo %L
@:huh?
)
echo interleaved output number ONE
goto :xx
echo (never shown)
:xx
echo interleaved output TWO


Report •

Ask Question