0

Here I have to split a large csv into multiple small csv

My csv looks like that

COL1;COL2;COL3
1;1;1
2;2;2
3;3;3

And The code for splitting

    # Variable de stockage du csv
    $sourceCSV = Import-CSV C:\Temp\RéductionCSV\CSV_Extraction_GEA_Reduce_1.csv -Delimiter ';'
    
    # Ligne de départ (Header)
    $startrow = 0 ;
    
    # Compteur
    $counter = 1 ;
    $delimiter = ';'
    
    # Set la boucle pour continuer tant que la valeur est plus petite que le nombre de ligne total
    while ($startrow -lt 3)
    {
    
    # Génération du fichier en fonction du nombre de lignes voulues
   $sourceCSV | select-object -skip $startrow -first 2 | Export-CSV "c:\temp\SplitCSV_$(get-date -f dd-MM-yyyy)_$($counter).csv" -NoClobber -NoTypeInformation -Delimiter ';'  | % {$_ -replace '"',''}
    
    # Avance le nombre de ligne pour le fichier d'après
    $startrow += 2 ;
    
    # Incrémente variable $counter
    $counter++ ;
    
    }

This works great for splitting csvs but the result is bad

"COL1;COL2;COL3"
"1;1;1"
"2;2;2"

I'd like to delete this double quotes from my result

Any idea?

I've tried what is commented in the code but it doesn't work

  • Is it itentional that you do not provide the parameter `-Delimiter` for your CSV import? ;-) I'd recommend to remove the code lines you disabled/commented. – Olaf Aug 03 '20 at 09:37
  • What do you mean "it does not work"? – Olaf Aug 03 '20 at 09:40
  • I have never known `Export-CSV` to output each record doublequoted, although I expect a single field record may. _Perhaps you mean that your record looks more like this `"COL1";"COL2";"COL3"`_ Also as you don't really seem to be manipulating any of the content, simply removing the first two records, why are you using `Import-CSV` and `Export-CSV`? – Compo Aug 03 '20 at 09:43
  • If I create a sample CSV with your sample data and import it with the parameter `-Delimiter ';'` it works just as expected. Please update your question with the corrected code. And please remove all code you're not using at the moment. – Olaf Aug 03 '20 at 09:44
  • All tools able to work with valid and standard compliant CSV files should be able to handle quotes gracefully. What's the problem? – Olaf Aug 03 '20 at 09:46
  • As I mentioned in my previous comment, the content of your file is irrelevant, you're simply removing lines from a file, for that, you do not need to deal with the component parts of a CSV file, so use `Get-Content` and `Set-Content`, a stream reader, or some other general file parsing method instead. – Compo Aug 03 '20 at 09:49
  • If you insist to remove the double quotes you will have to treat the CSV files as text and remove them this way. But that could cause other problems.Search for solutions for that. There are thousands of examples for that. – Olaf Aug 03 '20 at 09:52
  • sorry I'm still beginner in scriting (and french), I don't see waht you mean with the get-content (instead of the import-csv you mean?) – Hadrien Beaujean Aug 03 '20 at 09:53
  • 1
    [https://stackoverflow.com/questions/30247002/powershell-script-to-remove-double-quotes-from-csv-unless-comma-exists-inside-do](https://stackoverflow.com/questions/30247002/powershell-script-to-remove-double-quotes-from-csv-unless-comma-exists-inside-do) – Olaf Aug 03 '20 at 09:58
  • that whas just an example datas in the différent column won't be the same in real life – Hadrien Beaujean Aug 03 '20 at 09:58
  • 1
    `(Get-Content $csv) -replace '(?m)"([^,]*?)"(?=,|$)', '$1' | Set-Content $csv` - This should solve your issue. But you need to use `Get-content` instead of importing it as csv – Ranadip Dutta Aug 03 '20 at 10:02
  • If you really want to continue to use `Export-CSV` for this task, try [tag:powershell-7.0]. It has both a `-QuoteFields` option, _(Specifies the names of the columns that should be quoted. When this parameter is used, only the specified columns are quoted.)_, and a `-UseQuotes` option, _(Specifies when quotes are used in the CSV files. Possible values are: `Never` - don't quote anything, `Always` - quote everything [default], and `AsNeeded` - only quote fields that contain a delimiter character)_. – Compo Aug 03 '20 at 10:04
  • Thanx looks like your solution's working, I just have to loop on my differents files – Hadrien Beaujean Aug 03 '20 at 10:11

0 Answers0