Forum Discussion
Marcus535
Dec 10, 2022Copper Contributor
For loop for all files in get-childitem one file at a time
Hi all I have written a script that successfully convert and extract just a few of key/value pairs and write that to a. csv file. That part works nice and it could run for a small numbers of file...
Marcus535
Dec 10, 2022Copper Contributor
Hi all
I come that far now that I arrange files in an array and the script now makes on file for each source file however the data seems to be appended filesizes increase by as much as the filesize would be 6-10kb. I hope anyone can help me out.
$array = @()
$files = @(Get-ChildItem -Path 'C:\test\tu\*.json')
for ($ii=0;$ii -lt $files.Count ;$ii ++){
foreach ($f in $files[$ii]){
$outname = (Split-Path -$f -Leaf).split(".")[0]+".csv"
$response = Get-Content $f -Raw | ConvertFrom-Json
$entity = $response.entity
foreach ($i in $entity){
$array += New-Object psobject -Property @{'datum' = $i.trip_update.trip.start_date; 'entity_id' = $i.id; 'trip_id' = $i.trip_update.trip.trip_id; 'vehicle_id' = $i.trip_update.vehicle.id}
}}
$array |ConvertTo-Csv -NoTypeInformation |Out-File c:\test\out\$outname
}