Repository navigation
Expand file tree
/
Copy pathFileMove.ps1
More file actions
112 lines (97 loc) · 3.64 KB
/
Copy pathFileMove.ps1
File metadata and controls
112 lines (97 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
param(
[Parameter(Mandatory=$false)]
[string]$FilePath
)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Check if file was provided
if (-not $FilePath -or $FilePath.Trim() -eq "") {
[System.Windows.Forms.MessageBox]::Show("No files selected for moving.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
exit
}
# Convert single path to array for compatibility
$FilePaths = @($FilePath)
# Function to perform move
function Invoke-FileOperation {
param(
[string[]]$Paths,
[string]$Destination
)
try {
foreach ($path in $Paths) {
$item = Get-Item $path
$destPath = Join-Path $Destination $item.Name
if ($item.PSIsContainer) {
Move-Item -Path $path -Destination $destPath -Force
} else {
Move-Item -Path $path -Destination $destPath -Force
}
}
Write-Host "Move completed successfully!"
} catch {
Write-Host "Error during Move: $($_.Exception.Message)"
}
}
# Create form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Move Files"
$form.Size = New-Object System.Drawing.Size(500, 400)
$form.StartPosition = "CenterScreen"
# Selected files label
$labelFiles = New-Object System.Windows.Forms.Label
$labelFiles.Text = "Selected Files:"
$labelFiles.Location = New-Object System.Drawing.Point(10, 10)
$labelFiles.Size = New-Object System.Drawing.Size(100, 20)
$form.Controls.Add($labelFiles)
# Listbox for files
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10, 30)
$listBox.Size = New-Object System.Drawing.Size(460, 150)
foreach ($path in $FilePaths) {
$listBox.Items.Add($path)
}
$form.Controls.Add($listBox)
# Destination label
$labelDest = New-Object System.Windows.Forms.Label
$labelDest.Text = "Destination Folder:"
$labelDest.Location = New-Object System.Drawing.Point(10, 190)
$labelDest.Size = New-Object System.Drawing.Size(120, 20)
$form.Controls.Add($labelDest)
# Destination textbox
$textBoxDest = New-Object System.Windows.Forms.TextBox
$textBoxDest.Location = New-Object System.Drawing.Point(10, 210)
$textBoxDest.Size = New-Object System.Drawing.Size(350, 20)
$form.Controls.Add($textBoxDest)
# Browse button
$buttonBrowse = New-Object System.Windows.Forms.Button
$buttonBrowse.Text = "Browse..."
$buttonBrowse.Location = New-Object System.Drawing.Point(370, 208)
$buttonBrowse.Size = New-Object System.Drawing.Size(100, 23)
$buttonBrowse.Add_Click({
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
if ($folderBrowser.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$textBoxDest.Text = $folderBrowser.SelectedPath
}
})
$form.Controls.Add($buttonBrowse)
# Move button
$buttonMove = New-Object System.Windows.Forms.Button
$buttonMove.Text = "Move"
$buttonMove.Location = New-Object System.Drawing.Point(200, 250)
$buttonMove.Size = New-Object System.Drawing.Size(100, 30)
$buttonMove.Enabled = $false
$buttonMove.Add_Click({
if ([string]::IsNullOrWhiteSpace($textBoxDest.Text)) {
[System.Windows.Forms.MessageBox]::Show("Please select a destination folder.", "Warning", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
return
}
Invoke-FileOperation -Paths $FilePaths -Destination $textBoxDest.Text
$form.Close()
})
$form.Controls.Add($buttonMove)
# Enable button when destination is selected
$textBoxDest.Add_TextChanged({
$buttonMove.Enabled = -not [string]::IsNullOrWhiteSpace($textBoxDest.Text)
})
# Show form
$form.ShowDialog()