Niklas J. MacDowall
Niklas J. MacDowall

Reputation: 683

Batch processing multiple lines to a single line?

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

Upvotes: 1

Views: 4510

Answers (2)

Niklas J. MacDowall
Niklas J. MacDowall

Reputation: 683

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!
)

Upvotes: 2

Raymond Chen
Raymond Chen

Reputation: 45173

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

Upvotes: 3

Related Questions