'Process multiples CSV files separately
I am trying to process multiples CSV files, editing one by one, and export each file in other directory with a new suffix in the file name. I don´t want to merge the files.
I tried too use the Get-ChildItem to list the files and loop each file individualy.
$ParentDir = Get-Location
$ProcDir = "$ParentDir" + "\Proc"
New-Item -Name "Proc" -ItemType "directory"
$csvs = Get-ChildItem -Path $ParentDir -Filter "*.csv"
foreach($csv in $csvs){
$Name = $csvs.Name`
$NewName = $csvs.BaseName+'_DeletedLinesColumns'+'.'+'csv'
$file = Import-Csv -Path $ParentDir"$Name" -Delimiter ',' | where Ping -NotLike "`*`Header`*`" | select "Column1","Column3","Column7"
$file | Export-Csv -Path $ProcDir"$NewName" -Delimiter ',' -NoTypeInformation
}
Solution 1:[1]
I haven't tested your csv code, but this should get you on the right track
$ParentDir = $PSScriptRoot
$ProcDir = "$ParentDir\Proc"
if(!(test-path $ProcDir)){New-Item -Path $ParentDir -Name "Proc" -ItemType "directory"}
$csvs = Get-ChildItem -Path $ParentDir -Filter "*.csv"
foreach($csv in $csvs){
$NewName = $csv.BaseName+'_DeletedLinesColumns.csv'
Import-Csv -Path $csv.FullName -Delimiter ',' | ?{$_.Ping -NotLike "`*`Header`*`"} | select "Column1","Column3","Column7" | Export-Csv -Path "$ProcDir\$NewName" -Delimiter ',' -NoTypeInformation
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
