【问题标题】:Synchronise 2 local Folders through a Powershell button通过 Powershell 按钮同步 2 个本地文件夹
【发布时间】:2020-01-14 03:31:06
【问题描述】:

我只想说清楚,我对 powershell 完全陌生,如果我使用了错误的术语,很抱歉。

我想制作一个 GUI,您可以在其中使用同步按钮编写源文件夹路径和目标文件夹路径。我已经尝试了来自互联网的多种解决方案,但我似乎无法使其工作。

我想要这一切都在本地,所以没有服务器或任何涉及的东西。

´´´

#Generated Form Function
function GenerateForm {



[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$progressBar1 = New-Object System.Windows.Forms.ProgressBar
$label2 = New-Object System.Windows.Forms.Label
$textBox2 = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$textBox1 = New-Object System.Windows.Forms.TextBox
$lbl_Eingabe = New-Object System.Windows.Forms.Label
$btn_ok = New-Object System.Windows.Forms.Button
$btn_cancel = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$x = $textBox1.Text
$y = $textBox2.Text
$handler_lbl_Eingabe_Click= 
{
#TODO: Place custom script here

}

$btn_ok_OnClick= 
{
Robocopy $x $y /MIR /FFT /Z /XA:H /W:5
$form1.Close()

}

$handler_button1_Click= 
{
    $form1.Close()

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 321
$System_Drawing_Size.Width = 626
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "Primal Form"

$progressBar1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 161
$System_Drawing_Point.Y = 201
$progressBar1.Location = $System_Drawing_Point
$progressBar1.Name = "progressBar1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 352
$progressBar1.Size = $System_Drawing_Size
$progressBar1.TabIndex = 7

$form1.Controls.Add($progressBar1)

$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",15.75,0,3,1)

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 191
$System_Drawing_Point.Y = 32
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 48
$System_Drawing_Size.Width = 248
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 6
$label2.Text = "Ordner Synchronisieren"

$form1.Controls.Add($label2)

$textBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 161
$System_Drawing_Point.Y = 134
$textBox2.Location = $System_Drawing_Point
$textBox2.Name = "textBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 352
$textBox2.Size = $System_Drawing_Size
$textBox2.TabIndex = 3


$form1.Controls.Add($textBox2)


$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 59
$System_Drawing_Point.Y = 134
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 3
$label1.Text = "Zielverzeichnis:"

$form1.Controls.Add($label1)

$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 161
$System_Drawing_Point.Y = 83
$textBox1.Location = $System_Drawing_Point
$textBox1.Multiline = $True
$textBox1.Name = "textBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 352
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 3

$form1.Controls.Add($textBox1)



$lbl_Eingabe.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 83
$lbl_Eingabe.Location = $System_Drawing_Point
$lbl_Eingabe.Name = "lbl_Eingabe"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 35
$System_Drawing_Size.Width = 183
$lbl_Eingabe.Size = $System_Drawing_Size
$lbl_Eingabe.TabIndex = 2
$lbl_Eingabe.Text = "Quellverzeichnis:"
$lbl_Eingabe.TextAlign = 32
$lbl_Eingabe.add_Click($handler_lbl_Eingabe_Click)

$form1.Controls.Add($lbl_Eingabe)


$btn_ok.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 402
$System_Drawing_Point.Y = 250
$btn_ok.Location = $System_Drawing_Point
$btn_ok.Name = "btn_ok"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 100
$btn_ok.Size = $System_Drawing_Size
$btn_ok.TabIndex = 1
$btn_ok.Text = "Synchronisieren"
$btn_ok.UseVisualStyleBackColor = $True
$btn_ok.add_Click($btn_ok_OnClick)

$form1.Controls.Add($btn_ok)


$btn_cancel.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 508
$System_Drawing_Point.Y = 250
$btn_cancel.Location = $System_Drawing_Point
$btn_cancel.Name = "btn_cancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 30
$System_Drawing_Size.Width = 86
$btn_cancel.Size = $System_Drawing_Size
$btn_cancel.TabIndex = 0
$btn_cancel.Text = "Abbrechen"
$btn_cancel.UseVisualStyleBackColor = $True
$btn_cancel.add_Click($handler_button1_Click)

$form1.Controls.Add($btn_cancel)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

´´´

我希望它在目标文件夹中显示与源文件夹中相同的文件,但什么也没发生,我不知道为什么。

【问题讨论】:

    标签: powershell user-interface button synchronization


    【解决方案1】:

    $x 和 $y 变量不在按钮范围内

    试试这个

    $btn_ok_OnClick= 
    {
    $x = $textBox1.Text
    $y = $textBox2.Text
    
    Robocopy $x $y /MIR /FFT /Z /XA:H /W:5
    $form1.Close()
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多