Add to first line

Welcome Forums General PowerShell Q&A Add to first line

Viewing 8 reply threads
  • Author
    Posts
    • #219012
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Hi Guys.

      I have been playing around unsuccessfully which is why I am here. I have a thousand .XML files in a folder. I am trying to add the line below to be the very first line in the file.

      <?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

      On one of my scripts I got close but really does not like that text for some reason. Can anyone help? really don’t want to sift through a thousand files to add this line manually.

      Thanks in advance.

      • This topic was modified 1 month, 1 week ago by McTech09.
    • #219060
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Yes this is possible. Share what you have tried and what exactly isn’t working.

      On another note, why exactly are you doing this? That line is an XML declaration that has valuable information for each specific XML file. Just going out and changing that line can break the xml files if they are using a different coding or are not standalone and linked to other XML files. What exactly are you attempting to solve?

      https://xmlwriter.net/xml_guide/xml_declaration.shtml

    • #219087
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Basically. I have a massive XML file and I’ve split each section out in to individual xml files. This has worked perfectly. However that header is missing from each file. Thats why I just need to add it in.

      Ive rebooted my laptop since and didn’t save anything that was not working.

    • #219123
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      $a = Get-Content ‘C:\XML\*’

      $b = “1”

      Set-Content C:\*.xml –value $b, $a

      The above works , however it puts a 1 for each file it runs through. I guess I need to put it in a fore each.

      However it really does not like

      $b = “<?xml version=””1.0″” encoding=””utf-8″” standalone=”yes”?>”

      get something like

      + $b = “<?xml version=””1.0″” encoding=””utf-8″” standalone=”yes”?>”
      + ~~~~~~~
      Unexpected token ‘yes”?>”‘ in expression or statement.
      + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : UnexpectedToken

    • #219126
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Changed it to the below and it now works.

      $b = “<?xml version=””1.0″” encoding=””utf-8″” standalone=””yes””?>”

    • #219141
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Although its just appending each xml file. Guess I need to stick it in a for each and see if that works.

    • #219144
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Funny, my Powershell ISE and and VSCode retains all of my code after a reboot, even if it’s not saved. Anywho, this is a simple process, all you need is to use Set-Content to overwrite the file:

      #Get the files. You should make sure this is getting the files you want first.  I normally would do the First 1 to test and validate and then run it on the rest.
      $files = Get-ChildItem -Path C:\Scripts\*.txt # | Select -First 1
      
      foreach ($file in $files) {
          #Empty array
          $content = @()
          #Add first item in the array
          $content += '<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>'
          #Get Content get the content of the file seperated into an array. 
          #Next we append content of the file in the loop to the content array 
          $content += Get-Content -Path $file.FullName
      
          #Here you need take the content and set it to overwrite the file, you should validate it looks correct
          $content
      }
      
    • #219150
      Participant
      Topics: 2
      Replies: 5
      Points: 33
      Rank: Member

      Just to complete the post

      Get-ChildItem H:\XML -Filter *.xml |

      ForEach-Object{

      $a = Get-Content $_.FullName
      $b = “<?xml version=””1.0″” encoding=””utf-8″” standalone=””yes””?>”
      $c = “<game>”

      Set-Content $_.FullName –value $b, $c, $a

      }

    • #219198
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      This has worked perfectly.

      Obviously it has not. 😉

      However that header is missing from each file.

      You might show the code you used for that task and we might be able to fix your problem before it appears. 😉

Viewing 8 reply threads
  • You must be logged in to reply to this topic.