Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
psytae
- Posts: 9
- Joined: 02 Sep 2011 10:23
#1
Post
by psytae » 20 Jul 2012 08:40
When working directly with a cmd prompt this works.
Code: Select all
setlocal
set DOMAIN=test
ECHO %DOMAIN%
test
when trying to do something similar in a cmd file
Code: Select all
FOR /F "tokens=1,2,3,6" %%A in (output.txt) do (
:ECHO 1[%%A] 2[%%B] 3[%%C] 6[%%D]
IF "%%~D" == "" (
BREAK
) ELSE (
ECHO 6[%%D]
SET DOMAIN=%%D
ECHO %DOMAIN%
)
)
It returns this as output
Code: Select all
C:\downloads\Needed_Initial_Information>(IF "test" == "" (BREAK) ELSE (
ECHO 6[test]
SET DOMAIN=test
ECHO
) )
6[test]
ECHO is on.
Why is my ECHO %DOMAIN% always blank? How can I get my SET to take the variable?
-
psytae
- Posts: 9
- Joined: 02 Sep 2011 10:23
#2
Post
by psytae » 20 Jul 2012 08:48
I forgot to put this in my post.
The top two lines of my batch file contain.
Code: Select all
:@ECHO off
setlocal EnableExtensions EnableDelayedExpansion
-
psytae
- Posts: 9
- Joined: 02 Sep 2011 10:23
#5
Post
by psytae » 20 Jul 2012 09:13
Code: Select all
FOR /F "tokens=1,2,3,6" %%A in (C:\downloads\Needed_Initial_Information\output.txt) do (
:ECHO 1[%%A] 2[%%B] 3[%%C] 6[%%D]
IF "%%~D" == "" (
BREAK
) ELSE (
ECHO 6[%%D]
SET DOMAIN=%%D
ECHO !DOMAIN!
)
)
This did get it to work, I was trying all the different things I could think of on the set I wasn't doing it on the ECHO. Thank you.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 20 Jul 2012 09:18
psytae wrote:Code: Select all
FOR /F "tokens=1,2,3,6" %%A in (C:\downloads\Needed_Initial_Information\output.txt) do (
:ECHO 1[%%A] 2[%%B] 3[%%C] 6[%%D]
IF "%%~D" == "" (
BREAK
) ELSE (
ECHO 6[%%D]
SET DOMAIN=%%D
ECHO !DOMAIN!
)
)
This did get it to work, I was trying all the different things I could think of on the set I wasn't doing it on the ECHO. Thank you.
What is ironic about you saying that is the help for the SET command actually talks about delayed expansion.
Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:
set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%
in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%