Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
cls
$executingDir = Split-Path -parent $PSCommandPath
$isTest = $false
$startTime = Get-Date
function EnsureDirExists($uncDir){
if(-not(Test-Path -Path $uncDir -PathType Container )){
Write-Host "Doesnt exist create"
New-Item -ItemType Directory -Path $uncDir
}
}
function RecurseToGetNonExistingFileName($uncPath, $count){
$dir = [System.IO.Path]::GetDirectoryName($uncPath)
$minusExt = [System.IO.Path]::GetFileNameWithoutExtension($uncPath)
$extension = [System.IO.Path]::GetExtension($uncPath)
$testPath = Join-Path $dir ($minusExt + ".different" + $count + $extension)
if((Test-Path -Path $testPath)){
$count += 1
RecurseToGetNonExistingFileName $uncPath $count
} else {
$testPath
}
}
function AddShortGuidToFile($uncPath, $shortGuid){
$dir = [System.IO.Path]::GetDirectoryName($uncPath)
$minusExt = [System.IO.Path]::GetFileNameWithoutExtension($uncPath)
$extension = [System.IO.Path]::GetExtension($uncPath)
$now = (Get-Date).ToString("yyyyMMddHHmmssffffff")
$newPath = Join-Path $dir ($minusExt + ".$now" + $extension)
return $newPath
}
function MoveRemotePatternTimeExtra(){
param(
[Parameter(Mandatory=$true)]
[string]$uncDirectory,
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[string]$firstPart,
[Parameter(Mandatory=$true)]
[string]$datePattern,
[Parameter(Mandatory=$true)]
[string]$appAndEnv,
[string]$addToFileName = "",
[string]$removeFileExtension = "",
[boolean]$dontAddLogFileExtension = $false
)
if(-not(Test-Path $uncDirectory)){
Write-Host "Consider removing - folder doesnt exist - $uncDirectory"
return
}
$uri = new-object System.Uri($uncDirectory)
$uncRoot = [System.IO.Path]::GetPathRoot($uncDirectory)
$shortGuid = [Guid]::NewGuid().GUID.Split('-')[0].ToUpper()
$loggingDir = Join-Path $uncRoot "Logging\Archive"
foreach($p in $appAndEnv.Split('.')){
$loggingDir = Join-Path $loggingDir $p
}
Write-Host "LoggingDir - $loggingDir"
EnsureDirExists $loggingDir
$filesInDir = Get-ChildItem -Path $uncDirectory -File
if($filesInDir.Length -eq 0){
Write-Host "Consider removing - folder is empty - $uncDirectory"
return
}
$ignoreExtensions = @(".zip", ".xlsx")
foreach($fileInfo in $filesInDir){
[string]$file = $fileInfo
$extension = [System.IO.Path]::GetExtension($file)
if($ignoreExtensions.Contains($extension)){
Write-Host "File is bad extension, ignore. $file"
continue
}
if(-not ($file.StartsWith($firstPart))){
$fullName = $fileInfo.FullName
Write-Host "Ignore file - $fullName"
continue
}
Write-Host "File starts with $file"
$twoDaysAgo = (Get-Date).AddDays(-1)
if($fileInfo.LastWriteTime -gt $twoDaysAgo){
Write-Host "File was written to less than a day ago "
continue
} else {
Write-Host "File is fine to archive"
}
$moveFrom = Join-Path $uncDirectory $file
if([string]::IsNullOrWhiteSpace($removeFileExtension) -eq $false){
if($file.EndsWith(".$removeFileExtension")){
$file = $file.Substring(0, ($file.Length - ".$removeFileExtension".Length))
}
}
if($file -eq $firstPart){
$orig = $file
$info = (Get-ItemProperty "$uncDirectory\$file")
$file = $file + $info.LastWriteTime.Date.ToString($datePattern)
mv "$uncDirectory\$orig" "$uncDirectory\$file"
}
$minusFirstPart = $file.Substring($firstPart.Length)
$datePart = $minusFirstPart.Substring(0, $datePattern.Length)
try{
$dateValue = [DateTime]::ParseExact($datePart, $datePattern, $null)
} catch {
Write-Error "Can't parse date $datePart"
continue
}
$minusDatePart = $minusFirstPart.Substring($datePart.Length)
$fileName = $appAndEnv.Replace(".", "_") + $addToFileName + "_" + $dateValue.ToString("yyyyMMdd") + $minusDatePart
if($dontAddLogFileExtension -eq $false){
$fileName += ".log"
}
$moveTo = Join-Path $loggingDir $fileName
if((Test-Path -Path $moveTo)){
Write-Host "File Exists at $moveTo"
$moveTo = AddShortGuidToFile $moveTo $shortGuid
}
if($isTest){
Move-Item -Path $moveFrom -Destination $moveTo -WhatIf
} else {
Move-Item -Path $moveFrom -Destination $moveTo
}
}
}
MoveRemotePatternTimeExtra "\\some-server\d$\ProjectLogs" `
"Logging.Debug.log " `
"yyyy.MM.dd" `
"SomeProject.SomeSubProject.Environment" ""
$endTime = Get-Date
Write-Host "Started at"
$startTime
Write-Host "Finished at"
$endTime
Owner

CBurbidge commented Jan 4, 2017

This script is a bit messy, has a few things that are badly named and a few things which i'm not even sure are used.

But i've used it to rename ~65,000 log files on my works remote servers, so it's fairly well battle tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment