0

This is a popular subject on the Web but I can't find a simple way to do a find-and-replace inside a CSV file. My CSV file looks like this:

"0.219530613504834,43.7737904197643,PR RUE D'ARTOIS"
"0.522235882011867,41.7681203998576,PR DE LA FOSSE AU ROI"
"0.039404145384227,44.7565229712732,PR DES PETITS PRES"

I need to remove double quotes at the beginning and end of each line.

I tried the following script:

setlocal enabledelayedexpansion

for /f "tokens=*" %%a in (D:/data.csv) do (
    set temp=%%a
    set temp=%temp:^"=%
)

My logic is to store each line in %%a and to replace the double quotes by nothing.

I escape the double quote with ^.

This script is doing nothing. Could you help?

2 Answers 2

2

There is a much simpler solution that avoids delayed expansion altogther - The ~ modifier strips enclosing quotes from FOR variable content.

for /f "delims=" %%a in (D:/data.csv) do echo %%~a
Sign up to request clarification or add additional context in comments.

3 Comments

Good job! This do the job perfectly... However, I will prefer @Klitos Kyriacou's answer as I also have a few double quotes inside the lines that I need to get rid of too.
@wiltomap - Be careful about removing additional quotes from a CSV file - they may be important.
OK @dbenham, thanks for the warning! Anyway, my CSV file is very simple and I'm sure I can delete these characters safely.
1

Your attempt has several issues:

  1. You need to use delayed expansion (use ! instead of %). Without delayed expansion, the variable gets expanded before the loop is entered.

  2. It's more reliable to use "delims=" than "tokens=*". With "tokens=*" each line is still tokenized, and then all tokens are concatenated separated by a space. But the tokens may have originally been separated by a ;. So if you had "one;two" in the input you'd get "one two" in the output.

  3. You don't need to escape the ".

  4. You need to print temp, not just modify it without using the new value.

    setlocal enabledelayedexpansion
    
    for /f "delims=" %%a in (D:/data.csv) do (
        set temp=%%a
        echo !temp:"=!
    )
    

4 Comments

Thanks! But my file still includes double quotes. In the cmd window, I can control that temp is echoed properly without double quotes, but the file isn't changed (double quotes are not removed from it).
This doesn't make in-place modifications to the file. It produces new output, leaving the input unchanged. You can pipe the output of your batch script using ` > newfile` and then check the contents of newfile and, if it's what you want, you can replace the old file.
Thanks, that's what I was thinking about. I just wanted to make sure that it was not possible to do directly inside the input.
You are correct that many people use "tokens=*" when they really should use "delims=". But your explanation is wrong - tokens=* does not tokenize the entire line. It simply strips leading delimiters from the line, and leaves the rest alone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.