Edit multiple xmls in different folders

Aakash Aggarwal
Aakash Aggarwal used Ask the Experts™
on
All the folders in screenshot are created by copying and renaming one Template folder. Now all sub-folders contains one xml file, need to update the path in these xml's with the path of Crystal Reports and DocMan folder, which exist in all these folders.

[xml]$XmlDocument = Get-Content -Path d:\Site\"$($name)\$($name).xml"
$XmlDocument.breakfast_menu.food.DocMan = 'd:\Site\'
$XmlDocument.breakfast_menu.food.Crystalreports = 'd:\Site\'
$XmlDocument.Save("D:\Site\"$($name)\$($name).xml")

The above script is not giving the desired result.

My earlier script for copying and renaming:
$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"      
}
Screenshot.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Consultant
Commented:
If I understand properly, this should do it for you:
$folders = dir c:\site -directory
foreach( $folder in $folders )
{
	$xmlFile = Join-Path $folder.FullName ( $folder.Name + '.xml' )
	if( Test-Path $xmlFile )
	{
		[xml]$xml = Get-Content $xmlFile
		[string]$docman  = Join-Path $folder.FullName 'DocMan'
		[string]$crystal = Join-Path $folder.FullName 'Crystal reports'
		$xml.breakfast_menu.food.DocMan = $docman
		$xml.breakfast_menu.food.Crystalreports = $crystal
		$xml.breakfast_menu.food.DocMan
		$xml.breakfast_menu.food.Crystalreports
		$xml.Save( $xmlFile )
	}
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial