Skip to content

Instantly share code, notes, and snippets.

@kfstorm
Created June 9, 2015 09:07
Show Gist options
  • Save kfstorm/2081e45a60e9480a889c to your computer and use it in GitHub Desktop.
Save kfstorm/2081e45a60e9480a889c to your computer and use it in GitHub Desktop.
$rootPath = "D:\Advertiser"
$deleteFileBefore = New-Object -typename System.DateTime -argumentlist (2013,12,25,0,0,0)
$root = New-Object -typename System.IO.DirectoryInfo -argumentlist $rootPath
foreach ($file in $root.GetFiles(“*”, [System.IO.SearchOption]::AllDirectories))
{
if ($file.LastWriteTime -le $deleteFileBefore)
{
echo ("Deleting", $file.FullName -join " ");
$file.Delete();
}
}
foreach ($subDir in $root.GetDirectories(“*”, [System.IO.SearchOption]::AllDirectories))
{
$subDir.Refresh()
if ($subDir.Exists)
{
$filesCount = $subDir.GetFiles(“*”, [System.IO.SearchOption]::AllDirectories).Length;
if ($filesCount -eq 0)
{
echo ("Deleting", $subDir.FullName -join " ");
$subDir.Delete($True);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment