Renditional Folder Relocator

Sunday, July 13, 2014
<#

Purpose:    Relocate newly created directories from one directory to
            another, creating renditions if    necessary.
Notes:        I wrote this for a friend at work that was wanting a 
            script to monitor a folder (in this case via a scheduled
            task), and would move any directories that had not been 
            modified for greater than than a day.  There was a bit of 
            fear that the end-users would create directories that shared 
            names, so he wanted to be sure that the files were not 
            over-written.  I decided to    mitigate this risk by appending
            a revision number to the end of the filename.  A log was also
            required.  Any feedback is greatly appreciated!
Issue:        No known issues.  Use at your own peril!

#>

#Name the Directories
$LogFolder = "C:\Scripts\LogFolder\"
$SourceFolder = "C:\Scripts\MoveFrom\"
$EncryptedFolder = "C:\Scripts\MoveTo\"

#Query the folder where the new folders will be placed for folders that were modifified greater than one day ago.
$NewAudits = Get-ChildItem $SourceFolder -Directory | Where { $_.LastWriteTime -lt ((Get-Date).AddDays(-1)) }

#Query the destination folder (for later use).
$EncryptedAudits = Get-ChildItem $EncryptedFolder -Directory

#Instantiate a new array for storage.
$FolderStats = @()

if($NewAudits){
    #Iterate throught the NewAudits directory
    $FolderStats = ForEach($Directory in $NewAudits){
        #Create a new object per Directory
        $FolderObject = New-Object -TypeName PSCustomObject -Property @{
            Name = $Directory.Name
            OldPath = $Directory.FullName
            NewPath = $null
            }
        #Attempt to move the directory to the new location
        Try{
            Move-Item -Path $Directory.FullName -Destination $EncryptedFolder -ErrorAction Stop
            #Update the FolderObject with new information
            $FolderObject.NewPath = (-join($EncryptedFolder,$Directory.Name))
            }
        #Unable to move folder, becuase it probably already existed.
        Catch{
            #Query Destination Folder for files with the same name, and return a current count.
            [Double]$RevisionNumber = ($EncryptedAudits | Where { $_.Name -eq $Directory.Name -or $_.Name -match (-join($Directory.Name,"-Rev")) }).Count
            #Increment the count returned by one, to give us the new Revision number.
            $RevisionNumber++
            $NewName = (-join($Directory.Name,"-Rev",$RevisionNumber))
            #Move the Directory, appending the revision number en route.
            Move-Item $Directory.FullName -Destination (-join($EncryptedFolder,$NewName))
            $FolderObject.NewPath = (-join($EncryptedFolder,$NewName))
            }
        Finally{
            #Return the Object
            $FolderObject
            }
        }
    #Create HTML Document for logging.
    $FolderStats | ConvertTo-Html | Out-File -FilePath ("$LogFolder\Log.html")
    }
else{
    #No new Audits were found at the time the script was run.
    Write-Output ("There are no directories that have LastWriteTime values before "+(Get-Date -Format "yyyy-MM-dd")+".")
    }

No comments:

Post a Comment

Please keep all comments clean!