-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailPrep.ps1
More file actions
374 lines (271 loc) · 10.4 KB
/
Copy pathemailPrep.ps1
File metadata and controls
374 lines (271 loc) · 10.4 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
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
<#
.PARAMETER $mode
PrepForEmail, FixAfterEmail, QuickChange
#>
param (
[Parameter(Mandatory=$true,ParameterSetName='Mode')]
[ValidateSet("PrepForEmail", "FixAfterEmail", "QuickChange")]
[ValidateNotNullOrEmpty()]
[string]$mode = "QuickChange"
)
Function New-FolderToPlaceFiles {
param()
[string]$folderPathandName = "$PSScriptRoot\FilesToCopy"
if((Test-Path $folderPathandName) -ne $true){
New-Item -ItemType directory -Path $folderPathandName | Out-Null
Write-Host "Folder Created" -BackgroundColor Blue
}else {
Write-host "Folder already exists"
}
$folderContents = Get-ChildItem $folderPathandName
if ([string]::IsNullOrEmpty($folderContents) -eq $false){
Write-host "Folder has contents. Deleting files" -BackgroundColor Blue
ForEach ($file in $folderContents){
[io.path]::Combine($folderPathandName,$file) | Remove-Item
}
}else {
Write-host "Folder empty."
}
return $folderPathandName
}
Function Get-FilesFromBrowse {
param(
[String]$title = "Choose The Files You Want To Process",
[String]$initialDirectory = "$PSScriptRoot",
[string]$startingFile = "",
[boolean]$multiSelectFlag = $true
)
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = Split-Path $initialDirectory -Parent
$OpenFileDialog.Multiselect = $multiSelectFlag
#$OpenFileDialog.CheckFileExists
#$OpenFileDialog.CheckPathExists
$OpenFileDialog.Title = $title
if ([string]::IsNullOrWhiteSpace($startingFile -ne $true)){
$OpenFileDialog.FileName = Split-path $startingFile -leaf
}
$OpenFileDialog.ShowDialog() | Out-Null
[System.Collections.ArrayList]$fileNames = $OpenFileDialog.SafeFileNames
[System.Collections.ArrayList]$fileLocations = $OpenFileDialog.FileNames
[System.Collections.Hashtable]$fileHash = @{}
For ($i = 0; $i -lt $fileNames.Count; $i++){
$fileHash.Add($fileNames[$i], $fileLocations[$i])
}
#cannot pass an empty value and cannot pass a folder.
if ([string]::IsNullOrEmpty($fileHash) -ne $true -and $fileHash.GetType() -ne [System.IO.FileSystemInfo]){
return $fileHash
}else{
Write-host "Please choose at least one file to prepare for email" -BackgroundColor Red
Get-Files
}
}
Function Get-FilesToConvertBack {
param(
[String]$path
)
Write-host "Grabbing all files from folder"
[System.Collections.ArrayList]$myFiles = @(Get-ChildItem -Path $path -Exclude "*.csv" | Select-Object BaseName, FullName)
[System.Collections.Hashtable]$fileSpecifics = @{}
ForEach ($file in $myFiles){
$fileSpecifics.add($file.BaseName, $file.FullName)
}
Return $fileSpecifics
}
Function Copy-Files {
param (
[System.Collections.HashTable]$filesToCopy,
[string]$filePath
)
ForEach ($key in $filesToCopy.keys){
try{
Copy-Item -Path $filesToCopy[$key] -Destination $filePath -ErrorAction stop
Write-host "$key copied to $filePath"
}catch{
Write-host "$key failed to copy to $filePath"
Write-host $error[0].exception.Message
return $false
}
}
return $true
}
Function Get-AllFileExtensions {
param(
[String]$path
)
[System.Collections.Hashtable]$fileExtensions = @{}
Write-host "Getting all file extensions"
Get-ChildItem -Path $path -Exclude "*.csv" | ForEach-Object {
[string]$name = $_.BaseName
[string]$extension = $_.Extension
Write-host ":::::Name: $name - Extension: $extension"
$fileExtensions.add($name, $extension)
}
Write-host "Complete"
return $fileExtensions
}
Function Save-OriginalFileExtensionsAndNamesToCSV {
param (
[string]$Path
)
[System.Collections.Hashtable]$fileExtensions = Get-AllFileExtensions -path $path
$csvStoredPath = [io.path]::Combine($path,"FileExtensions.csv")
Write-host "Saving original file extensions to $csvStoredPath"
Try {
$fileExtensions.GetEnumerator() | Select-Object Key,Value | Export-Csv -path $csvStoredPath -NoTypeInformation
}catch {
Write-host $error[0].exception.message
Write-host "Export failed!"
Return $false
}
return $true
}
Function Set-FileExtensionToTxt {
param(
[System.Collections.HashTable]$myFiles,
[string]$folderPath
)
ForEach ($file in $myFiles.keys){
$newFileName = [io.path]::ChangeExtension("$file", "txt")
$pathTofile = [io.path]::Combine($folderPath, $file)
try {
rename-item -Path "$pathToFile" -newname $newFileName
}catch{
Write-host "File rename failed"
Write-host $error[0].exception.message
return $false
}
}
return $true
}
Function Get-FileNamePairedOriginalFileExtensionsFromCSV{
param()
[System.Collections.Hashtable]$csvFileInformation = Get-FilesFromBrowse -title "Get CSV File That Was Generated" -multiSelectFlag $false
[string]$csvStoredPath = $csvFileInformation.Values
[System.Collections.Hashtable]$FileNamePairedOriginalFileExtensions = @{}
Import-Csv -Path $csvStoredPath | ForEach-Object { $FileNamePairedOriginalFileExtensions.add($_.key, $_.value)}
return $FileNamePairedOriginalFileExtensions
}
Function Set-FileExtensionToOriginal {
param(
[System.Collections.hashtable]$FilesPairedFileLocations,
[System.Collections.Hashtable]$FileNamePairedOriginalFileExtensions,
[string]$Path
)
[System.collections.hashtable]$FileNamePairedOriginalFileExtensions = @{}
[System.Collections.ArrayList]$newFileNames = @()
Write-host "Changing extensions back to original"
Try {
ForEach ($item in $FilesPairedFileLocations.Keys){
$file = $item.BaseName
ForEach ($key in $FileNamePairedOriginalFileExtensions.keys){
if($key -ne $file){
continue
}else{
$newFileName = [io.path]::ChangeExtension($file, $FileNamePairedOriginalFileExtensions[$key])
$pathTofile = [io.path]::Combine($Path, $File + $item.extension)
rename-item -Path $pathToFile -newname $newFileName
Write-host "::::$file changed to $newFileName"
$newFileNames.add($newFileName)
break
}
}
}
}catch{
Write-host $error[0].exception.message
}
return $newFileNames
}
Function Clear-Folder {
param(
[string]$path
)
Write-host "Cleaning up folder and CSV"
Get-ChildItem -path $path -Exclude "*.csv" | Remove-Item -Recurse
$csvStoredPath = [io.path]::Combine($path, "fileExtensions.csv")
Clear-Content -path $csvStoredPath -Force
}
Function Get-ExtensionFromUser {
param()
[string]$extension = Read-Host -Prompt "What would you like to change these files to? ex. .ps1"
if ([string]::IsNullOrWhiteSpace($extension) -or $extension.GetType() -ne [string]){
Get-ExtensionFromUser
} elseif ($extension -eq "Exit"){
exit
} elseif ($extension.StartsWith(".") -ne $true){
Write-host "Please put the dot before the extension name. Ex. .ps1"
Get-ExtensionFromUser
}elseif ($extension.Length -gt 4) {
Write-Host "Please input an extension or type EXIT to get out of the script"
Get-ExtensionFromUser
}
return $extension
}
Function Set-ExtensionFromUser {
param (
[string]$extension,
[System.Collections.Hashtable]$fileNamesPairedFileLocation
)
try{
ForEach ($FileName in $fileNamesPairedFileLocation.Keys){
$newFileName = [io.path]::ChangeExtension($fileName, $extension)
$pathTofile = $fileNamesPairedFileLocation[$FileName]
rename-item -Path $pathToFile -newname $newFileName
Write-host "::::$fileBaseName changed to $newFileName"
}
}catch{
Write-host $error[0].exception.message
return $false
}
return $true
}
Function Main {
[string]$filePath
[System.Collections.HashTable]$filesToCopy
[System.Collections.Hashtable]$originalFileExtensions
[System.Collections.Hashtable]$filesToFix
[System.Collections.Hashtable]$fileNamesPairedFileLocation
$resultFlag = $false
if ($mode -eq "PrepForEmail"){
$filesToCopy = Get-FilesFromBrowse
$filePath = New-FolderToPlaceFiles
Copy-Files $filesToCopy $filePath
Save-OriginalFileExtensionsAndNamesToCSV -Path $filePath
$resultFlag = Set-FileExtensionToTxt $filesToCopy $filePath
if ($resultFlag -eq $true){
Write-host "Process completed succesfully"
}else {
Write-host "Process failed!"
}
}elseif ($mode -eq "FixAfterEmail"){
[string]$savePath = $PSScriptRoot
[boolean]$copyFlag = $false
$filePath = "$PSScriptRoot\FilesToCopy"
$filesToConvert = Get-FilesToConvertBack -path $filePath
$originalFileExtensions = Get-FileNamePairedOriginalFileExtensionsFromCSV
$filesToCopy = Set-FileExtensionToOriginal -myFiles $filesToConvert -Path $filePath -originalFileExtensions $originalFileExtensions
$copyFlag = Copy-Files $filesToCopy $savePath
if ($copyFlag -eq $true){
Clear-Folder -path $filePath
Write-host "Process Complete!"
}else{
Write-host "Copy Failed, delete stopped"
$resultFlag = $false
}
}elseif ($mode -eq "QuickChange"){
$fileNamesPairedFileLocation = Get-FilesFromBrowse
$extension = Get-ExtensionFromUser
$successFlag = Set-ExtensionFromUser -extension $extension -fileNamesPairedFileLocation $fileNamesPairedFileLocation
if($successFlag -eq $true){
Write-host "Process completed successfully"
}else {
Write-host "Process failed!"
}
}else {
Write-host "Invalid Argument"
Write-host "*----------------------*"
Write-host "To Prep for Email Delivery : PrepForEmail"
Write-host "To fix after Email Delivery: FixAfterEmail"
}
}
Main