Hi,
I want to remove all the leading spaces from multiple xml files.
Before
[HTML]
[/HTML]
After Trim
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » Developers, developers, developers » DevOps Lounge » Trimstart to delete all leading spaces in xml
John,
Here’s a little PowerShell program to do the trick:
#Version: 1.0 $FileList = Get-ChildItem -Path "G:Test" -Filter "*.xml" ForEach ($FSpec in $FileList) { $Lines = (get-content $FSpec.FullName) $Lines[1..($Lines.count -1)].TrimStart(" ") | Set-Content $FSpec.FullName } # End ForEach ($FSpec...
Results:
45397-StripXML
I started with your basic file and added the Identifying information Test 1 and Test2 including spaces on either side so you could see they didn’t get stripped.
If you haven’t used PowerShell you can check out THIS POST Items 1-3.
Here’s the code in a program file ready to go: 45398-Strip-XML-Leading-Spaces
Of course, you’ll need to change the -Path argument to fit your computer setup.
HTH :cheers:
Hi,
Please have a look i am getting this error.
$Lines[1..($Lines.count -1)].TrimStart(” “) |
Method invocation failed because [System.Object[]] doesn’t contain a method named ‘TrimStart’.
At C:Testtrim.ps1:7 char:41
+ $Lines[1..($Lines.count -1)].TrimStart <<<< (" ") |
+ CategoryInfo : InvalidOperation: (TrimStart:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
John,
In the PowerShell command window enter: $PSVersionTable this will provide the information on your version of powershell.
Example:
45409-psversiontable
Copy and paste it into your reply. :cheers:
Cliff,
Any Idea why he got the error? It worked perfectly on my machine. :cheers:
RG,
No, I do not. I can only assume that at least one of the files read, did not return a string object, and therefore the ‘TrimStart()‘ caused the error.
BTW, this bit of code: $Lines[1..($Lines.count -1)].TrimStart(” “) skips the first line, so when the lines are written back to the file, the first line is missing.
Cliff,
You always get me thinking and now I’ve learned a new thing about get-content. Seems you don’t have to iterate the lines you can just apply the method to the result as a whole (if you want to operate on the whole file that is).
I also liked your use of the Get-ChildItem right in the ForEach vs placing it in a variable.
Version 2:
#Version: 2.0 # Change $Path and $Filter. Will work on ANY Text type file! $Path = "G:Test" $Filter = "*.xml" ForEach ($FSpec in (Get-ChildItem -Path $Path -Filter $Filter)) { (Get-Content $FSpec.FullName).TrimStart(" ") | Set-Content $FSpec.FullName } # End ForEach ($FSpec...
:cheers:
Cliff,
OOPS! I modified some code that worked on .CSV files where I had to skip the headers. Your are correct it should be:
$Lines[0..($Lines.count -1)].TrimStart(" ")
That could have also caused the error if there was only one line in a file!
As always Cliff to the rescue! :thewave:
:cheers:
cafed00d,
I also use NP++ and love it. I knew you could do this there with regular expressions but I didn’t know it had the feature baked in. NP++ does so many things it’s hard to know all it’s features. Recently, I discovered the plugin for file compare. I’ve since been using it to death comparing one version of a PowerShell script to another and also comparing output files from different versions to quickly verify If I’ve messed something w/o having to go line by line. Needless to say I’m a big NP++ fan!
HTH :cheers:
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications