161 lines
5.6 KiB
PowerShell
161 lines
5.6 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
#[Parameter(Mandatory=$true)]
|
|
[string]$Path='E:\projects',
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$LogFile,
|
|
#[Parameter(Mandatory=$true)]
|
|
[string]$MoveTo='\\93KFSL1\projects$',
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$OlderThan=90,
|
|
[Parameter(Mandatory=$false)]
|
|
[switch]$Calc=$false
|
|
)
|
|
|
|
## Set the default log file name and location
|
|
If (! $LogFile )
|
|
{
|
|
$filename = "{0}.log" -f (Get-Date -Format "yyyyMMddHHmmss")
|
|
$LogFile = Join-Path -Path $MoveTo -ChildPath $filename
|
|
}
|
|
|
|
## Delete the log file if it already exists
|
|
If ( Test-Path $LogFile ) { Remove-Item $LogFile -Force }
|
|
|
|
Function Write-LogFile
|
|
{
|
|
param([string]$entry)
|
|
$out = "{0} {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $entry
|
|
Write-Output $entry
|
|
Add-content $LogFile -Value $out
|
|
}
|
|
|
|
Function Process-Move
|
|
{
|
|
param([string]$source)
|
|
|
|
## Integrate the source and destination paths so we can make the destination folder structure
|
|
$Destination = $source.Replace($Path,$MoveTo)
|
|
|
|
## Get the parent folder
|
|
$DestinationDirectory = Split-Path $Destination -Parent
|
|
|
|
## Make the destination directory
|
|
If (! (Test-Path $DestinationDirectory) ) { New-Item -Path $DestinationDirectory -ItemType Directory -Force | Out-Null }
|
|
|
|
Write-LogFile $source
|
|
|
|
Try
|
|
{
|
|
## Copy the $source
|
|
Copy-Item -Path $source -Destination $Destination -Recurse -Force -ErrorAction Stop
|
|
|
|
## Delete the $source
|
|
Remove-Item $source -Recurse -Force -ErrorAction Stop
|
|
}
|
|
|
|
Catch
|
|
{
|
|
$ErrorMessage = $_.Exception.Message
|
|
Write-LogFile "Error: ${ErrorMessage}"
|
|
}
|
|
}
|
|
|
|
Function Process-Path
|
|
{
|
|
param([string]$source)
|
|
|
|
Begin
|
|
{
|
|
## Running total of the size of all directories and files
|
|
$TotalSize = 0
|
|
|
|
## Running total of the number of all identified directories
|
|
$TotalDirectories = 0
|
|
|
|
## Running total of the number of all identified files
|
|
$TotalFiles = 0
|
|
|
|
$StartDate = Get-Date
|
|
|
|
$StartTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
|
|
Write-LogFile '-------------------------------------------------------------------------------'
|
|
Write-LogFile "Started at : ${StartTime}"
|
|
Write-LogFile "Run on : ${Env:COMPUTERNAME}"
|
|
Write-LogFile "Source : ${Path}"
|
|
Write-LogFile "Destination: ${MoveTo}"
|
|
If ( $OlderThan -gt 0 ) { Write-LogFile "Modified : ${OlderThan} day(s) ago" }
|
|
Write-LogFile "Log file : ${LogFile}"
|
|
If ( $Calc ) { Write-LogFile "Calculating files, folders, and total size only - no files will be moved" }
|
|
Write-LogFile '-------------------------------------------------------------------------------'
|
|
}
|
|
|
|
Process
|
|
{
|
|
## Make sure $Path exists
|
|
If ( Test-Path $source )
|
|
{
|
|
## Identify all files and folders that match the criteria for what we're looking for
|
|
$FOUND_OBJECTS = Get-ChildItem -Path $source -Recurse | Where {
|
|
(
|
|
## Revit model backup folder
|
|
$_.Name -match '_backup$' -or
|
|
|
|
## Revit backup models
|
|
$_.Name -match '\.\d\d\d\d\.rvt$'
|
|
|
|
## Files and folders not modified between now and $OlderThan days ago
|
|
) -and ($_.LastWriteTime -lt $StartDate.AddDays(-$OlderThan))
|
|
}
|
|
|
|
## Loop thru each path to get its size and
|
|
Foreach ( $obj in $FOUND_OBJECTS )
|
|
{
|
|
## Get the size of the directory and update counters
|
|
If ( $obj -is [System.IO.DirectoryInfo] )
|
|
{
|
|
$size = (Get-ChildItem -Path $obj.FullName -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
|
|
$TotalSize = $TotalSize + $size
|
|
$TotalDirectories++
|
|
}
|
|
|
|
## Get the size of the file and update counters
|
|
If ( $obj -is [System.IO.FileInfo] )
|
|
{
|
|
$size = (Get-Item $obj.FullName | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
|
|
$TotalSize = $TotalSize + $size
|
|
$TotalFiles++
|
|
}
|
|
|
|
## Write the name of the file/folder that would be moved
|
|
If ( $Calc ) { Write-LogFile $obj.FullName }
|
|
|
|
## Process the move
|
|
Else { Process-Move $obj.FullName }
|
|
}
|
|
}
|
|
Else { Write-LogFile "${source} does not exist!" }
|
|
}
|
|
|
|
End
|
|
{
|
|
If ( $TotalSize -lt 1073741824 ) { $GrandTotal = "{0}MB" -f ([Math]::Round(($TotalSize / 1MB),2)) }
|
|
Else { $GrandTotal = "{0}GB" -f ([Math]::Round(($TotalSize / 1GB),2)) }
|
|
$EndTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$Duration = "{0:g}" -f (New-TimeSpan -Start $StartTime -End $EndTime)
|
|
|
|
Write-LogFile '-------------------------------------------------------------------------------'
|
|
Write-LogFile "Finished at: ${EndTime}"
|
|
Write-LogFile "Duration : ${Duration}"
|
|
Write-LogFile "Directories: ${TotalDirectories}"
|
|
Write-LogFile "Files : ${TotalFiles}"
|
|
Write-LogFile "Total size : ${GrandTotal}"
|
|
Write-LogFile '-------------------------------------------------------------------------------'
|
|
}
|
|
}
|
|
|
|
## Make the destination directory if it doesn't already exist
|
|
If ( ($MoveTo) -and (!(Test-Path $MoveTo)) ) { New-Item -Path $MoveTo -ItemType Directory -Force }
|
|
|
|
Process-Path $Path |