# Solved: CMD Batch File Variable Help



## psytae (Sep 2, 2011)

When working directly with a cmd prompt this works.

setlocal
set DOMAIN=test
ECHO %DOMAIN%
test

when trying to do something similar in a cmd file

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

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?


----------



## Squashman (Apr 4, 2003)

Because you are not using Delayed Expansion.
http://www.robvanderwoude.com/variableexpansion.php


----------



## psytae (Sep 2, 2011)

actually I am I did not show the entire file but here is the top two lines of the CMD file

ECHO off
setlocal EnableExtensions EnableDelayedExpansion


----------



## Squashman (Apr 4, 2003)

psytae said:


> actually I am I did not show the entire file but here is the top two lines of the CMD file
> 
> ECHO off
> setlocal EnableExtensions EnableDelayedExpansion


Well then let me rephrase my last statement.
Because you are not using Delayed Expansion correctly. Read the link I posted.


----------



## psytae (Sep 2, 2011)

So you are saying I have to put it like ECHO !DOMAIN! to get it to echo what was SET previosly.


----------



## psytae (Sep 2, 2011)

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!
)
)


----------



## Squashman (Apr 4, 2003)

In the future try to use CODE tags around your code. Makes it a lot easier to read a question when the code is separated.

```
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!
	)
)
```


----------

