Batch file to add characters to beginning and end of each line in txt file

Go To StackoverFlow.com

8

Hey guys I have a text file, I was wondering anyone have a batch file to add " to the beninning and ", at the end of each line in a text file?

For example I have

1
2
3

and I want

"1",
"2",
"3",

If some could paste a quick one it would help me out =)

EDIT (from comment to @mastashake57's post):

Im on windows, My appologies if it felt like i was asking someone to do it, This is what I have.

@echo off 
setlocal 
set addtext=test 
for /f "delims=" %%a in (list.txt) do (echo/|set /p =%%a%addtext% & echo\ & echo) >>new.txt 

But i cant figure out how to put commas as it thinks its part of the command i assume or something of that sort. this only places text in the font of each line

2012-04-05 01:19
by Adil Chaudhry
Welcome to StackOverflow. This is not a "please do my work for me" or "please give me code" site. What have you tried so far that isn't working? If you post your attempt, and explain what isn't working like you expect, I'm sure someone here can help. We do expect you to show some effort on your own first, though. : - Ken White 2012-04-05 01:23
And mastashake57's answer bring up the question of what operating system are you on? windows and lunix have very different batch file - apple16 2012-04-05 01:31


8

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
echo ^"%%a^",>>output.txt
)

-joedf

2012-04-05 01:41
by Joe DF
I don't think enabling delayed variable expansion is necessary, and I don't see how N is used anywhere. Otherwise, this sounds like an answer - mojo 2013-06-22 06:59
:) haha... Whoops yep I don't know why I left the N variable there... :P yeah... I guess I wasn't paying attention,... Bazzazle! It works.. :D ; - Joe DF 2013-06-29 14:51


4

Off the top of my head, in Linux, you can...

$ for each in `cat filename` ; do echo \"$each\", ; done >> newfilename

"1",
"2",
"3",
"4",
"5",

Edited - since it's for Windows, this did the trick for me:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (filename.txt) do (
echo "%%a", >>newfilename.txt
)
2012-04-05 01:28
by Carlos
Im on windows, My appologies if it felt like i was asking someone to do it, This is what I have.

@echo off setlocal set addtext=test for /f "delims=" %%a in (list.txt) do (echo/|set /p =%%a%addtext% & echo\ & echo) >>new.txt

But i cant figure out how to put commas as it thinks its part of the command i assume or something of that sort. this only places text in the font of each line - Adil Chaudhry 2012-04-05 01:41

Edited to reflect the Windows solution - Carlos 2012-04-05 02:04
Ads