0

POWERSHELL ISE

Hi, I'm trying to write a program that will grab all .txt files in an input path, change the encoding, and send the new .txt files to an output location.

I have it working based on single text boxes. I'm not exactly sure how to set it where it grabs all .txt files instead of one. (maybe an array?) And output with same file name in a new location.

My question is how can I write this to grab all .txt files and output them all to a new location instead of doing a single file at a time?

Here is my current code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


$form = New-Object System.Windows.Forms.Form 
$form.Text = "Text Converter"
$form.Size = New-Object System.Drawing.Size(300,300) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(55,230)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,230)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)


$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Input Path:"
$form.Controls.Add($label) 



$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 


$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,65) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Input Encoding:"
$form.Controls.Add($label) 



$textBox2 = New-Object System.Windows.Forms.TextBox 
$textBox2.Location = New-Object System.Drawing.Point(10,85) 
$textBox2.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox2) 


$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,115) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Output Path:"
$form.Controls.Add($label) 



$textBox3 = New-Object System.Windows.Forms.TextBox 
$textBox3.Location = New-Object System.Drawing.Point(10,140) 
$textBox3.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox3) 

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,170) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Output Encoding"
$form.Controls.Add($label) 

$textBox4 = New-Object System.Windows.Forms.TextBox 
$textBox4.Location = New-Object System.Drawing.Point(10,190) 
$textBox4.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox4) 




$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text 
    $x

    $x2 = $textBox2.Text 
    $x2

    $x3 = $textBox3.Text 
    $x3

    $x4 = $textBox4.Text 
    $x4
} 


 Get-Content $x -encoding $x2 | 
 Set-Content $x3 -encoding $x4
JohnnySemicolon
  • 255
  • 2
  • 8
  • Where's the code where you actually do the work of getting, converting & saving the files? IOW, what have you tried thus far? Stack Overflow is not a code-writing service. – alroc Jun 05 '16 at 12:16
  • Using WIndowsForms for this is pointless (System.Drawing.Point does not change that). First use System.IO.Directory.GetFiles to get your input file paths. Then (in a foreach) System.IO.File.ReadAllText to read a file and System.IO.File.WriteAllText to write a file with the desired encoding. Do not mess with WIndows dialog boxes, use command line arguments instead and a param section in your script. – Martin Maat Jun 05 '16 at 13:02
  • Also worth mentioning looking into [Advanced Functions](https://technet.microsoft.com/en-us/magazine/hh413265.aspx) – user4317867 Jun 05 '16 at 16:04

1 Answers1

1

If you want the user to select multiple files, you could use an OpenFileDialog control to allow the user to select multiple input files, and then add them to a ListBox rather than a TextBox:

# Use a ListBox, since we're going to keep track of multiple strings
$InputFileBox = New-Object System.Windows.Forms.ListBox
$InputFileBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$InputFileBox.Size = New-Object System.Drawing.Size -ArgumentList 265,240
$Form.Controls.Add($InputFileBox)

# Set up the "OpenFile" dialog, set the Multiselect property
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = 'D:\test\forms'
$OpenFileDialog.Multiselect = $true

$OpenFileButton = New-Object System.Windows.Forms.Button
$OpenFileButton.Location = New-Object System.Drawing.Point -ArgumentList 220,265
$OpenFileButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OpenFileButton.Text = 'Browse!'
$OpenFileButton.add_Click({
    if($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
    {
        foreach($FileName in $OpenFileDialog.FileNames)
        {
            # only add files not already in the list
            if(-not $InputFileBox.Items.Contains($FileName))
            {
                $InputFileBox.Items.Add($FileName)
            }
        }
    }
})
$Form.Controls.Add($OpenFileButton)

Select multiple files!

You can then grab the files names either directly from the $OpenFileDialog.FileNames or from the ListBox if you want to allow the user to edit the list:

$FileNames = [string[]]$InputFileBox.Items 

For the output directory, use a FolderBrowserDialog control:

# TextBox is fine here, but disable it so users can't type directly in it 
$OutputFolderTextBox = New-Object System.Windows.Forms.TextBox
$OutputFolderTextBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$OutputFolderTextBox.Size = New-Object System.Drawing.Size -ArgumentList 265,20
$OutputFolderTextBox.Enabled = $false
$OutputFolderTextBox.BackColor = [System.Drawing.Color]::White
$Form.Controls.Add($OutputFolderTextBox)

# set up FolderBrowserDialog control
$OutputFolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OutputFolderBrowserDialog.RootFolder = [System.Environment+SpecialFolder]::MyComputer

$OutputFolderBrowseButton = New-Object System.Windows.Forms.Button
$OutputFolderBrowseButton.Location = New-Object System.Drawing.Point -ArgumentList 220,40
$OutputFolderBrowseButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OutputFolderBrowseButton.Text = 'Browse!'
$OutputFolderBrowseButton.add_Click({
    if($OutputFolderBrowserDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
    {
        # grab the selected folder path
        $OutputFolderTextBox.Text = $OutputFolderBrowserDialog.SelectedPath
    }
})
$Form.Controls.Add($OutputFolderBrowseButton)

Select output folder

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206