Here is a little function i created to create an XML file from the structure of a folder.
The folder structure looks like this:
E:\Archives\LatestArchiveFiles
20100821
Log20100821.log
Archive20100821.log
Routine20100821.log
20100828
Log20100828.log
Archive20100828.log
Routine20100828.log
It works by doing a Get-ChildItem on the folder and creating the XML file based on the folder structure.
Function New-XMLFile ($Path = "E:\Gateway\Archives")
{
Set-Location $path
Write-Verbose "Path has been set to $path"
$VerbosePreference = "Continue"
#$VerbosePreference = "silentlyContinue"
Write-Verbose $pwd
$xml = New-Object xml
$rootElements = gci
$root = $xml.CreateElement("root")
[void]$xml.AppendChild($root)
foreach($archiveElement in $rootElements ){
Write-Verbose "Archive Loop, for $archiveElement "
Write-Verbose "Setting location to $archiveElement"
set-location $path\$archiveElement
Write-Verbose "location Set to $pwd"
$archive = $xml.CreateElement("archive")
[void]$root.AppendChild($archive)
$archive.SetAttribute("Name", $archiveElement)
$fileElements = gci
foreach ($fileElement in $fileElements){
Write-Verbose "File Loop"
Write-Verbose "Looking at $fileElement`n"
$fileSize = ($fileElement.Length/1KB).tostring("0")
$file = $xml.CreateElement("File")
[void]$archive.AppendChild($file)
$file.SetAttribute("Name", $fileElement)
$file.SetAttribute("SizeKB", $fileSize )
$file.SetAttribute("LastWriteTime", $fileElement.LastWriteTime)
}
}
$xml.save("E:\Gateway\Archives")
}
New-XMLfile "C:\Documents and Settings\psuser"
The XML File created would look like this
<root> <archive Name="20100821"> <File Name="Log20100821.log" SizeKB="117727" LastWriteTime="08/21/2010 06:33:35" /> <File Name="Archive20100821.log" SizeKB="3" LastWriteTime="08/21/2010 06:33:35" /> <File Name="Routine20100821.log" SizeKB="74" LastWriteTime="08/21/2010 06:33:36" /> </archive> <archive Name="20100828"> <File Name="Log20100828.log" SizeKB="148740" LastWriteTime="08/28/2010 06:34:14" /> <File Name="Archive20100828.log" SizeKB="4" LastWriteTime="08/28/2010 06:34:15" /> <File Name="Routine20100828.log" SizeKB="90" LastWriteTime="08/28/2010 06:34:15" /> </archive> </root>
Advertisement