1

Here I am trying to create a program that can read and write to a CSV file. Can anyone see where I may be going wrong? Running in PowerShell. The error at the moment is that there seems to be nothing outputting to the CSV. I am trying to be able to append the new $patient to the CSV

$run = $true

$dataBase = "$PSScriptRoot\Vet.csv"


while ($run -eq $true)
{
    Write-Output "1. Add Patient"
    $choice = Read-Host 'Enter an option (1-5)'

    if ($choice -eq '1')
    {
        $name = Read-Host 'Enter the patient''s name'
        $bd = Read-Host "Enter the patient's birthdate (00/00/0000)"
        $gender = Read-Host "Male(m)/Female(f)"

        (Import-Csv -Path $dataBase -Encoding Default | ForEach-Object{ 
            if($name -eq '') 
            {
                $patient = New-Object PSObject -property @{
                    Name = $name
                    Birthdate = $bd
                    Gender = $gender
                }
            }
        } | ConvertTo-Csv -NoTypeInformation) -replace '"' | Out-File $dataBase
        
    }
    else
    {
        Write-Output "Please enter a valid input"
    }

}
Rekcut23
  • 11
  • 3
  • 1
    This would be a lot easier if you told us what actually happens when you run your script (including any error messages you might receive) :) – Mathias R. Jessen Jun 17 '22 at 15:06
  • Adding to Mathias's comment, I'm assuming you're expected outcome is an appended line to your Csv. If that's the case, please also add this information to your question – Santiago Squarzon Jun 17 '22 at 15:08
  • I just edited the post. Hopefully that clears it up. – Rekcut23 Jun 17 '22 at 15:18
  • 1
    `$choice` is undefined and don't force the removal of quotes in a csv file, you might ruin it very quickly that way – Theo Jun 17 '22 at 15:20
  • You're missing a Read-Host for `$choice` – Santiago Squarzon Jun 17 '22 at 15:21
  • I had this in my original code, just left it out of the question by accident. That is not the error. – Rekcut23 Jun 17 '22 at 15:22
  • 1
    So, you are only creating an object when there is no name? `if($name -eq '') `and store that in variable `$patient` (overwriting it in each iteration) and do nothing with it ? What is the point in looping over the records in the csv file if all you want to do is to add a new record? You're setting variable `$run` to $true and then loop for all eternity in the while loop because you never set that to `$false` anywhere.. and so on.. – Theo Jun 17 '22 at 15:24
  • What I am trying to do is if the user chooses option one, to create a new patient and store it in the CSV. $run is set to false if the user selects option 5, this option is not included in my code. – Rekcut23 Jun 17 '22 at 16:08

1 Answers1

1

This is what I would personally do instead of reading the Csv on each loop iteration, read it once before entering the loop and then update the object in memory while in the loop, lastly after exiting the loop (once Q option is chosen) you can update the existing Csv. This will allow you to compare if the user already exists without re-reading the Csv.

List<T> allows you to .Add( ) and .Remove( ) objects, I consider it to be the best option in this particular case.

For further improvements you could create functions for Adding, Updating and Removing entries from your Csv. Using functions would reduce the logic inside your while loop and in consequence, the code would be easier to follow.

$dataBase = "$PSScriptRoot\Vet.csv"
[Collections.Generic.List[object]] $csv = Import-Csv -Path $dataBase -Encoding Default

while ($true) {
    Write-Output "1. Add Patient"
    Write-Output "2. Update Patient"
    Write-Output "3. Remove Patient"
    Write-Output "Q. Quit"
    $choice = Read-Host 'Enter an option (1-5)'

    if ($choice -eq '1') {

        $name = Read-Host 'Enter the patient''s name'
        # check if the patient already exists in the Csv
        if($name -in $csv.Name) {
            # if it does, display a warning
            Write-Warning "'$name' is already in Csv! Use 'Update'!"
            # and go to next loop iteration
            continue
        }

        $bd     = Read-Host "Enter the patient's birthdate (00/00/0000)"
        $gender = Read-Host "Male(m)/Female(f)"

        # update the list
        $csv.Add(
            [pscustomobject]@{
                Name      = $name
                Birthdate = $bd
                Gender    = $gender
            }
        )

    }
    elseif($choice -eq 2) {
        # TO DO
    }
    elseif($choice -eq 3) {
        if($selection = $csv | Out-GridView -PassThru) {
            $null = $csv.Remove($selection)
        }
    }
    elseif($choice -eq 'Q') {
        break
    }
    else {
        Write-Output "Please enter a valid input"
    }
}

# after exiting the loop (`Q` option) we can update the file
($csv | ConvertTo-Csv -NoTypeInformation) -replace '"' | Out-File $dataBase
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37