Merge multiple lines from one text file into one line

So, I have a file with several 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 to a variable, as shown:

var=a1h7Gk2l

Any help would be appreciated.

+3
source share
2 answers
@echo off
setlocal enableDelayedExpansion
set "var="
for /f "usebackq" %%A in ("test.txt") do set var=!var!%%A
echo !var!

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

set "!var!=!var:~0,-3!"
+4
source

Or, if you just want to put the result in a file (as opposed to storing it in memory for some purpose), you can 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

, , , , .

+1

All Articles