Convert .txt file to .csv file with single quotations

robthomas09
robthomas09 used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Author

Commented:
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
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Sorry, missing caret:
@echo off
setlocal enabledelayedexpansion

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

Open in new window

Author

Commented:
That worked beautifully! Thank You!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial