Rekcut23
Rekcut23

Reputation: 11

Importing and exporting from CSV

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"
    }

}

Upvotes: 1

Views: 65

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 59822

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

Upvotes: 1

Related Questions