• Trimstart to delete all leading spaces in xml

    Home » Forums » Developers, developers, developers » DevOps Lounge » Trimstart to delete all leading spaces in xml

    Author
    Topic
    #506736

    Hi,

    I want to remove all the leading spaces from multiple xml files.

    Before
    [HTML]

    [/HTML]

    After Trim






    Viewing 9 reply threads
    Author
    Replies
    • #1575771

      John,

      Here’s a little PowerShell program to do the trick:

      Code:
      #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:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1575823

      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

    • #1575828

      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:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1575832

      John,

      Try this

      Code:
      foreach($file in (Get-ChildItem -Path ‘[B][COLOR=”#FF0000″]ENTER YOUR PATH HERE[/COLOR][/B]‘ -Filter ‘*.xml’)) {
        (Get-Content -Path $file.FullName) -replace ‘^s+’ |
        [B][/B]Set-Content -Path $file.FullName
      }
      
      
    • #1575839

      Hi,

      Thanks Cliff.H your solution is working perfectly.

      Thanks RG for supporting.

      Thanks.

    • #1575848

      Cliff,

      Any Idea why he got the error? It worked perfectly on my machine. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1575924

      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.

      • #1575954

        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:

        Code:
        #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:

        May the Forces of good computing be with you!

        RG

        PowerShell & VBA Rule!
        Computer Specs

    • #1575946

      Cliff,

      OOPS! I modified some code that worked on .CSV files where I had to skip the headers. Your are correct it should be:

      Code:
      $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:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1576174

      Would an app that does this do? I use Notepad++ to edit text files. One of its features is trimming leading spaces. There is also an XML plugin available the will pretty-print the XML (I deal a lot with XML without any linefeeds – it’s all on one line).

    • #1576205

      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:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 9 reply threads
    Reply To: Trimstart to delete all leading spaces in xml

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: