-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStevensCSVcombiner.ps1
127 lines (64 loc) · 2.98 KB
/
StevensCSVcombiner.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
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic
write-host "
================= csv File combiner =====================
Important Notes:
-- This utility will accept multiple csv files and combine them into one csv
-- The first file's headings will be kept, all others will be skipped
- The output file will be written to the same path chosen as the input directory
==================================================================
---- MIT LICENSE Copyright 2019 --- Author: Steven Soward | v1.0
"
######_______ makes a log of activities:
start-transcript -Path $outputpath\csvlog.txt -Append
$date = Get-Date -Format 'MM-dd-yy hh.mmtt'
#####_________this "add-type" line has to be added ad the beginning of the script to use "messagebox" in regular powershell execution, otherwise the script will only work in ps ise.
Add-Type -AssemblyName PresentationFramework
#--------------------------------------------------------------
#####________ Shows the intro message box:
$userresponse=[System.Windows.MessageBox]::Show('--This utility will import all .CSV files in the chosen directory and combine them into one .CSV file.
-- Only the column headings of the first file processed will be kept
-Press OK to choose the input directory', 'OBU CSV File Combiner','okcancel')
if ($UserResponse -eq "ok" )
{
#Yes activity (script will move on since this is null)
}
else
{
write-host "cleaning-up temp files and closing"
exit
}
#--------------------------------------------------------------
########_________________this opens the choose file dialogue:
$dest = [System.Windows.Forms.FolderBrowserDialog]::new()
$dest.ShowDialog()
### set the variable for the destination path
$selectedpath = $dest.SelectedPath
#--------------------------------------------------------------
#get-childItem "$selectedpath\*.csv" | foreach {[System.IO.File]::AppendAllText("$selectedpath\combinedCSV-$date.csv", [System.IO.File]::ReadAllText($_.FullName))}
#Import-Csv -Path (Get-ChildItem -Path $selectedpath -Filter '*.csv').FullName | Set-Content -Path "$selectedpath\EmployeeReport.csv"
$includefirstLine = $true
get-childItem "$selectedpath\*.csv" | foreach {
$csvfile = $_
$lines = Get-Content $csvfile
$outputlines = switch($includefirstline) {
$true {$lines}
$false {$lines | Select -Skip 1}
}
$includefirstline = $false
Add-Content "$selectedpath\CombinedCSV-$date.csv" $outputlines
}
$userresponseend=[System.Windows.MessageBox]::Show("The .CSV file has been generated and placed in:
$selectedpath
", 'csv File combiner','ok')
if ($UserResponseend -eq "ok")
{
ii $selectedpath
#Yes activity
write-host "cleaning-up temp files and closing"
stop-transcript
exit
}
stop-transcript
exit