Can't find my problem :/

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Shido Julien Shido Julien 9 months, 1 week ago.

  • Author
    Posts
  • #54790
    Profile photo of Shido Julien
    Shido Julien
    Participant

    Hello,

    I create recently a script on PowerShell and I didn't find my problem.
    Someone could help me please ?

    $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 | Set-Content "C:\sDatabase\db.txt"
                    }                               # Need to reload the script
                }
            }
            if ( $procced -ne $false ) {    # Add new informations      
                Set-Location "C:\sDatabase\"
                "$name;$number">>"db.txt"
    
                $listBox.Items.Add($name)      # Refresh arrayOfList
                $arrayOfList += "$name;$number" # Need to reload the script
            }
        }
    }

    So, this is a script that store informations in file database in (C:\sDatabase\db.txt).
    db.txt contains :

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

    When i call the method "addButton_onClick". He takes the name on listBox and the number on numberBox.
    After this, he put those informations in db.txt but... It looks like

    first information;1
    second information;30
    third information;3451f o u r i n f o r m a t i o n;234

    Before, it worked, but I don't understand why there is spaces and he is not in the next line.

    Please 🙂

    Have a nice day (Sorry for my english)

  • #54845
    Profile photo of Shido Julien
    Shido Julien
    Participant

    Someone could help me please ? 🙁

  • #54884
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Hey Shido,
    I haven't figured out just why, but it had to do with using set-content and then using the output redirectors ">>" to append the data. When it's a new file the output redirectors work just fine, but once set-content is used, something happens and subsequent uses of >> to redirect the output to the file causes the spacing. What you can do is use Add-Content instead of >>.

            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
            }

    Here's the mock-up I used to reproduce

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    
    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Select a Computer"
    $objForm.Size = New-Object System.Drawing.Size(300,200) 
    $objForm.StartPosition = "CenterScreen"
    
    $objListBox = New-Object System.Windows.Forms.ListBox 
    $objListBox.Location = New-Object System.Drawing.Size(10,40) 
    $objListBox.Size = New-Object System.Drawing.Size(260,20) 
    $objListBox.Height = 80
    
    [void] $objListBox.Items.Add("first information")
    [void] $objListBox.Items.Add("second information")
    [void] $objListBox.Items.Add("third information")
    
    $objForm.Controls.Add($objListBox) 
    
    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(10,10) 
    $objTextBox.Size = New-Object System.Drawing.Size(260,20) 
    $objTextBox.Height = 80
    
    $objForm.Controls.Add($objTextBox) 
    $test=@()
    $test+=@("second information;123")
    $test+=@("second information;345")
    $test+=@("third information;435")
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$name=$objListBox.text; $number=$objTextBox.text; "$name;$number">>text.txt})
    #$OKButton.Add_Click({$name=$objListBox.text; $number=$objTextBox.text; "$name;$number" | Add-Content text.txt})
    #$OKButton.Add_Click({$name=$objListBox.text; $number=$objTextBox.text; $test+="$name;$number"; $test | Set-Content text.txt})
    $objForm.Controls.Add($OKButton)
    
    [void] $objForm.ShowDialog()
  • #54939
    Profile photo of Shido Julien
    Shido Julien
    Participant

    Thank you very much, but you know how to add the new informations into the next line in my db.txt ? 🙂

  • #54946
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I provided that in the previous post. Use Add-Content instead. It worked correctly for me.

  • #55282
    Profile photo of Shido Julien
    Shido Julien
    Participant

    Thanks ! It works 😀 Have a nice day

You must be logged in to reply to this topic.