Link to home
Start Free TrialLog in
Avatar of Aakash Aggarwal
Aakash Aggarwal

asked on

Rename and Edit xml files using powershell

I have a folder and in that I have multiple sub-folders , each containing a default.xml file. I need to rename all the default.xml within all these sub-folders with the same name as that of the sub-folder.

So that my default.xml will become sub-folder_name.xml.

Lets say My main folder is \sites\ASDF, within that I have 10 sub-folders -> ABC-123, DEF-231, ......like this and I want to rename the default.xml within all these sub-folder to ABC-123.xml, DEF-231.xml. It should be done by using Powershell. Any ideas?

rename-item –path "d:\a2\a11\SIT-E1236\default.xml" –newname d:\a2\a11\SIT-E1236\SIT-E1236.xml
rename-item –path "d:\a2\a11\SIT-E1238\default.xml" –newname d:\a2\a11\SIT-E1236\SIT-E1238.xml

This using some loop?

Also how can I edit some xml files together to make some particular changes on the xml nodes?
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

pushd " C:\sites\ASDF"
dir -Directory | % {rename-item "C:\sites\ASDF\$($_.Name)\Default.xml" "$($_.Name).xml"}
popd

Open in new window


Not checked here, test carefully
For question 2: do you want to modify data in the XML or change the structure?
Avatar of Aakash Aggarwal
Aakash Aggarwal

ASKER

Its like , I have One XML file in  d:/Site/Template/default.xml and parallel to the default.xml file and have two folders - CrsRep and my default.xml file contains the two nodes CrsRep and DOC. I need to automatically edit the nodes with the updated path of the folder.

I am using the below script to copy and rename all the folders.

$source = 'd:\Site\Template'
$target = 'd:\Site\'
$xmlPath = 'D:\Tenant.xml'

$xml = [xml](Get-Content -Path $xmlPath)
$envCode = $xml.SelectSingleNode('Tenants/ENVcode').InnerText.Trim()
$xml.SelectNodes('Tenants/Schools/School') | ForEach-Object {
      $name = "$($envCode)-$($_.InnerText.Trim())"
      $destination = Join-Path -Path $target -ChildPath $name
      Copy-Item -Path $source -Destination $destination -Recurse -Force -Verbose
      Get-ChildItem -Path $destination -Filter default.xml -Recurse | Rename-Item -NewName "$($name).xml"
}

After running this I will get all the folders with names listed in my Tenant file. Also inside all the folders, my default.xml file is renamed to name same as the folder name.xml. I just need to update the paths also inside all these new folder.xml with the corresponding new path in CrsRep and Doc Node.
Not sure if I get what you're trying to do
Have a look at Jeff Hicks excellent explanation of how to handle XML files in PowerShell:
https://www.petri.com/creating-custom-xml-net-powershell


HTH
Hi Michael,

I have folders and files in the below order.

Site -> SIT-1234 -> SIT-1234.xml, CrsRep, DocMan folders.
Site -> SIT-1245 -> SIT-1245.xml, CrsRep, DocMan folders.

 SIT-1234, SIT-2345 and such 100 more folders and inside all these there is one xml named as SIT-1234.xml, SIT-2345.xml and so on.
Below is my XML.

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
   </description>
    <calories>650</calories>
      <DocMan>a123.xml</DocMan>
      <Crystal reports>a124.xml</Crystal reports>
  </food>
</breakfast_menu>

I just need to update the DocMan and CrsRep node same as the path of these folders in all XMLs.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.