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.EN Vcode-$Xml Document.T enants.Sch ools.Schoo l
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.T enants.ENV code-$XmlD ocument.Te nants.Scho ols.School "-"recurse -Force -Verbose
<?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.EN
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.T
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Its working. Thanks.
ASKER
Also now , I have One XML file in d:/Site/Template/default.x ml 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('Ten ants/ENVco de').Inner Text.Trim( )
$xml.SelectNodes('Tenants/ Schools/Sc hool') | ForEach-Object {
$name = "$($envCode)-$($_.InnerTex t.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.
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>/
</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('Ten
$xml.SelectNodes('Tenants/
$name = "$($envCode)-$($_.InnerTex
$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.
ASKER