Solved Use Batch File to Extract XML Tag to Batch Variable

September 19, 2012 at 19:44:47
Specs: Windows 7
If someone can Please help, I just need the Data in the XML tag extracted to Batch File Variable....the rest i figured out and works great for the backup...just need the extracted tags...

thank you

i have a parent directory that contains a sub directory and a XML file that i need a batch to read 2 specific tags then create a Set command in the batch with those 2 lines to create a backup folder with the tags as the names and such. looks like this:

Parent Directory = XML

XML Folder contains > WJFKB02229001.D Folder & Config.xml File

I need to read the contents of the xml "Config.xml" extract the PhaseID Tag data "WJFKB02229001"

Also need to extract the BoxID "D", I don't need anything else on these seperate lines.

keep trying the following For /F command but no luck


for /f "tokens=1 delims=[" %%G in ("PHASEID" %path%\%myfile%) DO (
set PhaseID=%%G
echo Phase: %PhaseID%


and i've searched so many sites and forums with no real help on this one....the data is contained in the xml, weird enough i guess that it's enclosed in brackets "[", which all the forums i read and try it either replies with a blank variable being passed or says it can't find the file at all....



Set PhaseID="Extracted PhaseID from xml"
Set BoxID="Extracted BoxID from xml"
Set Backup=%homedrive%\Backup\%phaseID%\%BoxID%


MD -p %Backup%
if exist %Backup% echo Created %Backup%
echo Starting Transfer...
ping -n 3>nul


xcopy "%homedrive%\*\phase\ptls\data\xml" "%Backup%" /E /Y /V /Q /X

if exist "%Backup%\Config.xml" echo Transfer Verified and Complete

else echo Transfer Failed to Copy Config File!!!

ping -n 3>nul
echo Exiting
ping -n 2>nul



<?xml version="1.0" encoding="UTF-16"?>
<![CDATA[9/18/2012 4:59:08 PM]]>


See More: Use Batch File to Extract XML Tag to Batch Variable

Report •

September 19, 2012 at 20:54:20
✔ Best Answer
maybe work with this:

@echo off & setlocal
for /f "tokens=1,2 delims=[]" %%a in ('find /i /n "<phaseid>"^<test.xml') do (
set f=%%a
for /f "skip=%f% tokens=1,3 delims=[]" %%x in (test.xml) do (
if /i "%%x" equ "</boxid>" goto :fin
if "%%y" neq "" (
if not defined phase (set phase=%%y) else (set boxid=%%y)
echo phase:%phase%
echo box:%boxid%
::====== end

if you need more help, just holler.

Report •

September 22, 2012 at 08:12:28
thank you for the response however that didn't work. it runs without error however the output is blank.

i tried adding more delimiters and changed the token to 7 and was able to extract the single tag however this config file is automatically generated and will rotate and change the tag data enclosed as such with this i was able to extract the "AH0417" however the next file with different tagid "CH0786" it removes the "C" from the variable. that was after placing the delimiters as:

tokens=7 delims=^<![CDATA[]] ^> "tried multiple different combos with all fail,
for /f "tokens=7,18-21* delims=^<![CDT[^>]], etc....

the tokens=1,2 delims=[]" %%a...... just passes blank data to the variables
%PhaseID% and %BoxID%

any other ideas..... i truly appreciate your help.

Report •

September 22, 2012 at 08:16:42
this is what i have used before for flat files "text files".... XML is my enemy at this point though, only because i am a straight newbie with it, lol.

for /f "tokens=* delims=" %%a in (%backupshare%\%machine%\users.txt) do (
set backuptarget=%backupshare%\%machine%\%%a
mkdir "%backupshare%\%machine%\%%a"

Report •

Related Solutions

September 22, 2012 at 10:51:22
I believe it is due to the exclamations in the xml. First, make sure you do NOT have
"enabledelayedexpansion" enabled.
if not, then the code I posted should work (it did in my tests).
If you want, you can do this and try:
setlocal disabledelayedexpansion
(this is, I think, the first time i've ever used this expression "disable...")
the var %%y will be visible inside the for-loop with this setting, it is not visible without this setting, but phase and box both get correct values once the loop is exited.
If you want the var.s to be global, remove the setlocal line entirely.
My code worked "as is" however, so maybe you can PM me both the code you are using and the xml file you are testing with. I cut/pasted your xml from your original post for my test data and it flew nicely. also tested it on my win-seven system just to be sure, and it worked there, too.

Report •

Ask Question