Home > OS >  How can I read semicolon separated text out from file and saved into variables
How can I read semicolon separated text out from file and saved into variables

Time:12-06

I want to get semicolon separated strings out from file while write into var1,var2.. like in a foreach loop:

foreach ($var1,$var2 in Get-Content ".\list.txt") {
# further processing $var1,$var2 }

CodePudding user response:

It doesn't appear you can assign multiple variables as the iterator. But you can take each line and simply split it to the variables inside the loop.

$tempfile = New-TemporaryFile

@'
Value1a;Value2b
Value2a;Value2b
'@ | Set-Content -Path $tempfile

foreach($line in Get-Content $tempfile){
    $var1,$var2 = $line -split ';'
    Write-Host First var: $var1 Second var $var2
}

CodePudding user response:

If you have a semicolon separated csv file, you can use

$csvContent = Import-Csv -Path "list.txt" -Delimiter ";"

Afterwards you can work with the $csvContent variable to access the contents .

  • Related