PowerShell: Multiple file transfer by extension Script

natezna

Seniorius Lurkius
1
Hi all,

I've modified this PowerShell script to have the option to choose what files formats I want to transfer:

#START OF CODE
function GenerateForm {

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

$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$listBox1 = New-Object System.Windows.Forms.ListBox
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox4 = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$b1= $false
$b2= $false
$b3= $false
$b4= $false

$handler_button1_Click=
{
$listBox1.Items.Clear();

if ($checkBox4.Checked) { $listBox1.Items.Add( "*.gif files selected." ) }
if ($checkBox3.Checked) { $listBox1.Items.Add( "*.eps files selected." ) }
if ($checkBox2.Checked) { $listBox1.Items.Add( "*.bmp files selected." ) }
if ($checkBox1.Checked) { $listBox1.Items.Add( "*.ai files selected." ) }

if ( !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked) { $listBox1.Items.Add("No file types have been selected!")}
}

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

#FORM CODE
#Title bar.
$form1.Text = "FILE TRANSFER APP"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 550 #450
$System_Drawing_Size.Height = 236
$form1.ClientSize = $System_Drawing_Size

#Form button size.
$button1.TabIndex = 4
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 85
$System_Drawing_Size.Height = 37
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

#Button plot point.
$button1.Text = "START TRANSFER"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 127 #27
$System_Drawing_Point.Y = 176 #156
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)

$form1.Controls.Add($button1)

#List box code.
$listBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 301
$System_Drawing_Size.Height = 215 #212
$listBox1.Size = $System_Drawing_Size
$listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox1.Name = "listBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 237 #137
$System_Drawing_Point.Y = 13
$listBox1.Location = $System_Drawing_Point
$listBox1.TabIndex = 3

$form1.Controls.Add($listBox1)

#CheckBox code
#CheckBox 4
$checkBox4.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104 #Check-Box size
$System_Drawing_Size.Height = 24 #Check-Box size
$checkBox4.Size = $System_Drawing_Size
$checkBox4.TabIndex = 3
$checkBox4.Text = ".gif files"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 80
$checkBox4.Location = $System_Drawing_Point
$checkBox4.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox4.Name = "checkBox4"

$form1.Controls.Add($checkBox4)

#CheckBox 3
$checkBox3.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104 #Check-Box size
$System_Drawing_Size.Height = 24 #Check-Box size
$checkBox3.Size = $System_Drawing_Size
$checkBox3.TabIndex = 2
$checkBox3.Text = ".eps files"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 60
$checkBox3.Location = $System_Drawing_Point
$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox3.Name = "checkBox3"

$form1.Controls.Add($checkBox3)

#CheckBox 2
$checkBox2.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104 #Check-Box size
$System_Drawing_Size.Height = 24 #Check-Box size
$checkBox2.Size = $System_Drawing_Size
$checkBox2.TabIndex = 1
$checkBox2.Text = ".bmp files"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 40
$checkBox2.Location = $System_Drawing_Point
$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox2.Name = "checkBox2"

$form1.Controls.Add($checkBox2)

#CheckBox 1
$checkBox1.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104 #Check-Box size
$System_Drawing_Size.Height = 24 #Check-Box size
$checkBox1.Size = $System_Drawing_Size
$checkBox1.TabIndex = 0
$checkBox1.Text = ".ai files"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 20
$checkBox1.Location = $System_Drawing_Point
$checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox1.Name = "checkBox1"

$form1.Controls.Add($checkBox1)

#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

#END OF CODE

and want to combine the script that lets me choose where I want to transfer them from & to. I am now trying to finish & combine the code below:

#START OF CODE
#From Folder
Function Get-Folder($initialDirectory="")

{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = " You have started the file tranfer application.
Select the folder where the files (including subfolders)
will be transferred FROM:"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = $initialDirectory

if($foldername.ShowDialog() -eq "OK")
{
$fromfolder += $foldername.SelectedPath
}
return $fromfolder
}

$fromfolder = Get-Folder

#From Confirmation
Write-Output $fromfolder

#Pause
# Pause

#To folder
Function Get-Folder($initialDirectory="")

{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select the folder where the files will go TO:"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = $initialDirectory

#Must select a folder.
if($foldername.ShowDialog() -eq "OK")
{
$tofolder += $foldername.SelectedPath
}
return $tofolder
}

$tofolder = Get-Folder

#From Confirmation
Write-Output $tofolder

#Transfer Files
Move-Item -Path $fromfolder -Destination $tofolder

#Pause
# Pause

#Transfer files.

#END OF CODE

I know I am missing a couple snippets of code but need some help form the community. I am trying get it all to work together. In summary, I need to select the file types I want to transfer, select from, to, and start the process.

Thank you!