Solved Find and delete a special string with BAT

September 30, 2009 at 17:20:06
Specs: Microsoft Windows XP Professional, 2.992 GHz / 3325 MB

Hi!

I have an XML-file where the string <?xml version="1.0"?> occurs several times. I would like to delete all lines equal to this string and keep the rest (saving the result in output.txt). The string at the top including the encoding, I want to keep. (Actually, I have 450 XML-files where I want to delete all lines equal to <?xml version="1.0"?>.)

The <, ?, space, = and " within the string cause me trouble. I just strip it down to 0"? to avoid including the first line in the XML-file. However, I can't figure out how to tell the system to search for the string "0"?".

My starting point (saved in test.bat)
setLocal enableDELAYEDexpansion
set mystring="0"?"
find /v /i %mystring% < 104_arstat.xml > output.txt

Example (104_arstat.xml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="I:\arkart\arstat\kode\arstat_komm.xslt"?>
<ROTNODE>
<TOTALT>
<?xml version="1.0"?>
<ROWSET>
<ROW>
<KOMM>101</KOMM>
<LAND_DAA>596410</LAND_DAA>
<VANN_DAA>45940</VANN_DAA>
<TOTAL_DAA>642350</TOTAL_DAA>
</ROW>
</ROWSET>
</TOTALT>
<AREALTILSTAND>
<?xml version="1.0"?>
<ROWSET>
...
</AREALTILSTAND>
<SKOG>
<?xml version="1.0"?>
<ROWSET>
<ROW>
<MSTATSKOG>0</MSTATSKOG>
<SKOG_BESKRIVELSE>Ikke skog</SKOG_BESKRIVELSE>
<SAREAL> 181569.2</SAREAL>
...

I would appreciate any help.
Thanks.

Best regards
Anne, Norway


See More: Find and delete a special string with BAT

Report •


✔ Best Answer
October 1, 2009 at 04:27:35

OK try this:

@echo off
cls
setlocal enabledelayedexpansion

findstr/c:"<?xml version=\"1.0\"?>" /i /v input.xml > output.txt



#1
October 1, 2009 at 03:41:45

Do you want a script to handle all your xml files?



Report •

#2
October 1, 2009 at 03:51:39

Thanks for your reply and suggestion.
It works a little "too good", meaning also the two first lines in the file are gone. And I need to keep those two lines (for displaying the xml-file with an xslt-file in a web browser).

output.txt looks like this:
<ROTNODE>
<TOTALT>
<ROWSET>
<ROW>
<KOMM>104</KOMM>
<LAND_DAA>58020</LAND_DAA>
<VANN_DAA>5470</VANN_DAA>
<TOTAL_DAA>63490</TOTAL_DAA>
</ROW>
</ROWSET>

</TOTALT>
<AREALTILSTAND>
<ROWSET>
<ROW>
<MSTATTYPE>1</MSTATTYPE>
<TYPE_BESKRIVELSE>Fulldyrka jord</TYPE_BESKRIVELSE>
<AAREAL> 6424.6</AAREAL>
...


Report •

#3
October 1, 2009 at 04:07:28

Yes, finally I want a bat-file going through all my xml-files in a folder and replacing the existing xml-files with new xml-files without the <?xml version="1.0"?> lines. For now, I'm just testing how to make it work on one single xml-file.

Report •

Related Solutions

#4
October 1, 2009 at 04:27:35
✔ Best Answer

OK try this:

@echo off
cls
setlocal enabledelayedexpansion

findstr/c:"<?xml version=\"1.0\"?>" /i /v input.xml > output.txt


Report •

#5
October 1, 2009 at 04:36:06

Hahaha, yes, I just found it out myself:

findstr /v /i /c:"<?xml version=\"1.0\"?>" 101_arstat.xml > output.txt

As Wahine, I just figuered out that /c: does the trick.
(I read the findstr /? one more time - a little bit more carefully this time.)

Thanks for the tips reagrding the use of findstr in stead of find and the use of \ in front of the " within the string.


Report •

#6
October 1, 2009 at 04:42:22

Thank you for coming back to report your success. Almost 1 a.m. here so am going to bed now. Will check in later this morning.

Good luck with the rest of your project and don't hesitate to come back for support if you need it.

Wahine..


Report •

#7
October 1, 2009 at 06:27:54

You're welcome.

Yes, I managed to succesfully search through all my *.xml, createing new *_ff.xml without the <?xml version="1.0"?> lines, deleting the orignal *.xml and then renaming the new *_ff.xml-files back to *.xml. (I want to keep the original filenames.)

Example:
1) 106_arstat.xml (WITH the <?xml version="1.0"?>-lines) is ...
2) ... changed to106_arstat_ff.xml (WITHOUT the <?xml version="1.0"?>-lines)
3) 106_arstat.xml in step 1) is deleted
4) 106_arstat_ff.xml is renamed to 106_arstat.xml

for %%f in (*stat.xml) do (
findstr /v /i /c:"<?xml version=\"1.0\"?>" %%f > %%~nf_ff.xml
del %%f
)
for %%f in (*.xml) do rename "%%f" *stat.xml

http://www.computerhope.com/forhlp.htm was a nice starting point for the "for-do"-matter.


Report •

#8
October 1, 2009 at 13:46:26

Anne, thank you for coming back to advise about the successful completion of your project. It's been a pleasure collaborating with you.

Good luck

Wahine.


Report •


Ask Question