Search code examples
xmlcsvbatch-filequotes

Remove specific quotes from csv file using bat file


I have next csv file with data like xml

  "<Person>
      <Name> ""Test"" </Name> <Surname>""Test1""</Surname>
   </Person>
   <Person>
     <Name>""TestA""</Name>  <Surname>""""</Surname>
   </Person>"

I want to replace """" with "" and replace "" with ". I found next bat file with code witch works in way that remove all quotes and spaces. But I dont now how to modify code to replace only specific quotes.

  @echo off
  setlocal EnableDelayedExpansion

  set FileIn=C:\Users\PC\Documents\test.csv
  set FileOut=C:\Users\PC\Documents\TestNew.csv

    (
      For /F "usebackq tokens=*" %%A in ("%FileIn%") do (
      set Line = %%A
      set Line=!Line:"=!
      Echo.!Line!
    )
    )
  > "%FileOut%"

Can anyone help me to resolve this problem? To get csv like this:

  <Person>
   <Name> "Test" </Name> <Surname>"Test1"</Surname>
  </Person>
  <Person>
   <Name>"TestA"</Name> <Surname>""</Surname>
  </Person>

Now I have problem of limit character in batch, can someone post me example with powershell please


Solution

  • You remove quotes. Ok, that's a step in the right direction.
    But you have to save double-doublequotes.

    You can do this by first replacing "" with another char (chose one that definitely won't be in your data), then delete each remaining " and finally revert that special char (§ below for demo purposes) back into ""

    For /F "usebackq tokens=*" %%A in ("%FileIn%") do (
      set "Line=%%A"
      set "Line=!Line:""=§!"
      set "Line=!Line:"=!"
      set "Line=!Line:§="!"
      Echo.!Line!
    )
    

    (Note: this will still delete empty lines. There are ways to avoid this, but this wasn't your question, so I left it out)