-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleLineUtility.ps1
226 lines (116 loc) · 5.27 KB
/
SimpleLineUtility.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
# ============== Simple Line Utility =====================
#
#
#---- MIT LICENSE Copyright 2019 ---Author: Steven Soward
#
#-- establishes the date/time format for use later in the temp filename
$date = Get-Date -Format 'MM-dd-yyyy.HH.mm'
# --this "add-type" line has to be added ad the beginning of the script to use "messagebox" in regular powershell execution, otherwise it will only work in ps ise.
Add-Type -AssemblyName PresentationFramework
#_____________ --intro message box:
$userresponse=[System.Windows.MessageBox]::Show('This Utility will trim/expand the lines text file (i.e. csv, txt, xml) file to a specific length
You can also convert the line endings (EOL) to Uinux if desired.
Press OK to Choose the input File', 'Simple Line Utility','okcancel')
if ($UserResponse -eq "ok" )
{
#Yes activity
}
else
{
exit
}
#_____________ --this opens the 'choose file' dialogue:
Function Get-FileName($initialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
if ($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{ $OpenFileDialog.FileName }
else
{exit}
$Global:SelectedFile = $OpenFileDialog.FileName
} #end function Get-FileName
Get-FileName
#_____________ --this asks the user what the desired line lenghth is for the output file
Add-Type -AssemblyName Microsoft.VisualBasic
$desiredlength = [Microsoft.VisualBasic.Interaction]::InputBox('Enter The Desired Line Length', 'Simple Line Utility - Choose Line Length', "-Numeric Value-")
if ($desiredlength -eq "" )
{
exit
}
else
{
#nothing
}
#_____________ --this asks the user what the desired output filename is
Add-Type -AssemblyName Microsoft.VisualBasic
$desiredfilename = [Microsoft.VisualBasic.Interaction]::InputBox('Enter The Desired Output Filename and File Extension:', 'Simple Line Utility - Choose Output Filename', "-- yourfile.csv, yourfile.txt, or yourfile.sql--")
if ($desiredfilename -eq "" )
{
exit
}
else
{
#nothing
}
$linelength = ($desiredlength)-(1)
## ------ Looks at the designated input file and saves it to a temp file
Get-Content -Path $SelectedFile >> C:\Users\$env:UserName\Desktop\INCOMPLETE$desiredfilename-$date.txt
#_______________________________-- Pads or Truncates Line Size if it's Less than $_______________
$calculatedpad = ($linelength)-($_.length)
Get-Content C:\Users\$env:UserName\Desktop\INCOMPLETE$desiredfilename-$date.txt | ForEach {
If ($_.Length -lt $linelength) {
$_.padright($calculatedpad)
$notif = 'The file has been padded and placed on your desktop'
} Else {
$_.Substring(0,$linelength)
$notif = 'The file has been truncated and placed on your desktop'
}
#________________________________ --- saves the final version of the file --- ______________________
} | Out-File C:\Users\$env:UserName\Desktop\$desiredfilename
#_______________________________________ --- Cleans up the Temp File --- _________________________
Remove-Item -path C:\Users\$env:UserName\Desktop\* -include INCOMPLETE$desiredfilename-*.txt
$fileforeolcovert = "C:\Users\$env:UserName\Desktop\$desiredfilename"
<#______Truncate if line size Greater than $ (this is not needed since I've included $_.Substring(0,$linelength) in the if/else statement above)_____________________________
Get-Content C:\Users\$env:UserName\Desktop\INCOMPLETE$desiredfilename-$date.txt | ForEach {
If ($_.Length -gt $linelength) {
$_.Substring(0,$linelength)
} Else {
$_
}
#________________________________ --- save the final version of the file --- ______________________
} | Out-File C:\Users\$env:UserName\Desktop\$desiredfilename.txt
#_______________________________________ --- Clean up the Temp File --- _________________________
Remove-Item -path C:\Users\$env:UserName\Desktop\* -include INCOMPLETE$desiredfilename-*.txt
#>
###___________removes extra blank line(s) from bottom__________
$Newtext = (Get-Content -Path $fileforeolcovert -Raw) -replace "(?s)`r`n\s*$"
[system.io.file]::WriteAllText($fileforeolcovert,$Newtext)
#_____________--- ask about EOL text box --- ________
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
$eolresponse=[Microsoft.VisualBasic.Interaction]::MsgBox("Do you want to convert the output file to contain Unix Line Endings (EOL)?",'YesNoCancel,Question', "Simple Line Utility - Convert EOL")
if ($eolresponse -eq "yes" )
{
$filecontent = [IO.File]::ReadAllText($fileforeolcovert) -replace "`r`n", "`n"
[IO.File]::WriteAllText($fileforeolcovert, $filecontent)
$notif += "
Additionally, Line endings have been converted to Unix."
}
else
{
#exit
}
#_________--- converts eol to unix --- __________
#$filecontent = [IO.File]::ReadAllText($fileforeolcovert) -replace "`r`n", "`n"
#[IO.File]::WriteAllText($fileforeolcovert, $filecontent)
#_______________________________________ --- Ending Notification --- _________________________
$userresponseend=[System.Windows.MessageBox]::Show("$notif", 'Simple Line Utility - Conversion Complete','ok')
if ($UserResponseend -eq "ok" )
{
#Yes activity
}
else
{
exit
}