Monthly Archive

Categories

Deleting folders based on month name

An interesting problem around deleting folders based on month name.

You have a folder structure that looks like this

C:\Testdata\2011\08-Aug
C:\Testdata\2011\09-Sep
C:\Testdata\2011\10-Oct
C:\Testdata\2011\11-Nov
C:\Testdata\2011\12-Dec
C:\Testdata\2012\01-Jan
C:\Testdata\2012\02-Feb
C:\Testdata\2012\03-Mar
C:\Testdata\2012\04-Apr
C:\Testdata\2012\05-May
C:\Testdata\2012\06-Jun
C:\Testdata\2012\07-Jul
C:\Testdata\2012\08-Aug
C:\Testdata\2012\09-Sep
C:\Testdata\2012\10-Oct
C:\Testdata\2012\11-Nov
C:\Testdata\2012\12-Dec
C:\Testdata\2013\01-Jan
C:\Testdata\2013\02-Feb
C:\Testdata\2013\03-Mar
C:\Testdata\2013\04-Apr
C:\Testdata\2013\05-May
C:\Testdata\2013\06-Jun
C:\Testdata\2013\07-Jul
C:\Testdata\2013\08-Aug

 

The structure extends forward and backward in time. Each year folder has 12 subfolders based on the number and name of the month.

 

You want to delete all folder, subfolders and contents that are more than 7 years old BUT you have to use the folder name for month and its parent for the year. You can’t assume that the create date or last write date will work in this instance.

 

If you want to paly around with this the code to create the folder structure looks like this:

##
## define enum for months
##
enum month {
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
}

##
## create folder structure
##

New-Item -Path c:\ -Name Testdata -ItemType Directory

2018..2005 |
foreach {
New-Item -Path c:\Testdata -Name $psitem -ItemType Directory

$path = "c:\Testdata\$psitem"

1..12 | 
foreach {
$name = "{0:00}-{1}" -f $psitem, [month]$psitem
New-Item -Path $path -Name $name -ItemType Directory
}
}

 

Create a top level folder. Then for the years 2018 back to 2005 create a year folder using the year as the name. In each year folder create 12 month folders. I’ve used the string formatting –f operator to put a leading zero on the month number and to get the appropriate value from the enum.

 

Now to delete the folders

enum month {
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
}

$date = Get-Date
$year = $date.Year - 8

##
## delete everything 8 years or older
##
Get-ChildItem -Path C:\Testdata -Directory |
where Name -le $year |
foreach {
Remove-Item -Path $psitem.Fullname -Recurse -Force -Confirm:$false
}

##
## if Month -ne January
## need to delete some months
##

if ($date.Month -gt 1){
$path = "C:\testdata\$($year+1)"
$month = $date.Month -1

1..$month | 
foreach {
$mpath = "$path\{0:00}-{1}" -f $psitem, [month]$psitem
Remove-Item -Path $mpath -Recurse -Force -Confirm:$false
}
}

 

The code gets the current date and gets the year minus 8. It loops through your top level folder and gets the folders that are less than or equal to the year you've defined. They, and their contents, are force deleted. Only thing that could stop the deletion is if you have one of the files pinned open.

 

If the current month is January there's nothing else to do. Otherwise create the path to the -7 year folder and calculate the last month you want to delete. Loop through the months, build the path and force the deletion of the folder and its contents.

 

The bulk of the work is done at the year level with a quick clean up of the months.

Comments are closed.