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

asked on

Copy and rename file or folder via powershell and name format to be taken up from XML

I have one simple Xml file- Tenants

<?xml version="1.0" encoding="UTF-8"?>
<Tenants>
  <ENVcode>
    SIT
  </ENVcode>
  <Schools>
      <School>
            E1234
      </School>
      <School>
            E1236
      </School>
      <School>
            E1238
      </School>
  </Schools>
 </Tenants>

Also I have one folder for which I need to copy that and paste the contents in another folder and the name should be read from the xml file - "Tenants" and format of name should be <ENVCode>-<School>.

I am writing the below command. Can anyone help please.

[xml]$XmlDocument = Get-Content -Path D:\a1\Tenant.xml
 Copy-Item -Path d:\a1 -Destination d:\$XmlDocument.Tenants.ENVcode-$XmlDocument.Tenants.Schools.School

In our case the folder should be pasted to 3 different folders with name SIT-E1234 , SIT-E1236, SIT-E1238.

Also inside these folders we need to rename all their xml files in the same format.xml

Also tried the below-

[xml]$XmlDocument = Get-Content -Path D:\a1\Tenant.xml
Copy-Item -Path "d:\a1\a11\*" -Destination "d:\a2\a11\"$XmlDocument.Tenants.ENVcode-$XmlDocument.Tenants.Schools.School"-"recurse -Force -Verbose
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aakash Aggarwal
Aakash Aggarwal

ASKER

Thanks its giving the desired output for copying but its not renaming the default xml file into each respective folder by its name.xml , still inside all folders I have default.xml only. Also is it possible to make changes to any node in these xml files in these new created folder ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Its working. Thanks.
Also now , I have One XML file in  d:/Site/Template/default.xml and parallel to the default.xml file and have two folders in Template  folder named as - 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.

 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>
      <Doc>a123.xml</Doc> //Changes in path should be reflected here and the new path should be as D:\Site\SIT-E1234\Crystal reports
      <Crsrep>a124.xml</Crsrep>//Changes in path should be reflected here.
  </food>
</breakfast_menu>

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.