# Assign DOS command output to variable



## lizb (Jan 19, 2010)

Hi guys,

I need some help with a batch file I'm writing. It takes each XML file stored in a folder, validates it using Altovaxml, stores the output of the validation to a variable and prints out the output. Here's my code:

@echo off 

for /f %%a in ('dir/b SourceXML\*.xml') do (
altovaxml -validate SourceXML\%%a > tmpFile
set /p validateOutput= < tmpFile
del tmpFile 
@echo %validateOutput%
)

My problem is the output I got for each of the XML files is the same - "The data isn't well-formed." when I know that I only have one XML that should be producing this output.
The strange thing is when I take out the for loop and hardcode the XML file names in place of %%a the output is correct. I don't know what's wrong with my for loop. Can someone help me to get the right output without hardcoding the XML file names please?

Thanks in advance!


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

When using variables inside a For loop, you need to use Delayed Expansion.
Try this:

```
@Echo Off
Setlocal EnableDelayedExpansion
For /F "Tokens=*" %%a In ('Dir /A-D /B SourceXML\*.xml') Do (
altovaxml -validate "SourceXML\%%a">tmpFile
Set /P validateOutput=<tmpFile
Del tmpFile
@Echo !validateOutput!
)
```
Type *Set /?* in a Command Prompt for info on Delayed Variable Expansion

EDIT: Would be best to put quotes around the file name, just in case any names have spaces in them, as Squashman just reminded me. And can't hurt to exclude directory names. And specify the Tokens option so names with spaces don't get chopped. Thanks Squashman:up:

Jerry


----------

