Solved

FOR Loop within IF Statement - Not Reading Variable

Posted on 2015-02-09
11
78 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 69

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
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
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 54

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
 
LVL 24

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 69

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 69

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 54

Expert Comment

by:Bill Prew
ID: 40602107
Glad that helped.

~bp
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
avoiding the dashes in the sqlcmd output... 1 853
Batch script to delete folder content 3 107
Check Disk (CHKDSK) on all volumes and fix if needed. 8 213
robocopy 6 70
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…

733 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