9

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Adil Chaudhry
  • 289
  • 2
  • 4
  • 12
  • 1
    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 Apr 05 '12 at 01:23
  • And mastashake57's answer bring up the question of what operating system are you on? windows and lunix have very different batch files – apple16 Apr 05 '12 at 01:31

4 Answers4

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

-joedf

Joe DF
  • 5,438
  • 6
  • 41
  • 63
  • 3
    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 Jun 22 '13 at 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 Jun 29 '13 at 14:51
5

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
)
Carlos
  • 1,897
  • 3
  • 19
  • 37
  • 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 Apr 05 '12 at 01:41
  • Edited to reflect the Windows solution. – Carlos Apr 05 '12 at 02:04
0
set "f=PATH\FILE.txt"
call :add_str_beginning_end_each_line "BEGIN_LINE--" "--END_LINE" "%f%"


REM : Adds strings at the beginning and end of each line in file
:add_str_beginning_end_each_line
    set "str_at_begining=%~1"
    set "str_at_end=%~2"
    set "input_file=%~3"

    set "tmp_file=tmp.ini"

    REM : >NUL => copy command is silent
    REM : Make a backup
    copy /Y "!input_file!" "!input_file!.bak" >NUL
    copy /Y "!input_file!" "!tmp_file!" >NUL
    del "!input_file!"

    REM : Add strings at each line
    for /f "delims=" %%a in (!tmp_file!) do (
        >>"!input_file!" echo !str_at_begining!%%a!str_at_end!
    )

    REM : delete backup
    del "!tmp_file!"
    del "!input_file!.bak"

    REM : exiting the function only
    EXIT /B 0

You can edit the code :

    "!input_file!" echo !str_at_begining!%%a!str_at_end!

By removing !str_at_end! to add str only at the beginning of the line, where %%a is the actual line.

jamarir
  • 9
  • 3
0

The script uses a FOR loop count the # of lines, and FOR /L along with SET /P to read the file line-by-line:

@echo off
SETLOCAL EnableDelayedExpansion

::Initialize SUB character (0x1A)
>nul copy nul sub.tmp /a
for /F %%S in (sub.tmp) do set "sub=%%S" SUB CHARACTER

::Variables
;set "lines=-1" Exclude LAST line
set "in=<CHANGE THIS>"
set "out=text.txt"

::Count lines
FOR /F tokens^=*^ delims^=^ eol^= %%L in (
'2^>nul findstr /N "^" "%in%"'
) do set /a "lines+=1"

::Read file using SET /P
>newline.tmp <"!in!" (
  for /L %%a in (1 1 %lines%) do (
    set "x=" For EMPTY lines
    set /p "x="
    echo("!x!",
  )
    ;set "x="
    ;echo("!x!",!sub!
)

::Trim trailing newline appended by ECHO
;copy newline.tmp /a "!out!" /b
;del sub.tmp newline.tmp

The foolproof way of counting the number of lines is explained in this answer.
Takes advantage of the SUB character (0x1A) to trim trailing newlines, discussed here on DosTips. If you don't want to trim them, just comment out the lines that start with ;.

ScriptKidd
  • 803
  • 1
  • 5
  • 19