Search This Blog

Tuesday, June 19, 2018

I don’t like mondays… Powershell: Move specific files

Recently had to move files from an archive location. THe archive location was a reasonable large folder structure containing around 60000 files of various sizes (between 1Kb to 100GB+).
The archive was to be cleaned out leaving only files created on a Monday. I won’t go into why this was the case.
Came up with the following script, which works pretty well.


$SourceRootPath = '\\server01\sourceRootFolder\'
$DestinationRootPath = '\\server02\DestinationRootFolder\DesitnationSubFolder'
Set-Location $SourceRootPath

$Count = 0;

#Replicate source folder structure in destination folder
Copy-Item $SourceRootPath $DestinationRootPath -filter {PSIContainer} -Recurse -Force

$allbackups = get-childitem $SourceRootPath -Recurse -filter '*.bak' | Where-Object {!$_.PSIsContainer -and $_.lastwritetime.DayOfWeek -ne 'Monday'}

ForEach ($file in $allbackups)
{
    $Count++;
    $relativePath = Resolve-Path -Path $file.DirectoryName -Relative
    $DestinationPath = Join-Path $DestinationRootPath -childpath $relativePath.replace('.\','')
    Write-Progress -Activity "Moving  $($file.FullName)" -Status 'Progress ->' -PercentComplete (([int]($Count / $allbackups.Count)) * 100)
    Move-Item -Path $file.FullName -Destination $DestinationPath
}


Share/Bookmark

No comments:

Post a Comment