-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteDuplicateFiles.ps1
386 lines (304 loc) · 14.1 KB
/
deleteDuplicateFiles.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# C:\Windows\System32\WindowsPowerShell\v1.0
<#______________________________________________________________________________________________________________________
(c) Vitaly Ruhl 2021-2022
Homepage: Vitaly-Ruhl.de
Github:https://github.com/vitalyruhl/
License: GNU General Public License v3.0
______________________________________________________________________________________________________________________#>
#>
$Funktion = 'deleteDuplicateFiles.ps1'
<#
______________________________________________________________________________________________________________________
Version Datum Author Beschreibung
------- ---------- ----------- -----------
#>
$Version = 'V1.0.0' # 29.03.2021 Vitaly Ruhl init
$Version = 'V1.0.1' # 03.04.2021 Vitaly Ruhl init publish
<#
______________________________________________________________________________________________________________________
Function:
delete duplicate Files - Steps 2+3 from 3 (Created CSV and reviewed (Step2) - Delete or move Files (Step3))
______________________________________________________________________________________________________________________
#>
#**********************************************************************************************************************
# Imports
. .\module\recentlyUsedFunctions.ps1 #Import some Functions
#**********************************************************************************************************************
#**********************************************************************************************************************
# Settings
$CSVTable = ".\All-Duplicate-Files.csv"
SetDebugState($false) #activate some debug informations
$AdminRightsRequired = $false #set $true, if Admin-Rights are for the Script reqired
#**********************************************************************************************************************
$global:Modul = 'Main'
Clear-Host
#region begin Request admin rights
if ($AdminRightsRequired) {
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
##https://www.heise.de/ct/hotline/PowerShell-Skript-mit-Admin-Rechten-1045393.html
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$princ = New-Object System.Security.Principal.WindowsPrincipal($identity)
if (!$princ.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
$powershell = [System.Diagnostics.Process]::GetCurrentProcess()
$psi = New-Object System.Diagnostics.ProcessStartInfo $powerShell.Path
$script = $MyInvocation.MyCommand.Path
$prm = $script
foreach ($a in $args) {
$prm += ' ' + $a
}
$psi.Arguments = $prm
$psi.Verb = "runas"
[System.Diagnostics.Process]::Start($psi) | Out-Null
return;
}
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
#endregion
#region Functions Main-Dialog
function CheckToDelItems() {
Write-Debug "Move Items are called"
. .\module\checkform.ps1
}
function MoveItems() {
SetDebugState($false)
$global:Modul = "MoveItems()"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
write-Debug "Move Items are called"
$SD = Get-ScriptDirectory
Write-Host "Please select directory to moved in..."
$SerchPath = Get-FolderDialog ("$SD")
if ($SerchPath -eq "-CANCEL-"){
Write-Warning "No Folder selected - Exit Script"
exit
}
elseif ($SerchPath -eq "-ERROR-") {
Write-Error "Error in Get-Folder-Dialog - Exit Script"
exit
}
#$SerchPath | Get-Member
Write-host "Selected folder for Backup:$SerchPath"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Load CSV'
$csv = import-Csv -Path $CSVTable #| Select-Object -Property MD5Hash, Name ,Extension, FullName, DirectoryName, CreationTime ,LastWriteTime ,MarkToDelete
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Get files to delete:'
$FilesToDelete = $csv | Where-Object{$_.MarkToDelete -eq $true}
#$FilesToDelete | Write-debug
$FilesToDelete | ForEach-Object {
$dPath = $_.DirectoryName.replace("\","_").replace(":","-").replace(" ","")
$MovePath = $SerchPath + '\' + $dPath + '\'
Write-debug "Move to Path:$MovePath"
try {
add-path $MovePath
$it = Get-Item $(Resolve-Path $_.FullName)
Write-debug "File to move is selected -->"
$it | Write-debug
Write-debug "<-- File to move"
$MovePath = $(Resolve-Path $MovePath)
$it | Move-Item -Destination $MovePath -Force
}
catch {
Write-Warning "$global:Modul - Something went wrong on $_"
}
}
}
function DeleteItems() {
$global:Modul = "DeleteItems()"
SetDebugState($false)
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
write-Debug "Delete Items are called"
Write-debug 'Load CSV'
$csv = import-Csv -Path $CSVTable #| Select-Object -Property MD5Hash, Name ,Extension, FullName, DirectoryName, CreationTime ,LastWriteTime ,MarkToDelete
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Get files to delete:'
$FilesToDelete = $csv | Where-Object{$_.MarkToDelete -eq $true}
#$FilesToDelete | Write-debug
$FilesToDelete | ForEach-Object {
try {
$it = Get-Item $(Resolve-Path $_.FullName)
Write-debug "File to delete is selected -->"
$it | Write-debug
Write-debug "<-- File to delete"
#$it | remove-Item -Force
$it | Send-ToRecycleBin #-Force
}
catch {
Write-Warning "$global:Modul - Something went wrong on $_"
}
}
}
function DeleteEmptyFolder() {
SetDebugState($true)
$global:Modul = "DeleteEmptyFolder()"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
write-Debug "DeleteEmptyFolder are called"
$SD = Get-ScriptDirectory
Write-Host "Please select directory to search in..."
$SerchPath = Get-FolderDialog ("$SD")
if ($SerchPath -eq "-CANCEL-"){
Write-Warning "No Folder selected - Exit Script"
exit
}
elseif ($SerchPath -eq "-ERROR-") {
Write-Error "Error in Get-Folder-Dialog - Exit Script"
exit
}
else {
#$SerchPath | Get-Member
Write-host "Selected folder for Search is:$SerchPath"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Get Folder to delete:'
#https://stackoverflow.com/questions/28631419/how-to-recursively-remove-all-empty-folders-in-powershell
<########################################################################
define a script block that will remove empty folders under a root folder,
using tail-recursion to ensure that it only walks the folder tree once.
-Force is used to be able to process hidden files/folders as well.
########################################################################>
$tailRecursion = {
param(
$Path
)
try {
foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $Path -Directory) {
& $tailRecursion -Path $childDirectory.FullName
}
$currentChildren = Get-ChildItem -Force -LiteralPath $Path
$isEmpty = $null -eq $currentChildren
if ($isEmpty) {
Write-Verbose "Removing empty folder at path '${Path}'." -Verbose
#todo add a custom form to confirm , confirm all where i can see all the Folder to delete! -> make a hashtable hier and delete later...
Remove-Item -Force -LiteralPath $Path -Confirm:$true #-WhatIf
}
}
catch {
Write-Warning "$global:Modul - Something went wrong"
}
}
& $tailRecursion -Path $SerchPath
}
}
function okButtonClick (){
#Radiobuttons...
#foreach ($o in @($radioButton1, $radioButton2, $radioButton3)){
# if ($o.Checked){
# $option = $o.Text}
# }
if ($radioButton1.Checked){CheckToDelItems}
elseif ($radioButton2.Checked) {MoveItems}
elseif ($radioButton3.Checked) {DeleteItems}
elseif ($radioButton4.Checked) {DeleteEmptyFolder}
#checkboxes
#If ($objTypeCheckbox.Checked = $true)
#{
# write-host "Checkbox is checked"
#}
#else {
# write-host "Checkbox is NOT checked"
#}
#wh
#$inpText = $textBox.Text
#write-host "Input-Text: $inpText"
#write-host "Selected Option: $option"
$form.Dispose()
}
#endregion
#region Functions Form Check
function MarkToDelete {
param (
$MarkBy
)
SetDebugState($false)
Write-Debug "param:[$MarkBy]"
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Load CSV'
$csv = import-Csv -Path $CSVTable #| Select-Object -Property MD5Hash, Name ,Extension, FullName, DirectoryName, CreationTime ,LastWriteTime ,MarkToDelete
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Sort and get unique'
switch ($MarkBy) {
"CT" {
$Dupcsv = $csv | Group-Object MD5Hash | Where-Object{$_.count -gt 1} | ForEach-Object {
$one = $false
$_.Group | Sort-Object CreationTime -Descending | ForEach-Object {
$_.MarkToDelete = $one
$one = $true
$_
}
}
}
"LW" {
$Dupcsv = $csv | Group-Object MD5Hash | Where-Object{$_.count -gt 1} | ForEach-Object {
$one = $false
$_.Group | Sort-Object LastWriteTime -Descending | ForEach-Object {
$_.MarkToDelete = $one
$one = $true
$_
}
}
}
"UN" {
$Dupcsv = $csv | Group-Object MD5Hash | Where-Object{$_.count -gt 1} | ForEach-Object {
$one = $false
$_.Group | ForEach-Object {
$_.MarkToDelete = $one
$one = $true
$_
}
}
}
Default {
$Dupcsv = $csv | Group-Object MD5Hash | Where-Object{$_.count -gt 1} | ForEach-Object {
$one = $false
$_.Group | ForEach-Object {
$_.MarkToDelete = $one
$one = $true
$_
}
}
}
}
#$Dupcsv = $csv | Where-Object{$_.isFolder -eq $False} | Select-Object -Property MD5Hash, Name ,Extension, FullName, DirectoryName, CreationTime ,LastWriteTime
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Debug CSV --> '
$Dupcsv | Write-debug
Write-debug '<--- Debug CSV'
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'export CSV'
#$Dupcsv | Add-Member -MemberType NoteProperty -Name 'MarkToDelete' -Value ($false)
$Dupcsv | Export-Csv -Path $CSVTable -Force
Write-Host "Table $CSVTable is updated - please check the Files to deletion for security!" -ForegroundColor red
}
function MarkByCreateTime() {
write-Debug "MarkByCreateTime are called"
MarkToDelete "CT"
}
function MarkByLastWriteTime() {
write-Debug "MarkByLastWriteTime are called"
MarkToDelete "LW"
}
function LeaveFirstOne() {
write-Debug "LeaveFirstOne are called"
MarkToDelete "UN"
}
function okButtonClickCheck (){
if ($radioButton1.Checked){MarkByCreateTime}
elseif ($radioButton2.Checked) {MarkByLastWriteTime}
elseif ($radioButton3.Checked) {LeaveFirstOne}
$form.Dispose()
}
#endregion
#region Main
SetDebugState($false)
Write-debug "`r`n`r`n------------------------------------------------------`r`n"
Write-debug 'Perform formular'
. .\module\mainform.ps1 #Open Main-Dialog
Write-debug 'done'
#exit
#endregion
Write-Host "`r`n`r`n"
Write-Warning "------------------------------------------------------`r`n"
Write-Warning 'Skript is done!'
Write-Warning 'When you dont see any red than is all fine ;-)'#-ForegroundColor Green
#start-countdown 30
#pause