- 1 year ago
process multiple CSV file and delete rows in a single column which has double semi colon characters using powershell
To process multiple CSV files in PowerShell and delete rows containing double semi-colon characters in a specific column, you can use the following script:
powershell$csvFiles = Get-ChildItem -Path "C:\Path\To\CSV\Files" -Filter "*.csv"
foreach ($csvFile in $csvFiles) {
$data = Import-Csv -Path $csvFile.FullName
$updatedData = $data | Where-Object { $_.ColumnWithDoubleSemicolon -notlike "*;;*" }
$updatedData | Export-Csv -Path $csvFile.FullName -NoTypeInformation
}
In this script:
The
Get-ChildItem
cmdlet retrieves a list of CSV files in the specified directory (C:\Path\To\CSV\Files
) using the-Filter
parameter.The
foreach
loop iterates through each CSV file.The
Import-Csv
cmdlet reads the CSV file and stores the data in the$data
variable.The
$updatedData
variable is assigned the filtered data, using theWhere-Object
cmdlet to exclude rows where theColumnWithDoubleSemicolon
column contains double semi-colon characters (*;;*
is the pattern for matching double semi-colons).Finally, the
$updatedData
is exported back to the same CSV file, overwriting the original file using theExport-Csv
cmdlet.
Make sure to replace "C:\Path\To\CSV\Files"
with the actual path to your CSV files directory. Also, adjust the column name ColumnWithDoubleSemicolon
to the name of the specific column in your CSV files that you want to check for double semi-colon characters.
Note: This script assumes that your CSV files have a header row. If your CSV files don't have a header row, you may need to add the -Header
parameter to the Import-Csv
cmdlet to specify the column names manually.
Remember to test this script on a subset of your CSV files or make backups of your files before running it on your entire dataset to avoid accidental data loss.