Solved

FOR Loop within IF Statement - Not Reading Variable

Posted on 2015-02-09
11
74 Views
Last Modified: 2015-02-10
Hi There,

I have a script:

IF "%_CMD%"=="ENABLE" (
	IF EXIST %_STATUS_FILE% (
		FOR /f "TOKENS=1,2 DELIMS==" %%i IN (%_STATUS_FILE%) DO CALL SET %%i=%%j
		ECHO Process - %STATUS%
		timeout 10
		EXIT 100
	) ELSE (
		ECHO STATUS=Process: %_PROCESS% %_ARG% started at %_BATCHDATE% %_BATCHTIME%>%_STATUS_FILE%
	)
) ELSE (
 DEL /F /Q %_STATUS_FILE%
)

Open in new window


So if the variable _CMD = ENABLE and the STATUS_FILE exists I want to loop through the STATUS_FILE and find the value of the STATUS variable in the STATUS_FILE!

The contents of the status file:

STATUS=Process call_test1 T2SE started at 20150209 1017

However, the value of the STATUS variable isn't "coming" through (Line 4)?

Any direction would be appreciated.

Thanks,
Mark
0
Comment
Question by:SuperLight
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 40599590
Try using EnableDelayedExpansion - see http://ss64.com/nt/delayedexpansion.html
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40599600
That is because %STATUS% is evaluated with the first parens it's contained in, so it's empty. You can use delayed expansion:
setlocal EnableDelayedExpansion
IF "%_CMD%"=="ENABLE" (
	IF EXIST %_STATUS_FILE% (
		FOR /f "TOKENS=1,2 DELIMS==" %%i IN (%_STATUS_FILE%) DO CALL SET %%i=%%j
		ECHO Process - !STATUS!
		timeout 10
		EXIT 100
	) ELSE (
		ECHO STATUS=Process: %_PROCESS% %_ARG% started at %_BATCHDATE% %_BATCHTIME%>%_STATUS_FILE%
	)
) ELSE (
 DEL /F /Q %_STATUS_FILE%
)

Open in new window

This expands variables as late as possible.
0
 

Author Comment

by:SuperLight
ID: 40599603
Hi Lee,

Sorry I should have posted up the full code where I use this command:

SET _CMD=%1
SET _PROCESS=%2
SET _ARG=%3

SET _STATUS_FILE="%~dp0%_PROCESS%_%_ARG%.status"

SETLOCAL ENABLEDELAYEDEXPANSION

IF "%_CMD%"=="ENABLE" (

	IF EXIST %_STATUS_FILE% (
		FOR /f "TOKENS=1,2 DELIMS==" %%i IN (%_STATUS_FILE%) DO CALL SET %%i=%%j
			ECHO Process - %STATUS%
			timeout 10
			EXIT 100
	) ELSE (
		ECHO STATUS=Process %_PROCESS% %_ARG% started at %_BATCHDATE% %_BATCHTIME%>%_STATUS_FILE%
	)
) ELSE (
 DEL /F /Q %_STATUS_FILE%
)

Open in new window


I have used both %STATUS% and !STATUS! and no joy.

Thanks,
Mark
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40599745
I'd say firstly get rid of the word "CALL" in the For statement.

Also where you have "CALL" make it "ECHO" then you can have it show to the screen the commands it would run for the SET.

Steve
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40599757
Give this a try, should work.

@ECHO OFF

SET _CMD=%1
SET _PROCESS=%2
SET _ARG=%3

SET _STATUS_FILE="%~dp0%_PROCESS%_%_ARG%.status"

SETLOCAL ENABLEDELAYEDEXPANSION

IF "%_CMD%"=="ENABLE" (

	IF EXIST %_STATUS_FILE% (
		FOR /f "USEBACKQ TOKENS=1,2 DELIMS==" %%i IN (%_STATUS_FILE%) DO SET %%i=%%j
		ECHO Process - !STATUS!
		timeout 10
		EXIT 100
	) ELSE (
		ECHO STATUS=Process %_PROCESS% %_ARG% started at %_BATCHDATE% %_BATCHTIME%>%_STATUS_FILE%
	)
) ELSE (
 DEL /F /Q %_STATUS_FILE%
)

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:NVIT
ID: 40599769
Try removing the quotes from:
SET _STATUS_FILE="%~dp0%_PROCESS%_%_ARG%.status"
to
SET _STATUS_FILE=%~dp0%_PROCESS%_%_ARG%.status
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40600350
Agree, using Bill's or NVIT's suggestion should allow to read the file.
The issue with your code is that surrounding the filename with double quotes makes it a string in FOR /F, not a file name anymore. And the string does not contain the var you want to scan for ...
0
 

Author Closing Comment

by:SuperLight
ID: 40601759
Hi Bill,

Thanks very much!  It was the USEBACKQ I was missing.  Do you have a layman's description of what this does?

Mark
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40601789
http:#a40600350 describes why it did not work.
Adding UseBackQ to FOR changes the meaning of " to be a delimiter for file paths (instead of literal strings).
0
 

Author Comment

by:SuperLight
ID: 40601875
Thanks!
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40602107
Glad that helped.

~bp
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now