Computing.Net > Forums > Programming > Find and delete a special string with BAT

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find and delete a special string with BAT

Reply to Message Icon

Name: Pettersen
Date: September 30, 2009 at 17:20:06 Pacific
OS: Microsoft Windows XP Professional
CPU/Ram: 2.992 GHz / 3325 MB
Product: Dell / Optiplex 960
Subcategory: Batch
Tags: bat
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Wahine
Date: October 1, 2009 at 03:41:45 Pacific
Reply:

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



0

Response Number 2
Name: Pettersen
Date: October 1, 2009 at 03:51:39 Pacific
Reply:

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>
...


0

Response Number 3
Name: Pettersen
Date: October 1, 2009 at 04:07:28 Pacific
Reply:

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.


0

Response Number 4
Name: Wahine
Date: October 1, 2009 at 04:27:35 Pacific
Reply:

OK try this:

@echo off
cls
setlocal enabledelayedexpansion

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


1

Response Number 5
Name: Pettersen
Date: October 1, 2009 at 04:36:06 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Wahine
Date: October 1, 2009 at 04:42:22 Pacific
Reply:

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..


0

Response Number 7
Name: Pettersen
Date: October 1, 2009 at 06:27:54 Pacific
Reply:

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.


0

Response Number 8
Name: Wahine
Date: October 1, 2009 at 13:46:26 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Find and delete a special string with BAT

Find and replace a string in a file www.computing.net/answers/programming/find-and-replace-a-string-in-a-file/19034.html

~ BATCH: Find & delete a reg key ~ www.computing.net/answers/programming/-batch-find-amp-delete-a-reg-key-/9750.html

delete a repeated string from a fil www.computing.net/answers/programming/delete-a-repeated-string-from-a-fil/15333.html