Skip to content

Instantly share code, notes, and snippets.

@brunomlopes
Last active September 3, 2015 14:55
Show Gist options
  • Save brunomlopes/165857 to your computer and use it in GitHub Desktop.
Save brunomlopes/165857 to your computer and use it in GitHub Desktop.
Simple powershell script to archive a downloads folder.
$base_downloads_dir = "S:\downloads\"
$base_archive_dir = "S:\downloads\archive"
if(-not (Test-Path $base_archive_dir)) {
[void] (mkdir $base_archive_dir)
}
$to_archive_files = get-childitem $base_downloads_dir | ? { $_.Name -ne "archive" -and $_.CreationTime -lt (Get-Date).AddDays(-1) }
foreach($to_archive in $to_archive_files){
$name = $to_archive.Name
$i = 1
while ( Test-Path "$base_archive_dir\$($name)" ) {
$name = "$($to_archive.BaseName) ($i)$($to_archive.Extension)"
$i+=1;
}
echo "Moving $($to_archive.FullName) to $base_archive_dir\$name"
Move-Item "$($to_archive.FullName)" "$base_archive_dir\$name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment