我需要上传文件并从地址

时间:2015-12-20 09:02:20

标签: powershell-v2.0

下面的代码将打开一个初始窗口,您需要输入要上载的文件数,然后在下一个窗口中选择要上载的文件。在这里,您将看到已上传文件的完整路径,但我只需要选择的文件名,而不是显示.e.g的完整路径。 System.Windows.Forms.TextBox, Text: C:\Users\Gourav Bid\Desktop\pwd.txt。我只需要取出pwd.txt

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

###### Initial Form Design ######

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "File Upload"
$objForm.Size = New-Object System.Drawing.Size(350,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

###### Label & TextBox Design ######

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Point(40,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Enter The Number Of Artefacts File Required :"
$objForm.Controls.Add($objLabel)

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

###### Operation Buttons & Cancel Button Design ######

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,80)
$OKButton.Size = New-Object System.Drawing.Size(70,20)
$OKButton.Text = "OK"
$OKButton.Add_Click{(Button), $objForm.Close()}
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(120,80)
$CancelButton.Size = New-Object System.Drawing.Size(70,20)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

function Button {
    [int]$artefacts = $objTextBox.Text
    $d = 100 + $artefacts*30
    $OKLoc = $d-70

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.Size = New-Object System.Drawing.Size(400,$d)
    $Form1.Text = "Select Artefacts Files"
    $Form1.StartPosition = "CenterScreen"
    $Form1.AutoScroll = $True
    $Form1.KeyPreview = $True

    ############################################## Buttons ##############################################
    $OKButton1 = New-Object System.Windows.Forms.Button
    $OKButton1.Location = New-Object System.Drawing.Size(20,$OKLoc)
    $OKButton1.Size = New-Object System.Drawing.Size(75,23)
    $OKButton1.Text = "OK"
    $OKButton1.Add_Click({$x=$objTextBox.Text;$Form1.Close();{split};Write-Host "$arte1"})
    $Form1.Controls.Add($OKButton1)

    $CancelButton1 = New-Object System.Windows.Forms.Button
    $CancelButton1.Location = New-Object System.Drawing.Size(150,$OKLoc)
    $CancelButton1.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton1.Text = "Cancel"
    $CancelButton1.Add_Click({$Form1.Close()})
    $Form1.Controls.Add($CancelButton1)

    ############################################## Buttons ##############################################

    if ($artefacts -eq 1) {
        $Label = New-Object System.Windows.Forms.Label
        $Label.Location = New-Object System.Drawing.Size(15,20)
        $Label.Size = New-Object System.Drawing.Size(50,20)
        $Label.Text = "File 1"
        $Form1.Controls.Add($Label)

        $arte = New-Object System.Windows.Forms.TextBox
        $arte.Location = New-Object System.Drawing.Size(80,15)
        $arte.Size = New-Object System.Drawing.Size(280,20)
        $Form1.Controls.Add($arte)
    }
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(270,$OKLoc)
    $Button.Size = New-Object System.Drawing.Size(75,23)
    $Button.Text = "Browse"
    $Button.Add_Click({Button})
    #Write-Host $test
    $Form1.Controls.Add($Button)

    #####################################################Function Button#####################################################

    function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect) {
         Add-Type -AssemblyName System.Windows.Forms
         $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
         $openFileDialog.Title = $WindowTitle
        if (![string]::IsNullOrWhiteSpace($InitialDirectory)) {
          $openFileDialog.InitialDirectory = $InitialDirectory
        }
        $openFileDialog.Filter = $Filter

        if ($AllowMultiSelect) {
          $openFileDialog.MultiSelect = $true
        }

        $openFileDialog.ShowHelp = $true
        # Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.

        $openFileDialog.ShowDialog() > $null
        if ($AllowMultiSelect) {
            return $openFileDialog.Filenames
        } else {
            return $openFileDialog.Filename
        }
    }

    function Button {
        for ($a=0;$a -lt $artefacts;$a++) {
            $b = $a+1
            $arte[$a].Text = Read-OpenFileDialog -WindowTitle "Select File $b" -InitialDirectory "C:\" -Filter "ALL files (*.*)|*.*"
        }
        Write-Host "$arte"
        $arte1=$arte
    }

    function Split {
        $arte1.split('\')[1]#.split(',')[0]
        Write-Host "$arte1"
    }

    #####################################################Function Button

    if ($artefacts -gt 1) {
        $b = 20
        $c = 15

        for ($a=0;$a -lt $artefacts;$a++) {
            $d = $a + 1

            $Label = New-Object System.Windows.Forms.Label
            $Label.Location = New-Object System.Drawing.Size(15,$b)
            $Label.Size = New-Object System.Drawing.Size(50,20)
            $Label.Text = "File $d"
            $Form1.Controls.Add($Label)

            [Array]$arte += New-Object System.Windows.Forms.TextBox
            $arte[$a].Location = New-Object System.Drawing.Size(80,$c)
            $arte[$a].Size = New-Object System.Drawing.Size(280,20)
            $Form1.Controls.Add($arte[$a])


            $b = $b+30
            $c = $c+30
        }
    }

    $Form1.Add_Shown({$Form1.Activate()})
    [void] $Form1.ShowDialog()
}

$objForm.Topmost = $false

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

1 个答案:

答案 0 :(得分:0)

如果您只需要文件名,则可以使用Split-Path

Split-Path "C:\Users\Gourav Bid\Desktop\pwd.txt" -Leaf

输出

pwd.txt

在您的示例中,您可以替换

Write-Host "$arte"

$arte | % { Write-Host (Split-Path $_.Text -Leaf) }

它将在$arte上迭代,并从.Text属性中提取文件名。

您获得的输出是整个TextBox对象,您需要指定所需的属性,在本例中为.Text