# Solved: Batch File Extract TAGID to Batch Variable? weird



## Cobra35y (Sep 19, 2012)

*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


```
[COLOR=red]for /f "tokens=1 delims=[" %%G in ("PHASEID" %path%\%myfile%) DO ([/COLOR]
[COLOR=red]set PhaseID=%%G[/COLOR]
[COLOR=red])[/COLOR]
[COLOR=red]echo Phase: %PhaseID%[/COLOR]
pause
```
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....

PLEASE HELP, lol


```
:StartSet
Set PhaseID="[COLOR=red][B][I]Extracted PhaseID from xml[/I][/B][/COLOR]"
Set BoxID="[COLOR=red][B][I]Extracted BoxID from xml[/I][/B][/COLOR]"
Set Backup=%homedrive%\Backup\[B][COLOR=red]%phaseID%\%BoxID%[/COLOR][/B]
 
:StartMD
MD -p %Backup%
if exist %Backup% echo Created %Backup%
echo Starting Transfer...
ping -n 3 127.0.0.1>nul
 
:StartXC
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 127.0.0.1>nul
echo Exiting
ping -n 2 127.0.0.1>nul
Exit
```


```
<?xml version="1.0" encoding="UTF-16"?>
<root>
     <CONFIG>
          <TAGID>
               <![CDATA[AH0417]]>
          </TAGID>
          <DATETIMESTAMP>
               <![CDATA[9/18/2012 4:59:08 PM]]>
          <SYSTYPE>
               <![CDATA[PTWS]]>
          </SYSTYPE>
          <PHASEID>
               <![CDATA[[COLOR=red][B]WJFKB02229001[/B][/COLOR]]]>
          </PHASEID>
          <BOXID>
               <![CDATA[[COLOR=red][B]D[/B][/COLOR]]]>
          </BOXID>
     </CONFIG>
</root>
```


----------



## Cobra35y (Sep 19, 2012)

i see views, but no replies, does anyone know how to get the data out of the xml file that i need as shown? it seems no one knows any method as i have had no suggestions for this extraction whether it be to use VB or another language to extract this.....

Thanks for any help in advance...


----------



## Squashman (Apr 4, 2003)

Cobra35y said:


> i see views, but no replies, does anyone know how to get the data out of the xml file that i need as shown?


Kind of a rhetorical question.

Based on the example you posted for your config.xml file this is what I would do.

```
@echo off
setlocal enabledelayedexpansion

set phaseflag=N
set boxflag=N

FOR /F "tokens=1-4 delims=[]^>^<" %%G IN (Config.xml) DO (
	IF !phaseflag!==Y (
		set phaseid=%%J
		set phaseflag=N
	)
	IF %%H==PHASEID set phaseflag=Y

	IF !boxflag!==Y (
		set boxid=%%J
		set boxflag=N
	)
	IF %%H==BOXID set boxflag=Y
)
echo phaseid: %phaseid%
echo boxid: %boxid%
```
Output

```
C:\Users\Squashman\Batch\XML>type Config.xml
<?xml version="1.0" encoding="UTF-16"?>
<root>
     <CONFIG>
          <TAGID>
               <![CDATA[AH0417]]>
          </TAGID>
          <DATETIMESTAMP>
               <![CDATA[9/18/2012 4:59:08 PM]]>
          <SYSTYPE>
               <![CDATA[PTWS]]>
          </SYSTYPE>
          <PHASEID>
               <![CDATA[WJFKB02229001]]>
          </PHASEID>
          <BOXID>
               <![CDATA[D]]>
          </BOXID>
     </CONFIG>
</root>
C:\Users\Squashman\Batch\XML>readxml.bat
phaseid: WJFKB02229001
boxid: D

C:\Users\Squashman\Batch\XML>
```


----------



## Cobra35y (Sep 19, 2012)

first thank you very much for helping!!!!!!!!!! YOU ARE FRREAKING AWESOME.... lol 

ok after seeing your output, thats what i need. also, after trying it and failing, i noticed your running XML from command prompt, i downloaded it, put a copy of xml and in system32 folder and in local folder where config.xml is, still fails.

if you can explain how you have your software setup in order for the XML to read and parse it to variables, greatly appreciate it. 

to include where and what this readxml.bat is that you ran in your test...?

when i try running XML>TYPE it gives me the help instead, but i copied and pasted what you have, not working on mine.

output is blank
and when i run with echo on just shows %G and so on with no data being passed.

thanks again for your help and your time


----------



## Cobra35y (Sep 19, 2012)

also FYI, this will be running on windows 7 x64.

and i have over 100 laptops that will need be using this script locally to send a backup folder to the network share folder.


----------



## Squashman (Apr 4, 2003)

The name of my batch file is *readxml.bat*. It is located in a sub directory named *xml*.
I just run it from the cmd prompt to make it easier to copy and paste the output.
*TYPE* is a command to output the contents of a text file to the screen. Was just showing you the contents of the Config.xml to prove that I used the example you posted in this thread.

```
C:\Users\Squashman\Batch\XML>dir /b
Config.xml
readxml.bat
```


----------



## Squashman (Apr 4, 2003)

Cobra35y said:


> also FYI, this will be running on windows 7 x64.


Then get rid of the antiquated PING pause and use the TIMEOUT command!


----------



## Cobra35y (Sep 19, 2012)

ok thanks now i get it. Thanks much, i will go test again


----------



## Cobra35y (Sep 19, 2012)

also, what xml are you running, i installed XMLStarlet.

when i type XML>type Config.xml
i get the help instead of any output....


----------



## Cobra35y (Sep 19, 2012)

D:\Users\APOLLO>xml>type config.xml
XMLStarlet Toolkit: Command line utilities for XML
Usage: xml [<options>] <command> [<cmd-options>]
where <command> is one of:
ed (or edit) - Edit/Update XML document(s)
sel (or select) - Select data or query XML document(s) (XPATH, etc)
tr (or transform) - Transform XML document(s) using XSLT
val (or validate) - Validate XML document(s) (well-formed/DTD/XSD/RelaxNG)
fo (or format) - Format XML document(s)
el (or elements) - Display element structure of XML document
c14n (or canonic) - XML canonicalization
ls (or list) - List directory as XML
esc (or escape) - Escape special XML characters
unesc (or unescape) - Unescape special XML characters
pyx (or xmln) - Convert XML into PYX format (based on ESIS - ISO 8879)
p2x (or depyx) - Convert PYX into XML
<options> are:
--version - show version
--help - show help
Wherever file name mentioned in command help it is assumed
that URL can be used instead as well.
Type: xml <command> --help <ENTER> for command help
XMLStarlet is a command line toolkit to query/edit/check/transform
XML documents (for more information see http://xmlstar.sourceforge.net/)


----------



## Squashman (Apr 4, 2003)

Cobra35y said:


> ok thanks now i get it. Thanks much, i will go test again





Cobra35y said:


> also, what xml are you running, i installed XMLStarlet.
> 
> when i type XML>type Config.xml
> i get the help instead of any output....


I am not using ANY XML APPLICATION. I told you that XML was the name of the directory that has my BATCH FILE and CONFIG.XML FILE. So apparently you don't understand.

*TYPE* IS a command used to display the contents of TEXT files to the console. What are you getting confused about. You don't need to run that command at all. I told you I was just displaying the contents of the config.xml file to the screen so that you could see I was using your example.


----------



## Cobra35y (Sep 19, 2012)

ok, my fault, i was stating "ok thanks now i get it." that was in reference to your batch file name.

look, i appreciate the help i really do, but there is absolutely no reason to be rude or condescending.

i simply misread your path. and the fact that XML was your folder and not a command line parser you were using. XMLStarlet has userdirectory> "XML -t" as part of the command line, thats why i became confused.

even still after running Your script.... i get nothing.
when i run TYPE CONFIG.XML
it does not show it like yours did. it shows it axactly as posted below.
and when running READXML.BAT, since i changed my batch filename to match everything you had....i removed the echo so i could show you what it is doing

it doesn't produce any result....


```
phaseid:
boxid:
D:\Users\APOLLO>type config.xml
<?xml version="1.0" encoding="UTF-16"?>
<root><CONFIG><TAGID><![CDATA[AH0417]]></TAGID><DATETIMESTAMP><![CDATA[9/18/2012 4:59:08 PM]]></DATETIMESTAMP><SYSTYPE><![CDATA[PTWS]]></SYSTYPE><PHASEID><![CDATA[WJFKB02229001]]></PHASEID><BOXID><![CDATA[D]]></BOXID></CONFIG></root>
D:\Users\APOLLO>readxml.bat
D:\Users\APOLLO>setlocal enabledelayedexpansion
D:\Users\APOLLO>set phaseflag=N
D:\Users\APOLLO>set boxflag=N
D:\Users\APOLLO>FOR /F "tokens=1-4 delims=[]^>^<" %G IN (Config.xml) DO (
IF !phaseflag! == Y (
set phaseid=%J
 set phaseflag=N
)
 IF %H == PHASEID set phaseflag=Y
 IF !boxflag! == Y (
set boxid=%J
 set boxflag=N
)
 IF %H == BOXID set boxflag=Y
)
D:\Users\APOLLO>echo phaseid:
phaseid:
D:\Users\APOLLO>echo boxid:
boxid:
D:\Users\APOLLO>
```
what OS are you running?
Thanks again for your help. Really, because your the first to take the challenge of this monster....lol


----------



## Squashman (Apr 4, 2003)

Condescending. No. Emphatic. Yes. There is a big difference.

I have two files in this directory path: C:\Users\Squashman\Batch\XML
The names of the two files inside that directory are:
readxml.bat 
config.xml

The contents of readxml.bat

```
@echo off
setlocal enabledelayedexpansion

set phaseflag=N
set boxflag=N

FOR /F "tokens=1-4 delims=[]^>^<" %%G IN (Config.xml) DO (
	IF !phaseflag!==Y (
		set phaseid=%%J
		set phaseflag=N
	)
	IF %%H==PHASEID set phaseflag=Y

	IF !boxflag!==Y (
		set boxid=%%J
		set boxflag=N
	)
	IF %%H==BOXID set boxflag=Y
)
echo phaseid: %phaseid%
echo boxid: %boxid%
```
The contents of config.xml

```
<?xml version="1.0" encoding="UTF-16"?>
<root>
     <CONFIG>
          <TAGID>
               <![CDATA[AH0417]]>
          </TAGID>
          <DATETIMESTAMP>
               <![CDATA[9/18/2012 4:59:08 PM]]>
          <SYSTYPE>
               <![CDATA[PTWS]]>
          </SYSTYPE>
          <PHASEID>
               <![CDATA[WJFKB02229001]]>
          </PHASEID>
          <BOXID>
               <![CDATA[D]]>
          </BOXID>
     </CONFIG>
</root>
```
When I execute readxml.bat my output is this.

```
C:\Users\Squashman\Batch\XML>readxml.bat
phaseid: WJFKB02229001
boxid: D
```


----------



## Squashman (Apr 4, 2003)

And now I know why the batch file does not work. You did not give an accurate example of what your XML file looked like.
Your first example said it looked like this.

```
<?xml version="1.0" encoding="UTF-16"?>
<root>
     <CONFIG>
          <TAGID>
               <![CDATA[AH0417]]>
          </TAGID>
          <DATETIMESTAMP>
               <![CDATA[9/18/2012 4:59:08 PM]]>
          <SYSTYPE>
               <![CDATA[PTWS]]>
          </SYSTYPE>
          <PHASEID>
               <![CDATA[WJFKB02229001]]>
          </PHASEID>
          <BOXID>
               <![CDATA[D]]>
          </BOXID>
     </CONFIG>
</root>
```
But when you run the TYPE command from the cmd prompt your file is really only TWO lines not NINETEEN lines.

```
D:\Users\APOLLO>type config.xml
<?xml version="1.0" encoding="UTF-16"?>
<root><CONFIG><TAGID><![CDATA[AH0417]]></TAGID><DATETIMESTAMP><![CDATA[9/18/2012 4:59:08 PM]]></DATETIMESTAMP><SYSTYPE><![CDATA[PTWS]]></SYSTYPE><PHASEID><![CDATA[WJFKB02229001]]></PHASEID><BOXID><![CDATA[D]]></BOXID></CONFIG></root>
```


----------



## Cobra35y (Sep 19, 2012)

i am the "jacked up twisted" newbie to xml, when i posted my config.xml file it was the exact layout from when you view it in IE.... if i right click and edit it opens in every program as that layout i gave, now when i copied it to a text file it still has "19" lines just not tabbed as in Visual Studio or IE. Never had it shown up in 2 lines like the output from TYPE.... So what method should be used to get the correct layout, as in what is the common layout for xml programmers?

didnt know there was a difference in the layout...Does that make a difference when? believe me when i say i have been "learning XML" this week as i never had such a task or need for it. now it looks like i'm jumping in head first and it's an empty pool, because there REALLY isn't any CLEAR guidance on XML it seems, way too many ways of working with XML which is awesome but it seems every individual person has almost their own method, So it seems though...

My other POST on same Script has more info possibly and you can see the layout as it was copied from XML file. maybe this helps...
http://stackoverflow.com/questions/12545488/batch-script-file-to-extract-xml-tag-data


----------



## Squashman (Apr 4, 2003)

I understand the concept of XML but in my job it wreaks havoc. Been doing data processing for about 12 years and our software can read in just about any file format except for XML. So I am not expert on manipulating XML data. There is not much more I can do for you if your entire XML data is sitting on one line. Batch files are not suited to manipulating TAG defined files.


----------



## Cobra35y (Sep 19, 2012)

ok well i definitely appreciate it. if its not possible with Batch...is it possible with VBS? I see it is possible with Java However i have no clue about Java Programming.... I NOW realize just how far behind languages i am.... If you don't ever need it in your line of work, you never really need to learn it.... NOW I AM GOING TO SIGN UP FOR SOME MORE COLLEGE CLASSES, Etc.... I want to know every language my brain is "willing" to learn, lol.


----------



## Squashman (Apr 4, 2003)

If your data is always in that same two line format we can use the square brackets to our advantage as a delimiter.

readxml.bat

```
@echo off

FOR /F "skip=1 tokens=12 delims=[]" %%G IN (Config.xml) DO set phaseid=%%G
FOR /F "skip=1 tokens=15 delims=[]" %%G IN (Config.xml) DO set boxid=%%G
echo phaseid: %phaseid%
echo boxid: %boxid%
```
Config.xml

```
<?xml version="1.0" encoding="UTF-16"?>
<root><CONFIG><TAGID><![CDATA[AH0417]]></TAGID><DATETIMESTAMP><![CDATA[9/18/2012 4:59:08 PM]]></DATETIMESTAMP><SYSTYPE><![CDATA[PTWS]]></SYSTYPE><PHASEID><![CDATA[WJFKB02229001]]></PHASEID><BOXID><![CDATA[D]]></BOXID></CONFIG></root>
```
Output

```
C:\Users\Squashman\Batch\XML>readxml.bat
phaseid: WJFKB02229001
boxid: D
```


----------



## Cobra35y (Sep 19, 2012)

will give it a check....

your a saint for sticking with this endeavor, i honestly think we might be getting somewhere, possibly answering alot of peoples unanswered questions lol....KUDOS.


----------



## Squashman (Apr 4, 2003)

A few years ago I was starting to learn Powershell I had written a script to read and write to an Excel file. Haven't done much with Powershell since but since all your computers are running windows 7 you might as well start learning it. Looks pretty easy to read an xml file in Powershell.
http://blogs.technet.com/b/heyscrip...owershell-to-easily-read-an-xml-document.aspx


----------



## Cobra35y (Sep 19, 2012)

ok per your advice i went with the powershell. disabled or rather allowed powershell to execute scripts unrestricted...

got the powershell script to easily!!!!!! extract the TAGID from the Config.xml... and it outputs fine in Powershell....

NOW, lol, does anyone know how to get the powershell script to execute FROM the batch file and OUTPUT the result from PS to BATCH file VARIABLE??????!

This is sooooo close to completion i am so excited!!!! Thank You Squashman very much!!!! Your a god even though we couldn't do it from windows BATCH.

this is my PS file. Named XML.PS1


```
[xml]$config1=get-content .\config.xml
foreach($tagid in $config1.root.config.tagid)
    {
    write-host $tagid.innertext
    }
```
OPUTPUT IS: 

```
PS C:\Users\Administrator> C:\Users\Administrator\XML.ps1
AH0417
```
i have now looked for forum threads and other sites for calling the PS1 from batch, but it is more common to Call a batch from PS it seems...

ANY TAKERS....?


----------



## Cobra35y (Sep 19, 2012)

IT's DONE!!!!! figured it out on the extracting the Powershell Output into Batch File Variable....
i wrote 3 separate PS Scripts so i can use them individually later in other scripts....

PowerShell Script TAG.PS1

```
[xml]$config1=get-content .\config.xml
foreach($tagid in $config1.root.config.tagid)
    {
    write-output $tagid.innertext
    }
```
PowerShell Script PHASE.PS1

```
[xml]$config1=get-content .\config.xml
foreach($phaseid in $config1.root.config.phaseid)
    {
    write-output $phaseid.innertext
    }
```
PowerShell Script BOX.PS1

```
[xml]$config1=get-content .\config.xml
foreach($Boxid in $config1.root.config.Boxid)
    {
    write-output $Boxid.innertext
    }
```
Windows Batch File *.bat

```
for /f "tokens=1" %%a in ('powershell.exe .\TAG.ps1') do set tagid=%%a
for /f "tokens=1" %%a in ('powershell.exe .\PHASE.ps1') do set phaseid=%%a
for /f "tokens=1" %%a in ('powershell.exe .\BOX.ps1') do set boxid=%%a
echo %tagid%
echo %phaseid%
echo %boxid%
timeout /t 3 /nobreak
 
Set Backup=%HOMEDRIVE%\Backup\%tagid%\%PhaseID%\%BoxID%
 
 etc..........
```


----------



## Squashman (Apr 4, 2003)

What do you mean we couldn't do it in batch. I did it twice based on the two different format variations of the xml file you provided as examples.


----------



## Cobra35y (Sep 19, 2012)

I agree, you were able to do it. However, I was not able and that was using your exact coding on 2 different computers. 1 running vista and the other running windows 7. You may have other programs installed on your system that may have allowed it to work. So for whatever reason it is that I am not able to produce the same results with your code is beyond me... I gave you kudos and named you a god in my book, as well as you provided an alternate method for which in my case works very well. I will eventually re-write everything I have into power shell as there are a lot of possibilities within its abilities.

My hat is off to you. 

Unless you want to post a list of everything you have installed to include windows updates and etc... To figure out what the differences are between your one system and my 2 separate systems, by all means. I will however be learning more about power shell as it has saved my butt a lot of time.


----------



## Squashman (Apr 4, 2003)

Cobra35y said:


> You may have other programs installed on your system that may have allowed it to work.


It is a pure batch file I provided to you. Has nothing to do with other programs I have installed on my computer. You should be able to look at the code and see that.


----------



## Squashman (Apr 4, 2003)

I know why now. Your XML file is UTF-16. The TYPE command will display a UTF-16 file correctly to the Console but batch cannot handle reading a UTF-16 file. So what most people do is use the TYPE command to first write their UTF16 file to a temporary file first because it will then output to a normal ASCII file that batch can then read.

So you could do this.

```
@echo off

type Config.xml>Config.tmp
FOR /F "skip=1 tokens=12 delims=[]" %%G IN (Config.tmp) DO set phaseid=%%G
FOR /F "skip=1 tokens=15 delims=[]" %%G IN (Config.tmp) DO set boxid=%%G
echo phaseid: %phaseid%
echo boxid: %boxid%
```


----------



## Cobra35y (Sep 19, 2012)

Squashman said:


> I know why now. Your XML file is UTF-16. The TYPE command will display a UTF-16 file correctly to the Console but batch cannot handle reading a UTF-16 file. So what most people do is use the TYPE command to first write their UTF16 file to a temporary file first because it will then output to a normal ASCII file that batch can then read.
> 
> So you could do this.
> 
> ...


YOU SIR ARE AMAZING, I didn't even notice that ...I feel so retarded.

 YOUR BATCH DOES WORK ON ALL SYSTEMS.... Thanks again for your help. Have a great week.


----------

