Solved Comparing two Values with batch and extract them (XML Files)

April 6, 2017 at 03:52:50
Specs: Windows 7
Hey everyone,

I'd need a batch script(or a way how to do) which runs through manyXML files but only extracts them into txt.file if two values are matching. I'am new to XML and batching.. so a if clause would really do help me out here.

Only extract if:
1. Value would be "Name=Ian"
2. Value would be "Postition=Executive"

if these values are matching I need the data from them which are:
1.currentWork
2.shortID
3.holidaycounter

I hope it won't be too much work. XML Datas are all similar.
<Name="Ian" workstampIN="2017-04-06" worknumber="XXXX" editor="XXXX" Position="Executive>
etc.


See More: Comparing two Values with batch and extract them (XML Files)

Report •

✔ Best Answer
April 7, 2017 at 22:07:17
glutton for punishment, I guess...
::========= begin batch-script
@echo off & setlocal

:: the following line defines the variables you want to collect
set selected=currentwork shortid holidaycounter

:: begin main xml-file qualifications
for /f "tokens=1 delims=:" %%a in ('findstr /i "name=\"ian\"" *.xml ^| findstr /i "position=\"executive\""') do (
set fil=%%a
for /f "tokens=* delims=<>" %%b in (%%a) do (
call :xx %%b
set fil=
)
)
goto :eof

:xx
if defined fil echo xml file: %fil%
:aa
set test=%1
if not defined test goto :donethis
if %test% equ %~1 (set var=%1) else (if defined var call :bb %var% %test%)
shift
goto :aa

:bb
set %1=%~2
set var=
goto :eof

:donethis
for %%a in (%selected%) do (
rem ========= OUTPUT SECTION based on selected variable names
if defined %%a set %%a
set %%a=
)
goto :eof



#1
April 6, 2017 at 04:03:34
gimme a sec, making a test version

edit: taking longer than expected :O

give this one a try:

::------------start of test.bat-------------
@echo off&setlocal EnableDelayedExpansion&set n=0&set m=0&set f=0&set c=0&set d=0
set vars=3
set var1=currentWork
set var2=shortID
set var3=holidaycounter
for /f "tokens=4" %%A in ('dir /4 ^| find ".xml"') do set /a f+=1
:skip1
set /a c+=1
set /a d=!c!-1
if !d! equ 0 for /f "tokens=3,*" %%A in ('dir /4 ^| find ".xml"') do set "file!c!=%%B"&goto indexing
for /f "skip=%d% tokens=3,*" %%A in ('dir /4 ^| find ".xml"') do set "file!c!=%%B"&goto indexing
:indexing
set file=!file%c%!
set /a m+=1
set temp=!var%m%!
for /f "tokens=1-26 delims== """ %%a in ('type "%file%" ^| findstr "Ian" ^| findstr "Executive" ^| find "%temp%"') do (
if '%%a'=='%temp%' set val!m!=1
if '%%b'=='%temp%' set val!m!=2
if '%%c'=='%temp%' set val!m!=3
if '%%d'=='%temp%' set val!m!=4
if '%%e'=='%temp%' set val!m!=5
if '%%f'=='%temp%' set val!m!=6
if '%%g'=='%temp%' set val!m!=7
if '%%h'=='%temp%' set val!m!=8
if '%%i'=='%temp%' set val!m!=9
if '%%j'=='%temp%' set val!m!=10
if '%%k'=='%temp%' set val!m!=11
if '%%l'=='%temp%' set val!m!=12
if '%%m'=='%temp%' set val!m!=13
if '%%n'=='%temp%' set val!m!=14
if '%%o'=='%temp%' set val!m!=15
if '%%p'=='%temp%' set val!m!=16
if '%%q'=='%temp%' set val!m!=17
if '%%r'=='%temp%' set val!m!=18
if '%%s'=='%temp%' set val!m!=19
if '%%t'=='%temp%' set val!m!=20
if '%%u'=='%temp%' set val!m!=21
if '%%v'=='%temp%' set val!m!=22
if '%%w'=='%temp%' set val!m!=23
if '%%x'=='%temp%' set val!m!=24
if '%%y'=='%temp%' set val!m!=25
if '%%z'=='%temp%' set val!m!=26
)
if not !vars! equ !m! goto indexing
set var=0
:confirmvar
set /a var+=1
set tmp=!var%var%!
set /a value1=!val%var%!+1
for /f "tokens=%value1% delims== """ %%1 in ('type "%file%" ^| find "%tmp%"') do set "val%var%=%%1"
if not !var! equ !vars! goto confirmvar
:output
echo %val1% %val2% %val3% %val4% %val5% %val6% %val7% %val8% %val9%^ >>outputfile.txt
if not !c! equ !f! set m=0&goto skip1
start notepad outputfile.txt
exit /b
::------------end of test.bat-------------

Simple solutions are often the best

message edited by hidde663


Report •

#2
April 7, 2017 at 00:52:28
Hey there ! I didn't expect an answer this fast !

Unfortunately an error occurs. It can't find the file specified :(

"The system cannot find the file specified"

message edited by Zacqr


Report •

#3
April 7, 2017 at 03:55:53
I don't know what your hopes were for this:
for /f "tokens=4" %%A in ('dir /4 ^| find ".xml"')

But you can count xml files with this:
for /f "tokens=* delims= " %%i in ('dir/b *.xml') do (set/a c+=1)

We'll try to fix the rest of this mess as we go along.
2017-04-07 17:55:23.70 GMT +7

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
April 7, 2017 at 22:07:17
✔ Best Answer
glutton for punishment, I guess...
::========= begin batch-script
@echo off & setlocal

:: the following line defines the variables you want to collect
set selected=currentwork shortid holidaycounter

:: begin main xml-file qualifications
for /f "tokens=1 delims=:" %%a in ('findstr /i "name=\"ian\"" *.xml ^| findstr /i "position=\"executive\""') do (
set fil=%%a
for /f "tokens=* delims=<>" %%b in (%%a) do (
call :xx %%b
set fil=
)
)
goto :eof

:xx
if defined fil echo xml file: %fil%
:aa
set test=%1
if not defined test goto :donethis
if %test% equ %~1 (set var=%1) else (if defined var call :bb %var% %test%)
shift
goto :aa

:bb
set %1=%~2
set var=
goto :eof

:donethis
for %%a in (%selected%) do (
rem ========= OUTPUT SECTION based on selected variable names
if defined %%a set %%a
set %%a=
)
goto :eof


Report •

#5
April 8, 2017 at 00:59:22
Can somebody tell me, in a few english-like sentences, what this .bat needs to do?
2017-04-08 14:58:45.03 GMT +7

=====================

M2 Get custom script or take private lessons


Report •

#6
April 9, 2017 at 19:24:12
Thanks for the help ! Sorry I couldn't reply earlier. Was out of town this weekend !

@nbrane. Is it possible to create a outputfile.txt after the .bat script ran trough the files?
Also do I need to define/configure these parts?

set var= <----- ?
goto :eof

:donethis
for %%a in (%selected%) do (
rem ========= OUTPUT SECTION based on selected variable names
if defined %%a set %%a
set %%a= <----- ?


Sorry, bat is really new to me.


@Mechanix2Go. Sorry for confusing you with my words. I'am looking for a .bat script which runs trough my XML files and extracts only certain Data of specified XML files. The data of these XML files should only be extracted, if the name "Ian" and position"executive" is matching. If the .bat found a matching XML file, it should only extract the values mentioned above.
I tried to make it a bit clearer. Sorry, English is not my mother-tongue.

message edited by Zacqr


Report •

#7
April 9, 2017 at 21:36:25
You ask for currentWork, shortID, and holidaycounter, but your example data does not have those attributes.

What I'm getting at is you seem to have a clear idea of what you want to happen, but probably not a clear enough idea to express it without pointing at the screen. Problem is, we can't see your screen, so you have to explain what you're looking for, and what you want to do with it. So far, you've only described how to identify it. Showing the DOM would probably help.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#8
April 9, 2017 at 22:11:15
Thanks for the heads up. I'am sorry for my poor description skills !

With"extract" I mean to filter out the data from those XML files ( only if they match= name:Ian + position:excutive) into outputfile.txt ( simple txt file ).

The text file should only contain the "Data or Values" I'am looking for.
With "Data or Values" I'am only interested in:
1.currentWork
2.shortID
3.holidaycounter

<Name="Ian" workstampIN="2017-04-06" worknumber="XXXX" editor="XXXX" Position="Executive currentWork="engineer shortID=543532 holidaycounter="50" >

Maybe this will clear it up a bit

message edited by Zacqr


Report •

#9
April 9, 2017 at 23:49:42
Hi R2,

I'm glad it's not just me.

=====================

M2 Get custom script or take private lessons


Report •

#10
April 10, 2017 at 19:54:50
Zac: no, these statements are meant to be as written. They "undefine", or clear, a variable prior to the next cycle:
set var=
goto :eof

:donethis
for %%a in (%selected%) do (
rem ========= OUTPUT SECTION based on selected variable names
if defined %%a set %%a
set %%a=

Make SURE there's not a space after the equals sign, or else insure the statements like:
(set %%a=)
(set var=)

Other than that clarification, I can only add that you did not give any specific format for output of the text, so I rendered it using 'set'. To get the text file, run the code piped out to the text file. Save the code in the same directory as the XML files, F/E: xxx.bat
Then run the code: xxx > xmfilter.txt
I don't know what result you have had so far if you tried my code. It worked ok on some dummy-files I made on my end, but...

ps: if you want explanation of code, I can try to do so.

message edited by nbrane


Report •

#11
April 10, 2017 at 21:36:15
Thank you very much with your explanations and patience.
It works now for me. Thank you very very much.

And sorry for being inexperienced in .bat. I'll do better next time with asking questions !


Report •

#12
April 12, 2017 at 00:51:51
Had to study for my exams, returned today.
Glad to see you guys solved my mistakes :)

Simple solutions are often the best


Report •

Ask Question