Solved

FOR Loop within IF Statement - Not Reading Variable

Posted on 2015-02-09
11
77 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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 53

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 53

Expert Comment

by:Bill Prew
ID: 40602107
Glad that helped.

~bp
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
hosts.ini- can it be made to block all websites? 14 81
netsh interface ip show interface in Windows XP 4 134
robocopy 6 66
Renaming with batch file 9 50
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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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