Findstr to remove whitespace in xml files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carmine
Posts: 15
Joined: 18 Nov 2013 13:12

Findstr to remove whitespace in xml files

#1 Post by carmine » 18 Aug 2016 11:28

Hi,

There are multiple xml files in folder d:/global and I want to remove all the whitespace /Tab from each and every line from the xml files in the folder.
I tried "findstr /r /v /c:" " /c:"^$" *.xml "
XML file
<?xml version="1.0" encoding="UTF-8"?>
space...<Document xmlns="urn:iso:std:iso:20022.........">
space....<GrpHdr>
space.....<something>
space......<something>
space........<something>
space....</GrpHdr>
space......<....
space........< ...

After spaces removed
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022.........">
<GrpHdr>
<something>
<something>
<something>
</GrpHdr>
< ....
< ...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Findstr to remove whitespace in xml files

#2 Post by foxidrive » 18 Aug 2016 11:48

findstr can't remove characters from a line but there is a batch script called Jrepl that has this as one of it's abilities.

I want to remove all the whitespace /Tab from each and every line

This may not be quite what you want to do though.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.xml /b /a-d ') do call jrepl "[ \t]" "" /x /f "%%a" /o -



This uses a native Windows batch script called Jrepl.bat written by Dave Benham
Put it in the same folder, or in a folder that is on the system path.

viewtopic.php?f=3&t=6044

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Findstr to remove whitespace in xml files

#3 Post by aGerman » 18 Aug 2016 12:27

I hope that jrepl doesn't damage the UTF-8 encoding. There are loads of explanations why you shouldn't apply regular expressions to HTML, XHTML, XML, ...
The funniest "not an explanation" that I ever read :lol:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
Nevermind. It may work anyway ...

Steffen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Findstr to remove whitespace in xml files

#4 Post by foxidrive » 19 Aug 2016 04:48

aGerman wrote:I hope that jrepl doesn't damage the UTF-8 encoding. There are loads of explanations why you shouldn't apply regular expressions to HTML, XHTML, XML, ...

That's a point I always overlook.
I've used XML in plain text files here - but I only rarely used xml and so don't have familiarity with it and always treat it as plain text files.

I'm glad you brought that up aGerman and I might remember next time. Or I might not. :oops:
The funniest "not an explanation" that I ever read :lol:

That is funny :)

carmine
Posts: 15
Joined: 18 Nov 2013 13:12

Re: Findstr to remove whitespace in xml files

#5 Post by carmine » 24 Aug 2016 01:02

Hi,

I want to remove all the whitespace /Tab from each and every line

That's working perfectly.

If i want to only remove the leading space before " <"

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Findstr to remove whitespace in xml files

#6 Post by foxidrive » 24 Aug 2016 01:49

carmine wrote:If i want to only remove the leading space before " <"


In a plain text file this works. It may work with your files too, give it a shot on sample files.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.xml /b /a-d ') do call jrepl "^[ \t]*" "" /x /f "%%a" /o -

Post Reply