Skip to content

Instantly share code, notes, and snippets.

@magicwenli
Last active April 3, 2022 11:15
Show Gist options
  • Save magicwenli/b35ff5dc3cfcbb56fcbf2939d29e4e19 to your computer and use it in GitHub Desktop.
Save magicwenli/b35ff5dc3cfcbb56fcbf2939d29e4e19 to your computer and use it in GitHub Desktop.
Copy files recursively, with GUI and filters!
<#
.NAME
MiaoCopy
.DESCRIPTION
Copy files recursively, with GUI and filters!
.LINK
github.com/magicwenli
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400, 357)
$Form.text = "Copy-Item"
$Form.TopMost = $false
$ListBox1 = New-Object system.Windows.Forms.ListBox
$ListBox1.text = "listBox"
$ListBox1.width = 317
$ListBox1.height = 233
$ListBox1.location = New-Object System.Drawing.Point(8, 11)
$ListBox1.HorizontalScrollbar = $true
$WinForm1 = New-Object system.Windows.Forms.Form
$WinForm1.ClientSize = New-Object System.Drawing.Point(400, 400)
$WinForm1.text = "Form"
$WinForm1.TopMost = $false
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Delete"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(333, 12)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Clear"
$Button2.width = 60
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(333, 55)
$Button2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button3 = New-Object system.Windows.Forms.Button
$Button3.text = "Copy"
$Button3.width = 60
$Button3.height = 30
$Button3.location = New-Object System.Drawing.Point(333, 214)
$Button3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Filter:"
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 20
$Label2.location = New-Object System.Drawing.Point(9, 250)
$Label2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Destination:"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 20
$Label1.location = New-Object System.Drawing.Point(9, 295)
$Label1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = ""
$Label3.AutoSize = $true
$Label3.width = 25
$Label3.height = 10
$Label3.location = New-Object System.Drawing.Point(110, 250)
$Label3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.text = Get-Location
$TextBox1.width = 377
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(8, 315)
$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.text = "*.mp4"
$TextBox2.width = 377
$TextBox2.height = 20
$TextBox2.location = New-Object System.Drawing.Point(9, 270)
$TextBox2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Form.controls.AddRange(@($ListBox1, $Button1, $Button2, $Button3, $TextBox2, $Label2, $Label1, $Label3, $TextBox1))
$ListBox1.AllowDrop = $true
$ListBox1.Add_DragEnter({ filesEnter })
$Button1.Add_Click({ deleteSelected })
$Button2.Add_Click({ clearList })
$Button3.Add_Click({ copyItems })
# $Button3.Add_Click({ moveItems })
$TextBox1.Add_Click({ selectFolder })
#region Logic
function deleteSelected {
while ($ListBox1.SelectedItems) {
$ListBox1.Items.Remove($ListBox1.SelectedItems[0])
}
$ListBox1.SetSelected(0, $true)
}
function clearList {
$ListBox1.Items.Clear()
}
function copyItems {
$doneNum = 0
$allNum = $ListBox1.Items.Count
$Label3.Text = "$doneNum/$allNum"
foreach ($item in $ListBox1.Items) {
$Label3.Text = "Copying $item. $doneNum/$allNum"
Copy-Item $item.FullName -Destination $TextBox1.Text -Verbose
$doneNum = $doneNum + 1
}
$Label3.Text = "$doneNum/$allNum copied!"
}
function moveItems {
$doneNum = 0
$allNum = $ListBox1.Items.Count
$Label3.Text = "$doneNum/$allNum"
foreach ($item in $ListBox1.Items) {
$Label3.Text = "Moving $item. $doneNum/$allNum"
Move-Item $item.FullName -Destination $TextBox1.Text -Verbose
$doneNum = $doneNum + 1
}
$Label3.Text = "$doneNum/$allNum moved!"
}
function selectFolder {
$TextBox1.Text = Get-Folder
}
function filesEnter () {
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) {
$childFiles = Get-ChildItem -Filter $TextBox2.Text -Path $filename -Recurse
foreach ($item in $childFiles) {
Write-Host $item.FullName
$this.Items.Add($item)
}
}
}
}
function Get-Folder() {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = Get-Location
if ($foldername.ShowDialog() -eq "OK") {
$folder += $foldername.SelectedPath
}
else {
$folder = Get-Location
}
return $folder
}
#endregion
[void]$Form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment