Advertisement
obeh

Update_xml_GUI

Sep 15th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $global:G_path = "G:\";
  2. $global:G_xml = "config.xml";
  3. function Select-Folder($message='Select a folder', $path = 0)
  4. {
  5.     $object = New-Object -comObject Shell.Application  
  6.     $folder = $object.BrowseForFolder(0, $message, 0, $path)
  7.     if ($folder -ne $null)
  8.     {
  9.         button "Enter Folders" "Series Name" "Target Folder" $folder.self.Path;
  10.     }
  11. }  
  12.      
  13. function button ($title, $Name, $Loction, $TF)
  14. {
  15. ###################Load Assembly for creating form & button######
  16.      
  17. [void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms")
  18. [void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic")
  19.      
  20. #####Define the form size & placement
  21.      
  22. $form = New-Object "System.Windows.Forms.Form";
  23. $form.Width = 500;
  24. $form.Height = 150;
  25. $form.Text = $title;
  26. $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
  27.      
  28. ##############Define text label1
  29. $textLabel1 = New-Object "System.Windows.Forms.Label";
  30. $textLabel1.Left = 25;
  31. $textLabel1.Top = 15;
  32. $textLabel1.Text = $Name;
  33.      
  34. ##############Define text label2
  35. $textLabel2 = New-Object "System.Windows.Forms.Label";
  36. $textLabel2.Left = 25;
  37. $textLabel2.Top = 50;
  38. $textLabel2.Text = $Loction;
  39.      
  40.      
  41. ############Define text box1 for input
  42. $textBox1 = New-Object "System.Windows.Forms.TextBox";
  43. $textBox1.Left = 145;
  44. $textBox1.Top = 10;
  45. $textBox1.width = 250;
  46.      
  47. ############Define text box2 for input
  48. $textBox2 = New-Object "System.Windows.Forms.TextBox";
  49. $textBox2.Left = 145;
  50. $textBox2.Top = 50;
  51. $textBox2.width = 250;
  52.      
  53.      
  54. #############Define default values for the input boxes
  55. $defaultValue = ""
  56. $textBox1.Text = $defaultValue;
  57. $textBox2.Text = $TF;
  58.      
  59. #############define OK button
  60. $button = New-Object "System.Windows.Forms.Button";
  61. $button.Left = 190;
  62. $button.Top = 85;
  63. $button.Width = 100;
  64. $button.Text = "OK";
  65.      
  66.      
  67. #############define Cancel button
  68. $button2 = New-Object "System.Windows.Forms.Button";
  69. $button2.Left = 295;
  70. $button2.Top = 85;
  71. $button2.Width = 100;
  72. $button2.Text = "Cancel";
  73.      
  74. ############# This is when you have to close the form after getting values
  75. $eventHandler = [System.EventHandler]{
  76. $textBox1.Text;
  77. $textBox2.Text;
  78. $form.Close();};
  79. $button.Add_Click($eventHandler);
  80. $button2.Add_Click({$form.Close()});
  81.      
  82. #############Add controls to all the above objects defined
  83. $form.Controls.Add($button);
  84. $form.Controls.Add($button2);
  85. $form.Controls.Add($textLabel1);
  86. $form.Controls.Add($textLabel2);
  87. $form.Controls.Add($textBox1);
  88. $form.Controls.Add($textBox2);
  89. $ret = $form.ShowDialog();
  90.      
  91. #################return values
  92. return $textBox1.Text, $textBox2.Text
  93. }
  94.      
  95. function Rebuild()
  96. {
  97.     $return = Select-Folder -mess 'Select some folder:' -path $G_path
  98.     if($return -ne $null)
  99.     {
  100.         # Create The Document
  101.         $XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)
  102.      
  103.         # Set The Formatting
  104.         $xmlWriter.Formatting = "Indented"
  105.         $xmlWriter.Indentation = "4"
  106.      
  107.         # Write the XML Decleration
  108.         $xmlWriter.WriteStartDocument()
  109.      
  110.         # Write Root Element
  111.         $xmlWriter.WriteStartElement("tvAutoMover")
  112.      
  113.         # Write the Document
  114.         $xmlWriter.WriteStartElement("tv_item")
  115.         $xmlWriter.WriteElementString("file_name",$return[0])
  116.         $xmlWriter.WriteElementString("destination",$return[1])
  117.         $xmlWriter.WriteEndElement
  118.      
  119.         # Write Close Tag for Root Element
  120.         $xmlWriter.WriteEndElement  
  121.         # End the XML Document
  122.         $xmlWriter.WriteEndDocument()
  123.      
  124.         # Finish The Document
  125.         $xmlWriter.Finalize
  126.         $xmlWriter.Flush
  127.         $xmlWriter.Close()
  128.         }
  129.     }
  130. function Delete()
  131. {
  132.     Remove-Item $filePath -Force
  133. }
  134.      
  135. function Add_Node()
  136. {
  137.     $return = Select-Folder -mess 'Select some folder:' -path $G_path
  138.     if($return -ne $null)
  139.     {
  140.         $Title = $return[0]
  141.         $Dest = $return[1]
  142.         $Title
  143.         $Dest
  144.         foreach($xmlItem in $fileContent.tvAutoMover.tv_item)
  145.         {
  146.             $TitleExist = $xmlItem.file_name.ToLower().CompareTo("$Title".ToLower())
  147.             $DestExist = $xmlItem.destination.ToLower().CompareTo("$Dest".ToLower())
  148.      
  149.             if($TitleExist -eq 0)
  150.             {
  151.                 $wshell = New-Object -ComObject Wscript.Shell
  152.                 $wshell.Popup($Title + " Already Exist!",0,"Error")
  153.                 #return $null
  154.                 Add_Node
  155.             }
  156.      
  157.             if($DestExist -eq 0)
  158.             {
  159.                 $wshell = New-Object -ComObject Wscript.Shell
  160.                 $wshell.Popup($Dest + " Already Exist!",0,"Error")
  161.                 #return $null
  162.                 Add_Node
  163.             }
  164.                                    
  165.         }
  166.         [xml]$ValueList = New-Object XML
  167.         $ValueList.Load($filePath)
  168.         $Match = $ValueList.tvAutoMover.tv_item.file_name
  169.         # Copy Object
  170.         try
  171.         {
  172.             [Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq $Match[0]}
  173.             $NweValue = $CopyValue.Clone()
  174.         }
  175.  
  176.         catch
  177.         {
  178.             [Object]$CopyValue = $ValueList.tvAutoMover.tv_item | Where-Object {$_.file_name -eq $Match}
  179.             $NweValue = $CopyValue.Clone()
  180.         }
  181.      
  182.         # Add Series to new Object
  183.         $NweValue.file_name = "$Title"
  184.         $NweValue.destination = "$Dest"
  185.      
  186.         # Add Series to XML Object
  187.         $ValueList.tvAutoMover.AppendChild($NweValue)
  188.      
  189.         #Save to XML object to XML file
  190.         $ValueList.Save($filePath)
  191.         $wshell = New-Object -ComObject Wscript.Shell
  192.         $wshell.Popup($Title + " Was added Secssefully.",0,"Added!")
  193.         Exit
  194.     }
  195. }
  196.      
  197. # Set the File Name
  198. $scriptDir = Split-Path -parent $MyInvocation.MyCommand.Definition
  199. $cfgName = $G_xml
  200. $filePath = $scriptDir + "\" + $cfgName
  201.      
  202. # Cheacking if the file Exist
  203. if(Test-Path $filePath)
  204. {
  205.     # Trying to access file and add new data in to it
  206.     try
  207.     {    
  208.         [xml]$fileContent = New-Object XML
  209.         $fileContent.Load($filePath)
  210.         Add_Node
  211.     }
  212.     # in case of an error, Delete the file and rebuild it
  213.     catch
  214.     {
  215.         Delete
  216.         $wshell = New-Object -ComObject Wscript.Shell
  217.         $wshell.Popup("Config file Corrupted!`nRebuilding file...",0,"Error")
  218.         Rebuild
  219.         $wshell = New-Object -ComObject Wscript.Shell
  220.         $wshell.Popup("File successfully built.",0,"Message")
  221.     }
  222.    
  223. }
  224. else
  225. {
  226.     $wshell = New-Object -ComObject Wscript.Shell
  227.     $wshell.Popup("Config file is Missing!`nRebuilding file...",0,"Error")
  228.     Rebuild
  229.     #Add_Node  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement