I'm working with a script that opens up a csv, splits it into multiple smaller csvs than compresses the files so they can be easily uploaded. The script does a fine job at this, but I need a way to escape double quotes (") so they are treated as text. I've included the part of the script that does the splitting below. Thanks!
param([string]$in_file, [string]$out_folder, [int]$linesperFile = 100000, [string]$ext = 'csv', [string]$header='Y', [string]$compress='Y')
Function Split-File{
Param(
$in_file,
$out_folder,
$linesperFile,
$ext,
$header
)
$filecount = 1
$reader = $null
$header_row = $null
try{
$reader = [io.file]::OpenText($in_file)
if ($header -ne 'N') {
$header_row = $reader.ReadLine()
}
try{
$writer = [io.file]::CreateText("{0}{1}.{2}" -f ($out_folder,$filecount.ToString("000"),$ext))
$filecount++
$linecount = 0
while($reader.EndOfStream -ne $true) {
if ($header -eq 'Y') {
$writer.WriteLine($header_row)
}
while( ($linecount -lt $linesperFile) -and ($reader.EndOfStream -ne $true)){
$writer.WriteLine($reader.ReadLine());
$linecount++
}
if($reader.EndOfStream -ne $true) {
$writer.Dispose();
$writer = [io.file]::CreateText("{0}{1}.{2}" -f ($out_folder,$filecount.ToString("000"),$ext))
$filecount++
$linecount = 0
}
}
} finally {
$writer.Dispose();
}
} finally {
$reader.Dispose();
}
return $filecount
I attempted iRon's solution and it still isn't working... It reaches one of the lines with the double quotes and then just starts repeating the same line over and over.