Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Assign DOS command output to variable

Reply  
Thread Tools
lizb's Avatar
Junior Member with 1 posts.
 
Join Date: Jan 2010
19-Jan-2010, 01:12 AM #1
Assign DOS command output to variable
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's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
19-Jan-2010, 10:16 AM #2
Welcome to TSG!

When using variables inside a For loop, you need to use Delayed Expansion.
Try this:
Code:
@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

Jerry
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions


Last edited by TheOutcaste; 19-Jan-2010 at 10:50 AM.. Reason: Fixing my Pre-coffee errors
Reply

Tags
batch output

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:39 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.