Pretty-Print xml files and preserve original file date/time

The goal of this program is to: 1) Rename the file to include the PONum from inside the file 2) Pretty-Print (format) the XML inside the file 3) Preserve (keep) the original date/time of the file
<pre>
cls

function Format-XMLDoc ([xml] $xml, $indent=3) 
{ 
    $StringWriter = New-Object System.IO.StringWriter 
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter 
    $xmlWriter.Formatting = “indented” 
    $xmlWriter.Indentation = $Indent 
    $xml.WriteContentTo($XmlWriter) 
    $XmlWriter.Flush() 
    $StringWriter.Flush() 
    Write-Output $StringWriter.ToString() 
}

function Format-XML ($xmlString, $indent=3) 
{ 
    [xml]$xml.LoadXml($xmlString) 
    $StringWriter = New-Object System.IO.StringWriter 
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter 
    $xmlWriter.Formatting = “indented” 
    $xmlWriter.Indentation = $Indent 
    $xml.WriteContentTo($XmlWriter) 
    $XmlWriter.Flush() 
    $StringWriter.Flush() 
    Write-Output $StringWriter.ToString() 
}

$folderName = "c:\BizTalk\Messaging\Archive\SentToDropShipOrchBackup"
$files = Get-ChildItem $folderName -Filter "Test*.xml" -File  | Sort-Object LastWriteTime -Descending

$maxFiles = 1   #used to test with a small number of files first 
$fileCount = 0 
$xml = New-Object -TypeName System.Xml.XmlDocument

foreach ($file in $files) 
{
     $fileCount = $fileCount + 1 
     if ($fileCount -gt $maxFiles) 
        {
          exit 
        }
     Write-Host $fileCount $file.Name 
     Write-Host "Creation    Time: $($file.CreationTime)"
     Write-Host "Last Write  Time: $($file.LastWriteTime)"
     Write-Host "Last Access Time: $($file.LastAccessTime)"

     $origCreationTime  =  $file.CreationTime
     $origLastWriteTime =  $file.LastWriteTime

     $fileContents = [IO.File]::ReadAllText($file.FullName)
     [xml]$xml.LoadXml($fileContents)  #load into XmlDoc 

     $fileContentsFormatted = Format-XMLDoc $fileContents -indent 4
     Write-Host $fileContentsFormatted
     [IO.File]::WriteAllText($file.FullName, $fileContentsFormatted)

     <## optional if you want to rename file based on some filed in the file
     $PONum = $xml.ShipDocDetails.ShipDocDetails_Child1.rcPONumber_d
     $PONum=$PONum.Replace('"','');
     Write "PONum=$PONum" 

     $newGuid = [guid]::NewGuid()
     $newFileName = "ShipDoc_PONum_${PONum}_$newGuid.xml"
     Write-Host "NewName=$newFileName"

     Rename-Item $file.FullName $newFileName 
     (Get-ChildItem "${folderName}\${newFileName}").CreationTime = $origCreationTime
     (Get-ChildItem "${folderName}\${newFileName}").LastWriteTime = $origLastWriteTime
     ##> 
    
     #if you don't rename file above: 
     (Get-ChildItem $file.FullName).CreationTime = $origCreationTime
     (Get-ChildItem $file.FullName).LastWriteTime = $origLastWriteTime

}
</pre>

NOTE: Use $maxFiles=1 when testing, then set maxFiles=999999 when done testing.

NOTE: If you want to re-run this multiple times on same folder (or from the task scheduler), you don’t need to not re-process files that have already been processed. I usually do this by putting a double underscore in the name, then skipping past any files that have the double underscore in them.

Uncategorized  

Leave a Reply