• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 88
  • Last Modified:

FOR Loop within IF Statement - Not Reading Variable

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
SuperLight
Asked:
SuperLight
  • 3
  • 3
  • 2
  • +3
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Try using EnableDelayedExpansion - see http://ss64.com/nt/delayedexpansion.html
0
 
QlemoC++ DeveloperCommented:
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
 
SuperLightAuthor Commented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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
 
NVITCommented:
Try removing the quotes from:
SET _STATUS_FILE="%~dp0%_PROCESS%_%_ARG%.status"
to
SET _STATUS_FILE=%~dp0%_PROCESS%_%_ARG%.status
0
 
QlemoC++ DeveloperCommented:
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
 
SuperLightAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
SuperLightAuthor Commented:
Thanks!
0
 
Bill PrewCommented:
Glad that helped.

~bp
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now