How to re-run a script ?

Hi,

I made a simple script. This script store informations in file database in (C:\sDatabase\db.txt).
db.txt contains :

first information;1
second information;30
third information;3451

$pathToDatabaseDirectory = "C:\sDatabase"           # Create the directory for the file database if it doesn't exist
$testPathToDatabaseDirectory = Test-Path $pathToDatabaseDirectory
if ($testPathToDatabaseDirectory -ne $true){ 
    Set-Location "C:\"
    New-Item -Name "sDatabase" -ItemType directory
}

$pathToDatabaseFile = "C:\sDatabase\db.txt"          # Create the file database if it doesn't exist
$testPathToDatabaseFile = Test-Path $pathToDatabaseFile
if ($testPathToDatabaseFile -ne $true){ 
    Set-Location "C:\sDatabase"
    New-Item -Name "db.txt" -ItemType File
}

$arrayOfList = @()                                 # Output informations in the list about the database
$db = Get-Content ($pathToDatabaseFile) 
if ( $db -ne $null ) {
    Foreach ( $line in $db ) {  
        $lineSplitted = $line.split(";")
        $listBox.Items.Add($lineSplitted[0])
        $arrayOfList += $line
    }
}

$deleteButton_OnClick= 
{
    if ( $listBox.Text -ne "" ) {
        if ( $listBox.SelectedIndex -ne $null ){
            $db = Get-Content ("C:\sDatabase\db.txt")
            $updateDb = @() 
            Foreach ( $line in $db ) {
                if ( $line -ne $arrayOfList[$listBox.SelectedIndex] ){
                    $updateDb += $line
                }
                $updateDb | Set-Content "C:\sDatabase\db.txt"
            }                    # Need reload the script
        }
    }
}

$refreshButton_OnClick= 
{                                                   # Output informations about the item selected
    if ( $listBox.SelectedIndex -ne $null ){
        $lineSelected = $arrayOfList[$listBox.SelectedIndex]
        $arrayOfListSplitted = $lineSelected.split(";")
        $numberBox.Text = $arrayOfListSplitted[1]
    }
}

$addButton_OnClick= 
{                                          # Input informations in database file (Problem : $arrayOfList doesn't keep new informations)
    if ( $listBox.Text -ne "" ) {
        $procced = $true
        $name = $listBox.Text
        $number = $numberBox.Text
        Foreach ( $value in $arrayOfList ) {
            $valueSplitted = $value.split(";")
            if ( $name -eq $valueSplitted[0] ) {  # Edit informations
                $db = Get-Content ("C:\sDatabase\db.txt")
                $updateDb = @() 
                $procced = $false
                Foreach ( $line in $db ) {
                   if ( $line -ne $arrayOfList[$listBox.SelectedIndex] ){
                       $updateDb += $line
                   } else {
                       $updateDb += "$name;$number"
                   }
                   $updateDb | Add-Content "C:\sDatabase\db.txt"
                }                               # Need to reload the script
            }
        }
        if ( $procced -ne $false ) {    # Add new informations      
            Set-Location "C:\sDatabase\"
            "$name;$number" | Add-Content "db.txt"

            $listBox.Items.Add($name)      # Refresh arrayOfList
            $arrayOfList += "$name;$number" # Need to reload the script
        }
    }
}

In places where I put “Need to reload the script”, I need to re-run my script because my array isn’t update :confused:

My question is… How can I re-run my script ? Close the windows and run again (without manually)

Thanks,

Have a nice day

Use one or more functions:

Get-Help about_Functions

$listbox in line 20 is undefined…
What’s this script trying to do?