Link to home
Start Free TrialLog in
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!
Avatar of oBdA
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

Avatar of 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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked beautifully! Thank You!
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