Newbie question

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Y Roefs Y Roefs 5 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37549
    Profile photo of Y Roefs
    Y Roefs
    Participant

    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

    #37551
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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
    
    #37552
    Profile photo of Y Roefs
    Y Roefs
    Participant

    Thanks Curtis,

    That's exactly what i was looking for.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.