-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOldFileRemover.ps1
254 lines (201 loc) · 10.4 KB
/
OldFileRemover.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#-------------------------
# INFO
#-------------------------
# AUTHOR: Bastiaan Groen
# Date : 11-10-2019
$version = "2.1"
#-------------------------
# --- DISCRIPTION --------
# A Powershell script deletes files older than a certain date in a predefined folder.
# It checks the last-used date property of a file, when its older than the specified date, it will be deleted.
# It also deletes all empty folders that it can find in its predefined folder
# ------------------------
# --- CAUTION ------------
# This powershell is NOT extensively tested.
# There is NO undo!
# ------------------------
function SchrijfOutput
{
param(
[Parameter(Position=1)]
[string]
$text,
[Parameter(Position=2)]
[string]
$level,
[Parameter(Position=3)]
[string]
$max,
[Parameter(Position=4)]
[string]
$updateNumber
)
$tijd = (get-date).ToString('T')
switch($level)
{
"info"
{
Write-Host -ForegroundColor "Yellow" -nonewline "[i] "
Write-Host -ForegroundColor "Red" -nonewline "[$($tijd)] "
Write-Host -ForegroundColor "Yellow" "$($text) "
}
"error"
{
Write-Host -ForegroundColor "red" -nonewline "[!] "
Write-Host -ForegroundColor "Red" -nonewline "[$($tijd)] "
Write-Host -ForegroundColor "Yellow" "$($text) "
}
"done"
{
Write-Host -ForegroundColor "green" -nonewline "[*] "
Write-Host -ForegroundColor "Red" -nonewline "[$($tijd)] "
Write-Host -ForegroundColor "Yellow" "$($text) "
}
"update"
{
Write-Host -ForegroundColor "DarkGray" -nonewline " $($text) `r"
Write-Host -ForegroundColor "Gray" -nonewline " [ $($updateNumber) / $($max)] `r"
Write-Host -ForegroundColor "DarkGray" -nonewline "[i] `r"
}
default
{
Write-Host -ForegroundColor "Yellow" -nonewline "[i] "
Write-Host -ForegroundColor "Red" -nonewline "[$($tijd)] "
Write-Host -ForegroundColor "Yellow" "$($text) "
}
}
}
Write-Host -ForegroundColor "Gray" " ____ _ _ ______ _ _ "
Write-Host -ForegroundColor "Gray" " / __ \| | | | | ____(_| | "
Write-Host -ForegroundColor "Gray" " | | | | | __| | | |__ _| | ___ ___ "
Write-Host -ForegroundColor "Gray" " | | | | |/ _ | | __| | | |/ _ / __| "
Write-Host -ForegroundColor "Gray" " | |__| | | (_| | | | | | | __\__ \ "
Write-Host -ForegroundColor "Gray" " ______\____/|_|\__,_| |_| |_|_|\___|___/ "
Write-Host -ForegroundColor "Gray" "|_ __ \ ___ _ __ ___ _____ _____ _ __ "
Write-Host -ForegroundColor "Gray" " | |__) / _ | '_ ' _ \ / _ \ \ / / _ | '__| "
Write-Host -ForegroundColor "Gray" " | __ /| __| | | | | | (_) \ V | __| | "
Write-Host -ForegroundColor "Gray" " _| | \ \___|_| |_| |_|\___/ \_/ \___|_| "
Write-Host -ForegroundColor "Gray" -NoNewline "|____| |__| "
Write-Host -ForegroundColor "DarkGray" "~ Bastiaan Groen ~"
Write-Host -ForegroundColor "DarkGray" -NoNewline "Version: "
Write-Host -ForegroundColor "red" "$($version) "
Write-Host -ForegroundColor "Gray" ""
Write-Host -ForegroundColor "DarkGray" " | [i] Use backward slashes: \"
Write-Host -ForegroundColor "DarkGray" -nonewline " C:\folderToDelete`r"
Write-Host -ForegroundColor "Yellow" -nonewline " ]`r"
Write-Host -ForegroundColor "Yellow" -nonewline " | [>] Fill in a folder path [ "
$path = Read-Host
Write-Host " "
Write-Host -ForegroundColor "DarkGray" " | [i] All files BEFORE this date will be deleted"
Write-Host -ForegroundColor "DarkGray" -nonewline " dd/mm/yyyy`r"
Write-Host -ForegroundColor "Yellow" -nonewline " ]`r"
Write-Host -ForegroundColor "Yellow" -nonewline " | [>] Fill in a date [ "
$datumString = Read-Host
try
{
$limit = [datetime]::parseexact($datumString, 'dd/MM/yyyy', $null)
}
catch
{
SchrijfOutput -level "error" -text "Date was incorrect!"
exit
}
$TotalDelFiles = ( Get-ChildItem -Path $path -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit } | Measure-Object ).Count;
$TotalFiles= ( Get-ChildItem -Path $path -Recurse -Force | Measure-Object ).Count;
$LegeFolders = ( Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Measure-Object ).Count;
Write-Host " "
Write-Host " "
Write-Host -ForegroundColor Yellow "==============================================="
Write-Host -ForegroundColor Yellow "[i] Total files: " -NoNewline
Write-Host -ForegroundColor Gray "$($TotalFiles) "
Write-Host -ForegroundColor Yellow "[i] To be deleted: " -NoNewline
Write-Host -ForegroundColor Gray "$($TotalDelFiles) "
Write-Host -ForegroundColor Yellow "[i] Empty folders: " -NoNewline
Write-Host -ForegroundColor Gray "$($LegeFolders)"
Write-Host -ForegroundColor Yellow "[i] log file: " -NoNewline
Write-Host -ForegroundColor Gray "deletedFiles.log "
Write-Host -ForegroundColor Yellow "[i] log file: " -NoNewline
Write-Host -ForegroundColor Gray "deletedFolders.log "
Write-Host -ForegroundColor Yellow "==============================================="
Write-Host " "
Write-Host -ForegroundColor DarkGray "Deletes: Read-Only files "
Write-Host -ForegroundColor DarkGray "Deletes NOT: Hidden files "
Write-Host -ForegroundColor DarkGray " System files "
#---- bevestig verwijdering ------
$title = "Confirm non reversible removal of files"
$message = "Are you sure you want to delete $($TotalDelFiles) files?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Confirm!"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&NO", `
"Cancel!"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
Write-Host " "
SchrijfOutput -level "info" -text "Start deleting files..."
$hiddenReadOnlyItemes = 0
$verwijderdeBestanden = 0
$verwijderdeFolders = 0
#---- bestanden verwijderen ------
$bestanden = Get-ChildItem -Path $path -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $limit }
foreach ($bestand in $bestanden)
{
try
{
$bestand | Remove-Item -ErrorAction Stop -Verbose 4>&1 | Add-Content "$($path)/deletedFiles.log"
$verwijderdeBestanden += 1
SchrijfOutput -level "update" -text "$($bestand.Name)" -max $TotalDelFiles -updateNumber $verwijderdeBestanden
} Catch [System.IO.IOException]
{
SchrijfOutput -level "error" -text "$($bestand.Name) couldn't be deleted "
$hiddenReadOnlyItemes += 1
} Catch {
SchrijfOutput -level "error" -text "There was an error with deleting a file"
}
}
Write-Host " "
SchrijfOutput -level "done" -text "Done with delting files"
SchrijfOutput -level "done" -text "New log file made: deletedFiles.log"
#---- folders verwijderen ------
Write-Host " "
SchrijfOutput -level "info" -text "Start deleting empty folders..."
$totalDelFolders = ( Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Measure-Object ).Count;
$folders = Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null }
foreach($folder in $folders)
{
try{
if($folder)
{
$folder | Remove-Item -Force -Recurse -ErrorAction Stop -Verbose 4>&1 | Add-Content "$($path)/deletedFolders.log"
$verwijderdeFolders += 1
SchrijfOutput -level "update" -text "$($folder.Name)" -max $totalDelFolders -updateNumber $verwijderdeFolders
}else
{
SchrijfOutput -level "update" -text "[BESTAAT NIET MEER]$($folder.Name)" -max $totalDelFolders -updateNumber $verwijderdeFolders
}
} Catch{
SchrijfOutput -level "update" -text "[BESTAAT NIET MEER]$($folder.Name)" -max $totalDelFolders -updateNumber $verwijderdeFolders
}
}
Write-Host " "
SchrijfOutput -level "done" -text "Done with deleting folders"
SchrijfOutput -level "done" -text "New log file made: deletedFolders.log"
Write-Host " "
Write-Host " "
Write-Host -ForegroundColor Yellow "==============================================="
Write-Host -ForegroundColor Yellow "[i] Total files: " -NoNewline
Write-Host -ForegroundColor Gray "$($TotalFiles) "
Write-Host -ForegroundColor Yellow "[i] deleted files: " -NoNewline
Write-Host -ForegroundColor Green "$($verwijderdeBestanden) "
Write-Host -ForegroundColor Yellow "[i] deleted folders: " -NoNewline
Write-Host -ForegroundColor Green "$($verwijderdeFolders)"
Write-Host -ForegroundColor Yellow "[i] Couldn't delete files: " -NoNewline
Write-Host -ForegroundColor Red "$($hiddenReadOnlyItemes) "
Write-Host -ForegroundColor Yellow "[i] log file: " -NoNewline
Write-Host -ForegroundColor Gray "deletedFiles.log "
Write-Host -ForegroundColor Yellow "[i] log file: " -NoNewline
Write-Host -ForegroundColor Gray "deletedFolders.log "
Write-Host -ForegroundColor Yellow "==============================================="
Write-Host " "
Write-Host " "
SchrijfOutput -level "done" -text "Done... press enter to exit"
$einde = Read-Host