?
Solved

FOR Loop within IF Statement - Not Reading Variable

Posted on 2015-02-09
11
Medium Priority
?
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 96

Expert Comment

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

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 56

Accepted Solution

by:
Bill Prew earned 2000 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 25

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 70

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 70

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 56

Expert Comment

by:Bill Prew
ID: 40602107
Glad that helped.

~bp
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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