-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDupSearch_Lite.ps1
177 lines (142 loc) · 6.49 KB
/
DupSearch_Lite.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
####################################################################
#DupSearch_Lite 1.0 - Script by @capncruncky github.com/capncruncky
#This script searches the current directory and sub-directories for duplicate files
# - comparing files by using file name and file length as comparison -
#then lists the paths of duplicate files for manual removal.
#Tip: Make sure to backup your files before doing any deleting!!!!!
#Tip: Pipe the output to a text file for reference!
#Usage: C:\Dir DupSearch_Lite.ps1
#Log: [PATH]\DupSearch_Lite_Log.txt
#####################################################################
#Initialize Variables
$count = 1
#Display Duplicate Files
$path = $pwd
#Display Info
Set-Content -Path $path/DupSearch_Lite_Log.txt "`n`n`n`n`n`n
#DupSearch_Lite 1.0 - Script by @capncruncky github.com/capncruncky
#This script searches the current directory and sub-directories for duplicate files
# - comparing files by using file name and file length as comparison -
#then lists the paths of duplicate files for manual removal.
#Tip: Make sure to backup your files before doing any deleting!!!!!
#Tip: Pipe the output to a text file for reference!
#Usage: C:\Dir DupSearch_Lite.ps1
#Log: [PATH]\DupSearch_Lite_Log.txt`n
" -PassThru
#Recurse through folder to collect all files and store in variable
$fileCollection = Get-ChildItem -File -Recurse
#Total number of files
$TotalFiles = $fileCollection.count
#Write total to output
Add-Content -Path $path/DupSearch_Lite_Log.txt "`nTotal number of Files: $TotalFiles`n" -PassThru
#Create Container(array) to hold matched files...
$MatchingFiles = @()
#Create Container(array) for storing Matched Source Files
$MatchedSourceFiles = @()
#Create Container(arra) for dipsplaying final results
$DuplicateResults = @()
foreach($file in $fileCollection)
{
#Display Progress Bar...
Write-Progress -Activity "Comparing Files" -status "Processing File $count / $TotalFiles" -PercentComplete ($count / $TotalFiles * 100)
#Display file information...
Add-Content -Path $path/DupSearch_Lite_Log.txt "Path: $($file.FullName)`t Size: $($file.Length / 1000) KB" #-PassThru
#Check for Matching File Names and Length...
$MatchingFiles = $fileCollection | Where-Object {($_.Name -eq $file.Name) -and ($_.Length -eq $file.Length)}
#Cycle through files in collection meeting above criteria...
foreach($matchEntry in $MatchingFiles)
{
#Check files for various requirements...
if(($file -ne $matchEntry) -and !(($MatchedSourceFiles) -contains $matchEntry))
{
#Write Output...
Add-Content -Path $path/DupSearch_Lite_Log.txt "`nFile Names and Lengths Match! `n*$($file.FullName)`n*$($matchEntry.FullName)" #-PassThru
#Write Output...
Add-Content -Path $path/DupSearch_Lite_Log.txt "`n`t`tDUPLICATE FOUND!: $($file.Name)" #-PassThru
Add-Content -Path $path/DupSearch_Lite_Log.txt "`t$($file.FullName)`n`t$($matchEntry.FullName)`n" #-PassThru
#Add match to array of matched files...
$MatchedSourceFiles += $matchEntry
#Create new output object for $file match...
$NewObject=[pscustomobject][ordered]@{
File_Name = $file.Name
File_Path = $file.FullName
#File_Hash = $fileHash
File_Size = $file.Length
}
#Create new output object for $matchentry match...
$NewObject2=[pscustomobject][ordered]@{
File_Name = $matchEntry.Name
File_Path = $matchEntry.FullName
#File_Hash = $matchEntryHash
File_Size = $matchEntry.Length
}
#Add new object to Duplicate Results array for processing later...
$DuplicateResults += $NewObject
$DuplicateResults += $NewObject2
}
}
#Increment Counter...
$count += 1
}
#Organize Objects in array
$GroupResults = $DuplicateResults
$FinalResults = @()
$tempSize = 0
$FinalSize = 0
#Cycle through all Duplicates...
foreach($result in $DuplicateResults)
{ #...
foreach($object in $GroupResults)
{
#Check for matches and uniqueness of files for storing in Final Results array
if(($object.File_Name -eq $result.File_Name) -and ($object.File_Path -ne $result.File_Path) -and ($object.File_Hash -eq $result.File_Hash) -and ($FinalResults.File_Path -notcontains $object.File_Path) -and ($FinalResults.File_Path -notcontains $result.File_Path))
{
#Accumulate file size into aggregate file size total
$tempSize += $object.File_Size
#Create new output object for match...
$FinalObject=[pscustomobject][ordered]@{
File_Name = $result.File_Name
File_Path = $result.File_Path
Duplicate_Name = $object.File_Name
Duplicate_Path = $object.File_Path
}
#Add object to Final Results container
$FinalResults += $FinalObject
}
}
}
#Calculate Storage Total Size
$FinalSize = $tempSize / 1024
$unit = "kB"
if($FinalSize -gt 1024000)
{
$FinalSize = $FinalSize / 1024000
$unit = "GB"
}
else
{
if($FinalSize -gt 1024)
{
$FinalSize = $FinalSize / 1024
$unit = "MB"
}
}
#Write Output...
Add-Content -Path $path/DupSearch_Lite_Log.txt "`n************************************************************" -PassThru
Add-Content -Path $path/DupSearch_Lite_Log.txt "`t`tRESULTS OF SCAN" -PassThru
Add-Content -Path $path/DupSearch_Lite_Log.txt "************************************************************`n" -PassThru
Add-Content -Path $path/DupSearch_Lite_Log.txt "Total of Duplicate Files: $($FinalResults.count)" -PassThru
Add-Content -Path $path/DupSearch_Lite_Log.txt "`nTotal of Storage Space used by duplicate files: $([math]::Round($FinalSize,3))$unit" -PassThru
#Write Output based on size...
if($FinalResults.Count -lt 20)
{
$FinalResults | Format-List -GroupBy File_Hash
Add-Content -Path $path/DupSearch_Lite_Log.txt "`nSee: $path\DupSearch_Lite_Log.txt for detailed results..." -PassThru
}
else
{
Add-Content -Path $path/DupSearch_Lite_Log.txt "`nSee: $path\DupSearch_Lite_Log.txt for detailed results..." -PassThru
}
#Write Output to log in correct format...
$FinalResults | Format-List -GroupBy File_Name | Out-File -FilePath $path/DupSearch_Lite_Log.txt -Append -Encoding utf8
#########End