Reputation: 31
Being new to Powershell I managed to get together a script. This already prepares a csv where I know, a preview of the file below:
"Documentnummer","Versie","Status","Geocode","IDS datum","Documentgroep (Omsch)","Documenttype (Wrd)","Tekeningnummer"
"550165","C","AB","008","19.07.2013","Themalagen OI","KS","008KS10"
"550164","I","AB","008","26.03.2016","Themalagen OI","KS","008KS09"
"550163","D","AB","008","19.07.2013","Themalagen OI","KS","008KS08"
This goes on for about 2000 lines. Now, I use this to download files, and when downloaded I create the file name using the same csv. Here is the question, I want to create and extra column, header e.g. 'Nummer' that contains an increasing number for each row;
Nummer 0001, 0002, 0003, ...., 0050, ...., 0105, ...., 2547
I always want four digits so it looks like this:
"Nummer","Documentnummer","Versie","Status","Geocode","IDS datum","Documentgroep (Omsch)","Documenttype (Wrd)","Tekeningnummer"
"0001","550165","C","AB","008","19.07.2013","Themalagen OI","KS","008KS10"
"0002","550164","I","AB","008","26.03.2016","Themalagen OI","KS","008KS09"
...
"0108","550163","D","AB","008","19.07.2013","Themalagen OI","KS","008KS08"
I tried it two ways and both failed, creating an incremental number an adding it to the csv and creating a csv with the right numbers, and merging both csv's.
Help is appreciated! (And sorry for the dutch header names)
Using Powershell 3.0 on Windows 7 x64
Update, the code I used to merge the two files:
$drawinfo = Import-Csv $downloads\${drawtype}_DIR_AUTO_FILTER.csv | ConvertTo-Csv -NoTypeInformation
$numberinfo = Import-Csv "$BBKSDIR\Related files\Incremental_number.csv" | ConvertTo-Csv -NoTypeInformation
The first one contains the bulk data, the second one only holds the numbers.
[array]$allinfo = "{0},{1}" -f $numberinfo[0],$drawinfo[0]
[array]$allinfo += "`r`n{0},{1}" -f $numberinfo[1],$drawinfo[1]
$allinfo
Here I first get the header row of both files, and in the second row I want to get all the other rows, but I'm only getting the 2nd row (1st data row).
Executing $allinfo now gets me:
"Nummer","Documentnummer","Versie","Status","Geocode","IDS datum","Documentgroep (Omsch)","Documenttype (Wrd)","Tekeningnummer" "0001","550165","C","AB","008","19.07.2013","Themalagen OI","KS","008KS10"
Upvotes: 3
Views: 2441
Reputation: 17472
try this
$script:cpt=0
import-csv "C:\temp10\report.csv" |
select @{N="Nummer";E={$script:cpt++;"{0:d4}" -f $script:cpt}}, * |
export-csv "C:\temp10\report2.csv" -NoTypeInformation
Upvotes: 1
Reputation: 43499
Something like this should give you a hint about how to do it:
$csv = Import-Csv "D:\temp\test.csv"
$global:i = 0; $csv | Select-Object @{ Name = 'Nummer'; Expression = { $global:i.ToString("0000"); $global:i += 1 } }, * | Export-Csv "D:\temp\test2.csv"
This is based on calculated properties.
Upvotes: 2