Avatar of robthomas09
robthomas09
 asked on

Convert .txt file to .csv file with single quotations

Hi All,

I'm using the following .bat file to convert a .txt file to a .csv file

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('type "%~1"') do (
   set line=%%a
   echo "!line:|=","!">>"%~2"
)

Open in new window


However my output file is coming out like this
"999859","0000000000000000000000000","PATIENT NOT FOUND"
"999380","0000000000000000000000000","PATIENT NOT FOUND"

Open in new window


When i need it to be like this, essentially replacing the surrounding double quotes with single quotes.
'999859','0000000000000000000000000','PATIENT NOT FOUND'
'999380','0000000000000000000000000','PATIENT NOT FOUND'

Open in new window


Is there any way to modify the .bat file script to accomplish this? Possibly doing a find and replace on the output file?

Thanks for the assistance!
Windows Batch

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
oBdA

Just change the script to use single quotes instead of double quotes:
@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('type "%~1"') do (
   set line=%%a
   echo '!line:|=','!'>>"%~2"
)

Open in new window

robthomas09

ASKER
I get the following error when i try to run the .bat file from a cmd prompt

C:\Users\djl4616\Desktop>CONVERT test.txt test.csv
''' is not recognized as an internal or external command,
operable program or batch file.

Open in new window


It still creates an output (test.csv) file but it is blank
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
robthomas09

ASKER
That worked beautifully! Thank You!
Your help has saved me hundreds of hours of internet surfing.
fblack61
Bill Prew

Since you know there will be three fields, a slightly simpler approach could be:

@echo off
for /f "tokens=1-3 delims=|" %%a in ('type "%~1"') do echo '%%~a','%%~b','%%~c'>>"%~2"

Open in new window

~bp