Add-Type -AssemblyName System.Windows.Forms $workingDirectory = 'D:\v_ram' $daysBeforeDelete = 3 $driveLetter = 'S:' $workFolder = 'current' $backupFolder = 'backup' $dateFormat = 'yyyy_MM_dd' function Is-Empty { param ( [string] $Path ) return (Get-ChildItem $Path | Measure-Object).count -eq 0 } function Show-Notify { param ( [string] $Title, [string] $Message, [int] $Timeout = 10000 ) $path = (Get-Process -id $pid).Path $notify = New-Object System.Windows.Forms.NotifyIcon $notify.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) #$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning $notify.BalloonTipTitle = $Title $notify.BalloonTipText = $Message $notify.Visible = $true $e_h = { param([object]$sender, [System.EventArgs]$e) $notify.Dispose() } $notify.add_BalloonTipClosed($e_h) $notify.ShowBalloonTip($Timeout) } # Make sure working directory exist mkdir $workingDirectory -force mkdir (Join-Path $workingDirectory $workFolder) -force mkdir (Join-Path $workingDirectory $backupFolder) -force # Move everything in work folder to backup folder if (-not (Is-Empty (Join-Path $workingDirectory $workFolder))){ $todayFolder = [IO.Path]::Combine($workingDirectory, $backupFolder, (get-date -f $dateFormat)) mkdir $todayFolder -force Move-Item -Path ([IO.Path]::Combine($workingDirectory, $workFolder, '*')) -Destination $todayFolder } $deleteCount = 0 # Delete old backup Get-ChildItem (Join-Path $workingDirectory $backupFolder) | foreach-object { # Get folder name and check is older enough to be deleted if ((new-timespan ([DateTime]::ParseExact($_.Name, 'yyyy_MM_dd', $null)) (get-date)).Days -gt $daysBeforeDelete) { rm -R -Force ([IO.Path]::Combine($workingDirectory, $backupFolder, $_.Name)) $deleteCount++ } } if ($deleteCount -gt 0) { Show-Notify "VRamdisk" "Permanently deleted $deleteCount backups" } # Mount folder as dirve subst $driveLetter (Join-Path $workingDirectory $workFolder)