Remove Quotes and Comma from Quoted fields

# Remove Quotes from deliminated data file, and remove comma from field value
# Parameters definition
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$InputFile,
	
   [Parameter(Mandatory=$True,Position=2)]
   [string]$OutputFile
)

$WorkFolder = ".\workingFolder"
$globalError = 0

if($InputFile -eq "" -or $OutputFile -eq ""){

    Write-Host "Usage : ";
    Write-Host "CleanQuotes <InputFile> <OutputFile>";

    Exit(0);
}

Write-Host (Get-Date -format G) " : Start conversion!";
Write-Host "Source File : $InputFile";
Write-Host "Output File : $OutputFile";

If(-not (Test-Path $WorkFolder)){
    try{
        New-Object $WorkFolder -ItemType "directory"
        }catch{
            Write-Host "Failed to create working a folder: $WorkFolder";
            Exit(-1);
        }
}

If(Test-Path -Path $InputFile){

    $reader = New-Object System.IO.StreamReader($InputFile)

}else{
    Write-Host "Source file $InputFile does not exist!";
    Exit(0);
}

$WorkFile=Join-Path $WorkFolder "CleanQuotes_$((Get-Date -Format "yyyyMMddHHmmss.fff").ToString())";

try{

    $unquotedCommaSplit= New-Object Regex ',(?=(?:[^"]|"[^"]*")*$)','Compiled'; # Use Unquoted-Comma as delimiter

    $writer = New-Object System.IO.StreamWriter("$WorkFile")
    $rowCount = 0;
    $line=$reader.ReadLine()
    while($line -ne $null){
        
        $rowCount++;
        $cleanLineStr="";
        #Split line in to fields
        $LineSections =[regex]::Split($line,$unquotedCommaSplit, 8); # 8 - Enforce using Compiled regex for best performance
            
        $cleanLineStr=$LineSections -replace '"','' -replace ',','' -join ',';  #Remove quote and comma from each field value and re-join they with comma
            
        $writer.WriteLine($cleanLineStr);

        $line=$reader.ReadLine()
    }

    $writer.close(); 
    $reader.close();

    Write-Host "Total Lines : $rowCount";

    Move-Item $WorkFile $OutputFile -Force

}catch{
    Write-Host "Something went wrong!";
	Write-Host "Exception :" $_.Exception.Message;
    $globalError=1
}

Write-Host (Get-Date -format G) " : Finish conversion!";


# Delete the working folder
try{
    Remove-Item $WorkFolder -Force -Recurse 
}catch{
    Write-Host "Warning: unable to clean up the working folder! Ignored"
}

exit $globalError

Leave a Reply