1

In batch (.bat) if I have a file that looks like this:

test1
test2
test3

How can I rewrite the content of that file to be on the same line? e.g:

test1, test2, test3


Thanks in advance!
Niklas

2 Answers2

3

Use the for /f command to read a file line-by-line. You can then accumulate the results into a variable for final output.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
2

I've solved it!

for /f "Tokens=*" %%i in (file.txt) do (
set var=%%i
rem The magic below appends the data var with the data var + the var
if defined var set data=!data!, !var!
)
  • 1
    Yes this works, but there are some issues with exclamation marks, carets and empty lines. You can read at [SO:how to read a file](http://stackoverflow.com/questions/206114/dos-batch-files-how-to-read-a-file/4531090#4531090) – jeb Oct 21 '11 at 12:01
  • 1
    The var variable is not necessary. Also, is better to use "delims=" instead of "tokens=*", that is: `for /f "delims=" %%i in (file.txt) do set data=!data!, %%i`, just clear the data variable before the for. – Aacini Oct 21 '11 at 13:17
  • 1
    This is just proof of concept, not the actual code. But yes, I do use enabledelayedexpansion together with the exclamation marks. Thanks for your replies. – Niklas J. MacDowall Oct 21 '11 at 13:57