0

I want to decide which folder that I need to choose based on my data, then if I can't find it, it will show the GUI for waiting and do looping to check it. I try this code, I can find the folder, but when I can't find it once I want to show the GUI it returns some error.

This is how I checking the folder

function FIND {
    Write-Host "call the function that can call the GUI.ps1 script"

        $Path = "D:\Process"
        Write-Host "Starting Mapping SSID and Finding Job"
        $SSID_Unit = "111dddddfafafesa"

        Try{
            $Path_Job = (Get-Item (Get-ChildItem "$Path\*\SSID_LST" | Select-String -Pattern "$SSID_Unit").Path).Directory.FullName

            $global:Result = [PSCustomObject]@{
                Exists   = $true
                FileName = $Path_Job.FullName
                Attempts = 1
            }
            Write-Host "Job'$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
            Write-Host "Continue to Assigned Job"
            Pause
        } Catch {
            Write-Host "Waiting for the jobss"
            & D:\X\Wait_GUI.ps1  -Path $Path_Job -MaxAttempts 20
            Write-Host "Job not found after $($global:Result.Attempts) attempts." -ForegroundColor Red

        }   
    }
    FIND

This is the GUI

    Param (   
        [string]$Path = '*.*',
        [string]$MaxAttempts = 5
    ) 

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()

    # set things up for the timer
    $script:nAttempts = 0
    $timer = New-Object System.Windows.Forms.Timer
    $timer.Interval = 1000  # 1 second
    $timer.Add_Tick({
        $global:Result = $null
        $script:nAttempts++
        $Path_Job = Get-Item -Path $Path
        if ($Path_Job) {
            $global:Result = [PSCustomObject]@{
                Exists   = $true
                FileName = $Path_Job.FullName
                Attempts = $script:nAttempts
            }
            $timer.Dispose()
            $Form.Close()
        }
        elseif ($script:nAttempts -ge $MaxAttempts) {
            $global:Result = [PSCustomObject]@{
                Exists   = $false
                FileName = ''
                Attempts = $script:nAttempts
            }
            $timer.Dispose()
            $Form.Close()
        }
    })

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()

    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '617,418'
    $Form.text                       = "AutoGM"
    $Form.BackColor                  = "#8b572a"
    $Form.TopMost                    = $false
    $Form.WindowState                = 'Maximized'

    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "UNDER AUTOMATION PROCESS"
    $Label1.AutoSize                 = $true
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.Anchor                   = 'top,right,bottom,left'

    $Label1.ForeColor                = "#ffffff"
    $Label1.Anchor                   = "None"
    $Label1.TextAlign                = "MiddleCenter"

    $Label2                          = New-Object system.Windows.Forms.Label
    $Label2.text                     = "Waiting for the job..."
    $Label2.AutoSize                 = $true
    $Label2.width                    = 25
    $Label2.height                   = 10

    $Label2.ForeColor                = "#ffffff"
    $Label2.Anchor                   = "None"
    $Label2.TextAlign                = "MiddleCenter"

    $Form.controls.AddRange(@($Label1,$Label2))

    [void]$Form.Show()
    # Write-Host $Form.Height
    # Write-Host $Form.Width

    $Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
    $Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))

    $L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
    $Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
    $Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"

    $Form.controls.AddRange(@($Label1,$Label2))
    # start the timer as soon as the dialog is visible
    $Form.Add_Shown({ $timer.Start() })

    $Form.Visible = $false
    [void]$Form.ShowDialog()

    # clean up when done
    $Form.Dispose()

if not found, it return this

    Waiting for the jobss
    Job not found after 1 attempts.

and the GUI is not shown

5
  • 2
    That error being ... what? Commented Aug 14, 2019 at 9:05
  • @Job This is really the first part of my answer of your previous question, above the Update code, because then you specified new requirements. Seems in this question you're back to that first idea.. Only the form changed a little and the file paths. What is the error you are getting with this? Commented Aug 14, 2019 at 9:58
  • Hi @Theo, I updated the code reference to your previous answer. But it still return an error, could you please help me. Thanks Commented Aug 16, 2019 at 7:36
  • It is a bit unclear what exactly you are looking for.. A file that has a string "1010" somewhere in its contents, or a folder by that name or... The error message is simple enough. You are testing if something exists, and if not you show the GUI with the path parameter set to this $null valued variable $Path_Job. Inside the GUI you should perform the exact same test as you do outside of the form. Please explain a bit more what it is the function is looking for. Commented Aug 16, 2019 at 8:55
  • @Theo I updated the script to call the GUI, it does not return error, but It can not execute the GUI. Is there any wrong with the GUI script? Commented Aug 16, 2019 at 9:52

1 Answer 1

4
+50

Ok, first of all, you are using different tests for the file and/or directory in the code and in the GUI. Furthermore, you call the GUI.ps1 file with a path set to a $null value.

I would change your code to something like this:

$Path      = "D:\Process\*\SSID_LST\*"  # the path to look for files
$SSID_Unit = "111dddddfafafesa"         # the Search pattern to look for inside the files

function Test-FileWithGui {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Path,
        [Parameter(Mandatory = $true, Position = 2)]
        [string]$Pattern,
        [int]$MaxAttempts = 5
    )

    Write-Host "Starting Mapping SSID and Finding Job"

    # set up an 'empty' $global:Result object to return on failure
    $global:Result = '' | Select-Object @{Name = 'Exists'; Expression = {$false}}, FileName, Directory, @{Name = 'Attempts'; Expression = {1}}

    # test if the given path is valid. If not, exit the function
    if (!(Test-Path -Path $Path -PathType Container)) {
        Write-Warning "Path '$Path' does not exist."
        return
    }
    # try and find the first file that contains your search pattern
    $file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1

    if ($file) {
        $file = Get-Item -Path $file.Path

        $global:Result = [PSCustomObject]@{
            Exists    = $true
            FileName  = $file.FullName
            Directory = $file.DirectoryName
            Attempts  = 1
        }
    }
    else {
        & "D:\GUI.ps1" -Path $Path -Pattern $Pattern -MaxAttempts $MaxAttempts
    }
}

# call the function that can call the GUI.ps1 script
Test-FileWithGui -Path $Path -Pattern $SSID_Unit -MaxAttempts 20

# show the $global:Result object with all properties
$global:Result | Format-List

# check the Global result object
if ($global:Result.Exists) {
    Write-Host "File '$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
}
else {
    Write-Host "File not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}

Next the GUI file. As you are now searching for a file that contains some text, you need a third parameter to call this named Pattern.

Inside the GUI file we perform the exact same test as we did in the code above, using the parameter $Pattern as search string:

Param (   
    [string]$Path,
    [string]$Pattern,
    [int]$MaxAttempts = 5
) 

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000  # 1 second
$timer.Add_Tick({
    $global:Result = $null
    $script:nAttempts++

    # use the same test as you did outside of the GUI
    # try and find the first file that contains your search pattern
    $file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1

    if ($file) {
        $file = Get-Item -Path $file.Path

        $global:Result = [PSCustomObject]@{
            Exists    = $true
            FileName  = $file.FullName
            Directory = $file.DirectoryName
            Attempts  = $script:nAttempts
        }
        $timer.Dispose()
        $Form.Close()
    }
    elseif ($script:nAttempts -ge $MaxAttempts) {
        $global:Result = [PSCustomObject]@{
            Exists    = $false
            FileName  = $null
            Directory = $null
            Attempts  = $script:nAttempts
        }
        $script:nAttempts = 0
        $timer.Dispose()
        $Form.Close()
    }
})

$Form             = New-Object system.Windows.Forms.Form
$Form.ClientSize  = '617,418'
$Form.Text        = "AutoGM"
$Form.BackColor   = "#8b572a"
$Form.TopMost     = $true
$Form.WindowState = 'Maximized'

# I have removed $Label2 because it is easier to use 
# just one label here and Dock it to Fill.
$Label1           = New-Object system.Windows.Forms.Label
$Label1.Text      = "UNDER AUTOMATION PROCESS`r`n`r`nWaiting for the job..."
$Label1.AutoSize  = $false
$Label1.Dock      = 'Fill'
$Label1.TextAlign = "MiddleCenter"
$Label1.ForeColor = "#ffffff"

$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 10
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"

$Form.controls.Add($Label1)

# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })

[void]$Form.ShowDialog()

# clean up when done
$Form.Dispose()

The results during testing came out like below

If the file was found within the set MaxAttempts tries:

Starting Mapping SSID and Finding Job


Exists    : True
FileName  : D:\Process\test\SSID_LST\blah.txt
Directory : D:\Process\test\SSID_LST
Attempts  : 7



File 'D:\Process\test\SSID_LST\blah.txt' Exists. Found after 7 attempts.

When the file was NOT found:

Starting Mapping SSID and Finding Job


Exists    : False
FileName  : 
Directory : 
Attempts  : 20



File not found after 20 attempts.

If even the folder $Path was not found, the output is

Starting Mapping SSID and Finding Job
WARNING: Path 'D:\Process\*\SSID_LST\*' does not exist.


Exists    : False
FileName  : 
Directory : 
Attempts  : 1



File not found after 1 attempts.

Hope that helps

Sign up to request clarification or add additional context in comments.

11 Comments

Hi @Theo.. I tried this, but it always return this Starting Mapping SSID and Finding Job WARNING: Path 'D:\Process\*\SSID_LST\*' does not exist. Exists : False FileName : Directory : Attempts : 1 File not found after 1 attempts. Even the string $SSID_Unit exist in the SSID_LST file, it always return not found And the GUI is Not shown if it is not found
Hi @Theo, I updated the code this part $Path = Get-ChildItem "D:\Process\*\SSID_LST\" # the path to look for files $SSID_Unit = "1309" # the Search pattern to look for inside the files function Test-FileWithGui { [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0)] $Path, I add Get-ChildItem and I remove the [string] in the $Path In the GUI I also remove [string] in the $Path I use this Param ( $Path, [string]$Pattern, [int]$MaxAttempts = 5 ) and it works.
@Job Good to hear you've got it working. For me, without having your original files, it was hard to test it all out.
Hi @Theo I found the error, cannot bind argument to parameter 'pattern' because its an empty string
@Job. That error is NOT in my code. You really need to start learning something about Function parameters and how to use them. The $Path parameter is designed to be a [string]. If you send it the result of Get-ChildItem "D:\Process\*\SSID_LST\", you're not sending a string, but an array of FileInfo and DirectoryInfo objects.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.