<#
.NAME
XML-GUI.ps1
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '246,178'
$Form.text = "XML Mulcher"
$Form.BackColor = "#cccccc"
$Form.TopMost = $false
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.Text = ""
$TextBox1.multiline = $false
$TextBox1.ReadOnly = $true
$TextBox1.Width = 185
$TextBox1.height = 20
$TextBox1.Location = New-Object System.Drawing.Point(16,20)
$TextBox1.Font = 'Microsoft Sans Serif,10'
$ListBox1 = New-Object system.Windows.Forms.ListBox
$ListBox1.text = ""
$ListBox1.width = 100
$ListBox1.height = 56
@('Classification','ProductType','ProductID') | ForEach-Object {[void
] $ListBox1.Items.Add
($_)} $ListBox1.location = New-Object System.Drawing.Point(16,50)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.Text = "Processing:"
$Label1.width = 68
$Label1.height = 16
$Label1.location = New-Object System.Drawing.Point(16,146)
$Label1.Font = 'Microsoft Sans Serif,8'
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.ReadOnly = $true
$TextBox2.Width = 140
$TextBox2.height = 16
$TextBox2.Location = New-Object System.Drawing.Point(88,144)
$TextBox2.Font = 'Microsoft Sans Serif,8'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Go"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(171,65)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "..."
$Button2.width = 25
$Button2.height = 25
$Button2.location = New-Object System.Drawing.Point(206,19)
$Button2.Font = 'Microsoft Sans Serif,10'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.Text = "Output:"
$Label2.width = 60
$Label2.height = 16
$Label2.location = New-Object System.Drawing.Point(16,120)
$Label2.Font = 'Microsoft Sans Serif,8'
$RadioButton1 = New-Object system.Windows.Forms.RadioButton
$RadioButton1.text = "XML"
$RadioButton1.AutoSize = $true
$RadioButton1.width = 40
$RadioButton1.height = 16
$RadioButton1.location = New-Object System.Drawing.Point(88,118)
$RadioButton1.Font = 'Microsoft Sans Serif,8'
$RadioButton2 = New-Object system.Windows.Forms.RadioButton
$RadioButton2.text = "CSV"
$RadioButton2.Checked = $true
$RadioButton2.AutoSize = $true
$RadioButton2.width = 40
$RadioButton2.height = 16
$RadioButton2.location = New-Object System.Drawing.Point(148,118)
$RadioButton2.Font = 'Microsoft Sans Serif,8'
$Form.controls.AddRange(@($ListBox1,$TextBox1,$Button1,$Button2,$Label1,$TextBox2,$Label2,$RadioButton1,$RadioButton2))
#region gui events {
$Button1.Add_Click({
if ($TextBox1.Text -ne "") {
if ($ListBox1.SelectedItem -ne $null) {
Clear-Host
Set-Regex ($ListBox1.SelectedItem)
}
}
})
$Button2.Add_Click({
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Description = "Select folder containing XML"
$objForm.SelectedPath = [System.Environment+SpecialFolder]'MyComputer'
$objForm.ShowNewFolderButton = $false
$result = $objForm.ShowDialog()
if ($result -eq "OK") {
$TextBox1.Text = $objForm.SelectedPath
} else {
$TextBox1.Text = ""
}
})
#endregion events }
#endregion GUI }
#Write your logic code here
Function Set-Regex {
param (
[string]$selItem
)
switch ($selItem) {
"Classification" { $regex = '(____________________________)(.+?)(___)' }
"ProductType" { $regex = '(_____________________)(.+?)(___)' }
"ProductID" { $regex = '(___________________)(.+?)(___)' }
}
Mulch-Files $regex
}
Function Mulch-Files {
param (
[string]$pattern
)
$files = Get-ChildItem -Path ($TextBox1.Text + "\*.xml")
for ($h = 0; $h -lt $files.Count; $h++) {
$TextBox2.Text = $files[$h].Name
$products = (Get-Content $files[$h] -Raw) -_____ '(____)^.*?(____________________________)'
for ($i = 1; $i -lt $products.Count; $i += 2) {
$products[$i] -_____ '(_________)(.+?)(___)'
$prod = $Matches[0]
$temp = $products[$i] -_____ $pattern
for ($j = 0; $j -lt $temp.Count; $j++) {
if ($RadioButton2.Checked) {
$outFile = $Matches[0] + ".csv"
$outText = ($prod + (((($products[$i] -replace '(<[^>]+>|\s)', ',' ) -replace '`r', '') -replace '`n', '') -replace '(,)(,)+', '$1').TrimEnd(','))
} else {
$outFile = $Matches[0] + ".xml"
$outText = $products[$i]
}
Out-File -FilePath $outFile -InputObject $outText -Append
}
}
}
$TextBox2.Text = "Finished"
}
[void]$Form.ShowDialog()