FOR LOOP Issue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

FOR LOOP Issue

#1 Post by SIMMS7400 » 23 Apr 2017 14:07

Hi Folks -

Can you help me understand why this wont work?

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%A IN ( coreapplication_obips1 coreapplication_obiccs1 coreapplication_obisch1 coreapplication_obijh1 coreapplication_obis1 ) DO (

for /f "tokens=4 delims=|" %%B in ('findstr/r "^[\ \   ]*%%A" "C:\Hyperion_Batch\opmn.txt" ') do set "HYP_VAR=%%B"

echo %%A = %HYP_VAR% >> "C:\Hyperion_Batch\results.txt"

)


%HYP_VAR% remains empty. I assume it something do with variable expansion but I thought I took care of that.

Here is the file:

Code: Select all

Processes in Instance: instance1
---------------------------------+--------------------+---------+---------
ias-component                    | process-type       |     pid | status 
---------------------------------+--------------------+---------+---------
coreapplication_obips1           | OracleBIPresentat~ |    1328 | Alive   
coreapplication_obiccs1          | OracleBIClusterCo~ |   10220 | Alive   
coreapplication_obisch1          | OracleBIScheduler~ |    5400 | Alive   
coreapplication_obijh1           | OracleBIJavaHostC~ |    8852 | Alive   
coreapplication_obis1            | OracleBIServerCom~ |    5516 | Alive   


Thanks!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: FOR LOOP Issue

#2 Post by ShadowThief » 23 Apr 2017 14:18

Using delayed expansion means you'd use !HYP_VAR! instead of %HYP_VAR%.

You could also remove the need for delayed expansion at all by simply merging the two lines and not setting a variable at all.

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR %%A IN ( coreapplication_obips1 coreapplication_obiccs1 coreapplication_obisch1 coreapplication_obijh1 coreapplication_obis1 ) DO (

for /f "tokens=4 delims=|" %%B in ('findstr/r "^[\ \   ]*%%A" "C:\Hyperion_Batch\opmn.txt" ') do echo %%A = %%B >> "C:\Hyperion_Batch\results.txt"

)

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: FOR LOOP Issue

#3 Post by SIMMS7400 » 23 Apr 2017 14:19

God, I'm so stupid. Duh ,of course.

Thanks!

Post Reply