2

my CSV contains the following data:

enter image description here

CSV in plain text:

System Speed Dialling Number,"Name (20 characters)","CO Line Access Number + Telephone Number (32 digits)","CLI Destination" 
0,"USER1","Dial=00123456300","Dial=" 
1,"USER2","Dial=00123456400","Dial=" 
2,"","","" 
3,"","","" 
4,"","","" 
5,"","","" 
6,"","","" 
7,"","","" 
8,"","","" 
9,"","","" 
10,"","",""

Up to 1000 rows.

I tried using the following script to replace the empty values ​​in the "CO Line Access Number + Telephone Number (32 digits)" and "CLI Destination" columns:

$dial1 = import-csv C:\TEMP\dial.csv
$dial1 | foreach-object {
$_.'CO Line Access Number + Telephone Number (32 digits)' = $_.'CO Line Access Number + Telephone Number (32 digits)'.replace("","Dial=") 
}
$dial1 | export-csv C:\TEMP\dial.csv –NoTypeInformation

Error: The string cannot be zero-length

Question: Is there a way to fill the columns mentioned using a script and the value "Dial="

Sorry I have no experience with this...


Alternatively, I thought of a script that writes only the lines with users from one CSV to another CSV (ex. where "Name (20 Characters)" isn´t empty). Unfortunately I couldn't find anything about this.

4
  • 1
    Can you update your question including the CSV as plain text instead of a screenshot? Commented Sep 22, 2022 at 15:02
  • I just updated my question Commented Sep 22, 2022 at 15:12
  • 1
    If I understand correctly, you just want to insert Dial= in the CO Line Access Number + Telephone Number (32 digits) if the value is an empty / null string. Is this correct ? Commented Sep 22, 2022 at 15:17
  • yes, that's correct. Commented Sep 22, 2022 at 15:18

1 Answer 1

2

You can follow this logic to accomplish it, check if the value on the column CO Line Access Number + Telephone Number (32 digits) is a null or whitespace string using String.IsNullOrWhiteSpace, if the condition evaluates to $true, re-assign the desired value (Dial=) to that property.

Import-Csv path\to\csv.csv | ForEach-Object {
    if([string]::IsNullOrWhiteSpace($_.'CO Line Access Number + Telephone Number (32 digits)')) {
        $_.'CO Line Access Number + Telephone Number (32 digits)' = 'Dial='
    }
    $_
} | Export-Csv newfixedcsv.csv -NoTypeInformation

To briefly explain the error, .Replace($oldValue, $newValue) complains because the argument for $oldValue cannot be an empty string ('' or [string]::Empty or [NullString]::Value).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.