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)