Bynari
Bynari

Reputation: 69

Combine multiple lines from one text file into one line

So I have a file with multiple lines of letters and numbers as shown:

a 
1
h
7
G
k
3
l

END

I need a type of code that combines them together and preferably outputs it into a variable as shown:

var=a1h7Gk2l

Any help would be appreciated.

Upvotes: 3

Views: 2599

Answers (2)

dbenham
dbenham

Reputation: 130819

@echo off
setlocal enableDelayedExpansion
set "var="
for /f "usebackq" %%A in ("test.txt") do set var=!var!%%A
echo !var!

Edit
I assumed "END" does not physically exist in your file. If it does exist, then you can add the following line after the FOR statement to strip off the last 3 characters.

set "!var!=!var:~0,-3!"

Upvotes: 4

Andriy M
Andriy M

Reputation: 77657

Or, if you just want to put the result into a file (as opposed to storing it in memory for some purpose), you could do something like this:

@ECHO OFF
TYPE NUL >output.txt
FOR /F %%L IN (input.txt) DO (
  IF NOT "%%L" == "END" (<NUL >>output.txt SET /P "=%%L")
)
ECHO.>>output.txt

The last command may be unnecessary, depending on whether you need the End-of-Line symbol at, well, the end of the line.

Upvotes: 1

Related Questions