Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $global:G_path = "G:\";
- $global:G_xml = "config.xml";
- function Select-Folder($message='Select a folder', $path = 0)
- {
- $object = New-Object -comObject Shell.Application
- $folder = $object.BrowseForFolder(0, $message, 0, $path)
- if ($folder -ne $null)
- {
- button "Enter Folders" "Series Name" "Target Folder" $folder.self.Path;
- }
- }
- function button ($title, $Name, $Loction, $TF)
- {
- ###################Load Assembly for creating form & button######
- [void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms")
- [void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic")
- #####Define the form size & placement
- $form = New-Object "System.Windows.Forms.Form";
- $form.Width = 500;
- $form.Height = 150;
- $form.Text = $title;
- $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
- ##############Define text label1
- $textLabel1 = New-Object "System.Windows.Forms.Label";
- $textLabel1.Left = 25;
- $textLabel1.Top = 15;
- $textLabel1.Text = $Name;
- ##############Define text label2
- $textLabel2 = New-Object "System.Windows.Forms.Label";
- $textLabel2.Left = 25;
- $textLabel2.Top = 50;
- $textLabel2.Text = $Loction;
- ############Define text box1 for input
- $textBox1 = New-Object "System.Windows.Forms.TextBox";
- $textBox1.Left = 145;
- $textBox1.Top = 10;
- $textBox1.width = 250;
- ############Define text box2 for input
- $textBox2 = New-Object "System.Windows.Forms.TextBox";
- $textBox2.Left = 145;
- $textBox2.Top = 50;
- $textBox2.width = 250;
- #############Define default values for the input boxes
- $defaultValue = ""
- $textBox1.Text = $defaultValue;
- $textBox2.Text = $TF;
- #############define OK button
- $button = New-Object "System.Windows.Forms.Button";
- $button.Left = 190;
- $button.Top = 85;
- $button.Width = 100;
- $button.Text = "OK";
- #############define Cancel button
- $button2 = New-Object "System.Windows.Forms.Button";
- $button2.Left = 295;
- $button2.Top = 85;
- $button2.Width = 100;
- $button2.Text = "Cancel";
- ############# This is when you have to close the form after getting values
- $eventHandler = [System.EventHandler]{
- $textBox1.Text;
- $textBox2.Text;
- $form.Close();};
- $button.Add_Click($eventHandler);
- $button2.Add_Click({$form.Close()});
- #############Add controls to all the above objects defined
- $form.Controls.Add($button);
- $form.Controls.Add($button2);
- $form.Controls.Add($textLabel1);
- $form.Controls.Add($textLabel2);
- $form.Controls.Add($textBox1);
- $form.Controls.Add($textBox2);
- $ret = $form.ShowDialog();
- #################return values
- return $textBox1.Text, $textBox2.Text
- }
- function Rebuild()
- {
- $return = Select-Folder -mess 'Select some folder:' -path $G_path
- if($return -ne $null)
- {
- # Create The Document
- $XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)
- # Set The Formatting
- $xmlWriter.Formatting = "Indented"
- $xmlWriter.Indentation = "4"
- # Write the XML Decleration
- $xmlWriter.WriteStartDocument()
- # Write Root Element
- $xmlWriter.WriteStartElement("tvAutoMover")
- # Write the Document
- $xmlWriter.WriteStartElement("tv_item")
- $xmlWriter.WriteElementString("file_name",$return[0])
- $xmlWriter.WriteElementString("destination",$return[1])
- $xmlWriter.WriteEndElement
- # Write Close Tag for Root Element
- $xmlWriter.WriteEndElement
- # End the XML Document
- $xmlWriter.WriteEndDocument()
- # Finish The Document
- $xmlWriter.Finalize
- $xmlWriter.Flush
- $xmlWriter.Close()
- }
- }
- function Delete()
- {
- Remove-Item $filePath -Force
- }
- function Add_Node()
- {
- $return = Select-Folder -mess 'Select some folder:' -path $G_path
- if($return -ne $null)
- {
- $Title = $return[0]
- $Dest = $return[1]
- $Title
- $Dest
- foreach($xmlItem in $fileContent.tvAutoMover.tv_item)
- {
- $TitleExist = $xmlItem.file_name.ToLower().CompareTo("$Title".ToLower())
- $DestExist = $xmlItem.destination.ToLower().CompareTo("$Dest".ToLower())
- if($TitleExist -eq 0)
- {
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup($Title + " Already Exist!",0,"Error")
- #return $null
- Add_Node
- }
- if($DestExist -eq 0)
- {
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup($Dest + " Already Exist!",0,"Error")
- #return $null
- Add_Node
- }
- }
- [xml]$ValueList = New-Object XML
- $ValueList.Load($filePath)
- $Match = $ValueList.tvAutoMover.tv_item.file_name
- # Copy Object
- try
- {
- [Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq $Match[0]}
- $NweValue = $CopyValue.Clone()
- }
- catch
- {
- [Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq $Match}
- $NweValue = $CopyValue.Clone()
- }
- # Add Series to new Object
- $NweValue.file_name = "$Title"
- $NweValue.destination = "$Dest"
- # Add Series to XML Object
- $ValueList.tvAutoMover.AppendChild($NweValue)
- #Save to XML object to XML file
- $ValueList.Save($filePath)
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup($Title + " Was added Secssefully.",0,"Added!")
- Exit
- }
- }
- # Set the File Name
- $scriptDir = Split-Path -parent $MyInvocation.MyCommand.Definition
- $cfgName = $G_xml
- $filePath = $scriptDir + "\" + $cfgName
- # Cheacking if the file Exist
- if(Test-Path $filePath)
- {
- # Trying to access file and add new data in to it
- try
- {
- [xml]$fileContent = New-Object XML
- $fileContent.Load($filePath)
- Add_Node
- }
- # in case of an error, Delete the file and rebuild it
- catch
- {
- Delete
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup("Config file Corrupted!`nRebuilding file...",0,"Error")
- Rebuild
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup("File successfully built.",0,"Message")
- }
- }
- else
- {
- $wshell = New-Object -ComObject Wscript.Shell
- $wshell.Popup("Config file is Missing!`nRebuilding file...",0,"Error")
- Rebuild
- #Add_Node
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement