-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortDownLoadDirectory.ps1
More file actions
163 lines (115 loc) · 5.51 KB
/
SortDownLoadDirectory.ps1
File metadata and controls
163 lines (115 loc) · 5.51 KB
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
param(
[string]$DownloadDirectory="C:\Home\download",
[bool]$ConfirmSort=$false
)
#$DownloadDirectory = "E:\Downloads"
$zipDirectory = $DownloadDirectory + "\zip"
$ExeDirectory =$DownloadDirectory + "\exe"
$TextDirectory = $DownloadDirectory + "\text"
$IsoDirectory =$DownloadDirectory + "\iso"
$MiscDirectory =$DownloadDirectory + "\misc"
$imageDirectory =$DownloadDirectory + "\image"
$logDirectory = $DownloadDirectory + "\log"
$scriptsDirectory = $DownloadDirectory + "\scripts"
function VerifyRun (){
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Are you sure you want to Sort the download directory"
$MessageTitle = "Confirm Sort"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
return $Result
}
function RemoveEmptyDirectories([string] $path){
If ([string]::IsNullOrEmpty($path) -eq $true)
{
Throw "The path must be specified."
}
[bool] $pathExists = Test-Path $path
If ($pathExists -eq $false)
{
Throw "File does not exist (" + $path + ")"
}
$numberOfFiles = Get-ChildItem -Path $path -Recurse | Measure-Object
if ($numberOfFiles.Count -eq 0){
Write-Host "The path " + $path +" has files no files so deleting "
Remove-Item -Recurse -Force some_dir
}
}
If ((Test-Path $DownloadDirectory) -eq $false) {
New-Item -ItemType Directory -Path $DownloadDirectory -Force
}
if ($ConfirmSort -eq $True){
$Result = VerifyRun
if($Result -eq "No"){
Exit
}
}
#Ensure the directories are created. If not then create them.
If ((Test-Path $logDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $logDirectory -Force
}
RemoveEmptyDirectories $logDirectory
If ((Test-Path $scriptsDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $scriptsDirectory -Force
}
If ((Test-Path $zipDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $zipDirectory -Force
}
If ((Test-Path $ExeDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $ExeDirectory -Force
}
If ((Test-Path $TextDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $TextDirectory -Force
}
If ((Test-Path $IsoDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $IsoDirectory -Force
}
If ((Test-Path $MiscDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $MiscDirectory -Force
}
If ((Test-Path $imageDirectory ) -eq $false) {
New-Item -ItemType Directory -Path $imageDirectory -Force
}
$IncludeIsoFiles = ("*.iso")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeIsoFiles | Move-Item -Destination $IsoDirectory -verbose
$IncludeScriptFiles = ("*.ps1","*.sql","*.bat")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeScriptFiles | Move-Item -Destination $scriptsDirectory -verbose
$IncludeLogFiles = ("*.log")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeLogFiles | Move-Item -Destination $logDirectory -verbose
$IncludeTxtFiles = ("*.txt","*.doc","*.docx","*.pdf","*.xlsx","*.csv","*.rtf")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeTxtFiles | Move-Item -Force -Destination $TextDirectory -verbose
$IncludeZipFiles = ("*.zip","*.tar","*.7zip", "*.rar","*.7z")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeZipFiles | Move-Item -Force -Destination $zipDirectory -verbose
$IncludeExeFiles = ("*.EXE","*.exe", "*.msi","*.vsix")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeExeFiles | Move-Item -Force -Destination $ExeDirectory -verbose
$IncludeImageFiles = ("*.png","*.jpg","*.JPG","*.JFIF","*.jfif")
Get-ChildItem "$DownloadDirectory\*" -Include $IncludeImageFiles | Move-Item -Destination $imageDirectory -verbose
#Move everything elese into the miscellaneous directory and compress them while you do it.
Add-Type -assembly "system.io.compression.filesystem"
$ExcludesDirectory =($zipDirectory,$ExeDirectory,$TextDirectory,$IsoDirectory,$imageDirectory,$MiscDirectory,$logDirectory,$scriptsDirectory)
$PickupDirectoryPath = Get-ChildItem "$DownloadDirectory" -Exclude $ExcludesDirectory | Where-Object {($_.Attributes -match 'Directory')}
foreach ($Path in $PickupDirectoryPath)
{
if (($ExcludesDirectory.Contains($Path.FullName) -eq $false) ){
write-host $Path
Move-Item $path -Destination $MiscDirectory -verbose
}
}
#compress the stuff in the misc diretory.
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$FilestoZip = Get-ChildItem "$MiscDirectory" | Where-Object {($_.Attributes -notmatch 'compressed') -and ($_.Attributes -match 'Directory') }
foreach ($Path in $FilestoZip)
{
$processingFile = $Path.FullName +".zip"
If ((Test-Path $processingFile ) -eq $True) {
Remove-Item $Path.FullName -Recurse -Force
}
[System.IO.Compression.ZipFile]::CreateFromDirectory($Path.FullName,
$Path.FullName +".zip",
$compressionLevel,
$true)
}
#$ExcludesDirectory =($zipDirectory,$ExeDirectory,$TextDirectory,$IsoDirectory,$imageDirectory)
#Get-ChildItem "$DownloadDirectory\*" -Exclude $ExcludesDirectory | where {$_.Attributes -match 'Directory' -and ($_.Parent -eq $DownloadDirectory ) } | Move-Item -Destination $MiscDirectory -verbose