POWERSHELL ISE批量/批量选择进行编码转换(Get-Content& Set-Content)

时间:2016-06-05 09:45:20

标签: powershell powershell-v5.0

POWERSHELL ISE

嗨,我正在尝试编写一个程序,它将获取输入路径中的所有.txt文件,更改编码,并将新的.txt文件发送到输出位置。

我让它基于单个文本框工作。我不确定如何设置它抓取所有.txt文件而不是一个。 (也许是一个数组?)并在新位置输出相同的文件名。

我的问题是如何编写此文件以获取所有.txt文件并将它们全部输出到新位置而不是一次只执行一个文件?

这是我目前的代码:

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

1 个答案:

答案 0 :(得分:1)

如果您希望用户选择多个文件,您可以使用OpenFileDialog control来允许用户选择多个输入文件,然后将其添加到ListBox而不是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!

如果您想允许用户编辑列表,您可以直接从$OpenFileDialog.FileNamesListBox获取文件名称:

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

对于输出目录,请使用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