Hi,<\/p>\n
I’m brand new to Powershell and have managed (with some help online) to get a couple of scripts working for me.<\/p>\n
This is done with the path having C:\\Work$($TextBox.text)$($TextBox2.text)\\destination_folder.<\/p>\n
I cannot get the second one to “watch” a folder based on the same method IE( C:\\Work$($TextBox.text)$($TextBox2.text)\\destination_folder_Number2)<\/p>\n
What I need is the two text boxes to determine both paths and when I click the button, the first one runs as usual and it also starts the watch process at the same time.<\/p>\n
Please help, I don’t understand why it won’t work?<\/p>\n
Thanks<\/p>\n
Adam<\/p>","upvoteCount":5,"answerCount":5,"datePublished":"2019-05-30T09:08:37.000Z","author":{"@type":"Person","name":"spiceuser-126d9","url":"https://community.spiceworks.com/u/spiceuser-126d9"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi,<\/p>\n
I’m brand new to Powershell and have managed (with some help online) to get a couple of scripts working for me.<\/p>\n
This is done with the path having C:\\Work$($TextBox.text)$($TextBox2.text)\\destination_folder.<\/p>\n
I cannot get the second one to “watch” a folder based on the same method IE( C:\\Work$($TextBox.text)$($TextBox2.text)\\destination_folder_Number2)<\/p>\n
What I need is the two text boxes to determine both paths and when I click the button, the first one runs as usual and it also starts the watch process at the same time.<\/p>\n
Please help, I don’t understand why it won’t work?<\/p>\n
Thanks<\/p>\n
Adam<\/p>","upvoteCount":5,"datePublished":"2019-05-30T09:08:37.000Z","url":"https://community.spiceworks.com/t/filesystemwatcher-path-problem/714154/1","author":{"@type":"Person","name":"spiceuser-126d9","url":"https://community.spiceworks.com/u/spiceuser-126d9"}},{"@type":"Answer","text":"
What’s your complete script. Post it pls. As a beginner you should skip the gui. Gui made by hand are a pita<\/p>","upvoteCount":2,"datePublished":"2019-05-30T09:23:04.000Z","url":"https://community.spiceworks.com/t/filesystemwatcher-path-problem/714154/2","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"
Hi Edwin, Thanks for responding,<\/p>\n
(The reason for the GUI is because I need lots of different people to be able to put in the file names and run, people who know nothing about the process, code etc They will have never heard of Powershell or CMD…)<\/p>\n
First script is the “run.bat” one here:<\/strong><\/p>\n The second one is the file watcher here:<\/strong><\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\nfunction startBAT() {\n $STARTButton.Enabled = $false #disable START button, so u won't accidentally run it again while executing\n Set-Location \"O:\\Work\\$($TextBox.text)\\$($TextBox2.text)\\Production\\3D_Files\\scenes\"\n Start-Process \":\\Work\\$($TextBox.text)\\$($TextBox2.text)\\Production\\3D_Files\\scenes\\Max_Scene_Open_TXT_Swap_Render.bat\"\n $STARTButton.Enabled = $true #enable START button after done\n $form.Close() #close GUI immediately after execution, delete this line if you dont want to\n}\n\nAdd-Type -AssemblyName System.Windows.Forms\n[System.Windows.Forms.Application]::EnableVisualStyles()\n\n$Form = New-Object system.Windows.Forms.Form\n$Form.ClientSize = '500,330'\n$Form.text = \"PIA Roll-Out\"\n$Form.TopMost = $false\n$Form.StartPosition = 'CenterScreen'\n$Form.FormBorderStyle = 'Fixed3D'\n$Form.MaximizeBox = $false\n\n$Label = New-Object system.Windows.Forms.Label\n$Label.text = \"Project No:\"\n$Label.AutoSize = $true\n$Label.location = New-Object System.Drawing.Point(50,30)\n$Label.size = New-Object System.Drawing.Size(50,50)\n$Label.Font = 'Microsoft Sans Serif,10'\n\n$TextBox = New-Object system.Windows.Forms.TextBox\n$TextBox.multiline = $false\n$TextBox.location = New-Object System.Drawing.Point(50,50)\n$TextBox.size = New-Object System.Drawing.Size(400,50)\n$TextBox.Font = 'Microsoft Sans Serif,10'\n\n$Label2 = New-Object system.Windows.Forms.Label\n$Label2.text = \"Job No:\"\n$Label2.AutoSize = $true\n$Label2.location = New-Object System.Drawing.Point(50,90)\n$Label2.size = New-Object System.Drawing.Size(50,80)\n$Label2.Font = 'Microsoft Sans Serif,10'\n\n$TextBox2 = New-Object system.Windows.Forms.TextBox\n$TextBox2.multiline = $false\n$TextBox2.location = New-Object System.Drawing.Point(50,110)\n$TextBox2.size = New-Object System.Drawing.Size(400,50)\n$TextBox2.Font = 'Microsoft Sans Serif,10'\n\n$Label3 = New-Object system.Windows.Forms.Label\n$Label3.text = \"GTIN:\"\n$Label3.AutoSize = $true\n$Label3.location = New-Object System.Drawing.Point(50,150)\n$Label3.size = New-Object System.Drawing.Size(50,80)\n$Label3.Font = 'Microsoft Sans Serif,10'\n\n$TextBox3 = New-Object system.Windows.Forms.TextBox\n$TextBox3.multiline = $false\n$TextBox3.location = New-Object System.Drawing.Point(50,170)\n$TextBox3.size = New-Object System.Drawing.Size(400,50)\n$TextBox3.Font = 'Microsoft Sans Serif,10'\n\n$STARTButton = New-Object System.Windows.Forms.Button\n$STARTButton.Location = New-Object System.Drawing.Point(200,230)\n$STARTButton.Size = New-Object System.Drawing.Size(100,50)\n$STARTButton.Text = 'START'\n$STARTButton.Add_Click({startBAT}) \n\n$Form.controls.AddRange(@($Label, $TextBox,$Label2, $TextBox2,$Label3, $TextBox3, $STARTButton))\n\n$Form.ShowDialog()\n<\/code><\/pre>\n
$folder = 'O:\\Work\\Folder1\\Folder2\\Production\\3D_Files\\export' \n$filter = '*.*' \n \n$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}\n\nRegister-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {\n$name = $Event.SourceEventArgs.Name\n$changeType = $Event.SourceEventArgs.ChangeType\n$timeStamp = $Event.TimeGenerated\nWrite-Host \"The file '$name' was $changeType at $timeStamp\" -fore green\nOut-File -FilePath c:\\temp\\log\\Filelog.txt -Append -InputObject \"The file '$name' was $changeType at $timeStamp\"\nSet-Location \"O:\\EU_CGI_Resources\\IMS\\Automation_Testing\\Scripting\\MaxScript\\Texture_change_1\\P12345\\54321\\Production\\3D_Files\\export\"\nStart-Process \"O:\\EU_CGI_Resources\\IMS\\Automation_Testing\\Scripting\\MaxScript\\Texture_change_1\\P12345\\54321\\Production\\3D_Files\\export\\PS_Run.bat\"\n}\n\nRegister-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {\n$name = $Event.SourceEventArgs.Name\n$changeType = $Event.SourceEventArgs.ChangeType\n$timeStamp = $Event.TimeGenerated\nWrite-Host \"The file '$name' was $changeType at $timeStamp\" -fore red\nOut-File -FilePath c:\\temp\\log\\Filelog.txt -Append -InputObject \"The file '$name' was $changeType at $timeStamp\"}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-05-30T09:58:01.000Z","url":"https://community.spiceworks.com/t/filesystemwatcher-path-problem/714154/3","author":{"@type":"Person","name":"spiceuser-126d9","url":"https://community.spiceworks.com/u/spiceuser-126d9"}},{"@type":"Answer","text":"