Clam
Clam

Reputation: 75

New lines appearing in text file when they are not supposed to be

I'm trying to read out a CSV file, which contains userid, given name, surname and the sex of some users. The script should read out the userID and check if a directory with this ID already exists, if not it creates one. Than it should read out, which sex the user has. With this information the scripts makes a text file which contains a proper salutation and a static body text, that is the same for men and women. This is what I have accomplished so far:

#Variables

$script:pgm="welcome"
$script:log="$PSScriptRoot\$pgm.log"        
$script:csv="$pgm.csv"
$script:dir="D:\ps\users"
$script:txt="$pgm.txt"
$script:fix="$pgm.fix"    # fix is the file with the static text
$script:mrs="Dear Mrs."
$script:mr="Dear Mr."

# Create directories & textfiles

$imp=import-csv $csv -Delimiter ";" -Encoding UTF8
$users=$imp[0..2].Userid
$sex=$imp[0..2].Sex
$sirname=$imp[0..2].SirName
$f=cat $fix

foreach($a in $users) {
    if(! (Test-Path $dir\$a)) {
        New-Item -Path $dir\$a -ItemType Directory      
    }
}
foreach($d in $sex) {
    if ($d -eq "F") {
        foreach($name in $sirname) {
            write-output $mrs $name"," | out-file $dir\$a\$txt 
        }
    }   
    Else {
        foreach($name in $sirname) {
            write-output $mr $name"," | out-file $dir\$a\$txt
        }   
    }
    write-output $f >> $dir\$a\$txt
}   

I have two struggles:

  1. The script always writes "Jones" as name in the text file and ignores the other two and also only writes 'Dear Mr.' (also to female users).
  2. The script is putting the name in a separate line. Write-Host -NoNewLine works fine for this problem, but only in the console, you can't pipe that into a file. Is there a way to do it with write-output?

The output textfile looks like this:

Dear Mr.  
Jones,
welcome to our company!

Your sysadmin

But it should look like this:

Dear Mr. Jones,
welcome to our company!

Your sysadmin

The CSV file:

Userid;GivenName;SirName;Sex 
JACKS;Tom;Jackson;M 
MILLE;Kate;Miller;F
JONES;Paul;Jones;M

Thank you for your help!

Upvotes: 0

Views: 53

Answers (2)

Loïc MICHEL
Loïc MICHEL

Reputation: 26120

no need to over complicate !

$csv=Import-Csv users.csv -Delimiter ';' 
$csv |%{                                                                                       
    if ($_.Sex -eq 'M') {$prefix = 'Mr.'}                                                     
    else{$prefix='Mrs.'}                                                                      
    echo "Dear $prefix $($_.SirName)" |out-file -filepath "c:\temp\$($_.userid)welcome.txt"   
    get-content $fix |out-file "c:\temp\$($_.userid)welcome.txt" -Append                  
 }                                                                                             

Upvotes: 1

Vesper
Vesper

Reputation: 18747

First, you should use a matched record as a source, instead of splitting data into several unlinked variables:

foreach($user in $imp) {...}

Once inside, you can call $user.sex and other CSV fields to determine the required parameters of a user. This will make the email generation code into this:

foreach ($user in $imp) {
    $title=('Mr.','Mrs.')[$user.sex -eq 'F'] # returns 'Mr.' if there's not 'F' in sex
    @"
    Dear $title $($user.surname)!
    Welcome to our company!

    Your sysadmin.
    "@ # a multilined string with placeholders for variables
} # that's all, folks

Upvotes: 1

Related Questions