Author Posts

April 12, 2016 at 3:09 am

Sorry for this newbie question.I'm trying to create a text file from a csv source.
For each line in the csv i would like to generate an unique id number.

The output should look like:
phonebook
I. Donker
06-00000000
16
H. de Man
06-00000001
17

The output i'm getting:
phonebook
I. Donker
06-00000000
15
H. de Man
06-00000001
15

$File ="M:\test.txt"

$Kopregel1 = 'phonebook'

# Header aanmaken
$Kopregel1 | Out-File $File

$Users = Import-Csv -Delimiter ";" -Path "M:\csv.csv"
foreach($User in $Users) {
	$Mobielnummer = $User.'Telefoon'
	$Naam = $User.'Roepnaam' + ' ' + $User.'Tussenv' + ' ' + $User.'Achternaam'
	$Number = 15

	$Regel01 = $Naam
	$Regel02 = $Mobielnummer
	$Regel03 = $Number++
	
	$Regel01 | Out-File $File -Append
	$Regel02 | Out-File $File -Append
	$Regel03 | Out-File $File -Append
}

Invoke-Item $File

Thanks in advance

April 12, 2016 at 3:32 am

The reason why is you are setting $Number back to 15 every time you loop. You should see $Number to it's starting value outside of the loop, and then increment inside of the loop.

$File ="M:\test.txt"

$Kopregel1 = 'phonebook'

# Header aanmaken
$Kopregel1 | Out-File $File

$Users = Import-Csv -Delimiter ";" -Path "M:\csv.csv"

$Number = 15

foreach($User in $Users) {
	$Mobielnummer = $User.'Telefoon'
	$Naam = $User.'Roepnaam' + ' ' + $User.'Tussenv' + ' ' + $User.'Achternaam'

	$Regel01 = $Naam
	$Regel02 = $Mobielnummer
	$Regel03 = $Number
	
	$Regel01 | Out-File $File -Append
	$Regel02 | Out-File $File -Append
	$Regel03 | Out-File $File -Append

	$Number = $Number + 1
}

Invoke-Item $File

April 12, 2016 at 3:40 am

Thanks Curtis,

That's exactly what i was looking for.