2

I have a text file with text on each line. I would like to be able to put each line in one long line along with a space. So, if the text file has:

Bob
Jack
Sam

I want the result to be

Bob Jack Sam

Below are two methods that I am working on but I am stuck. Anything in brackets [] means that I know the syntax is completely wrong; I only put it there to show my thought process. The commented sections are just me experimenting and I have left them in case anyone wants to comment on what they would do / why they do what they do.

Method 1:

@echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
for %%a in (!count!) do (
!var%%a! = !var%%a! & " "
echo !var%%a!
)
SET /a count=!count!+1
echo !count!
)

::echo !var1! !var2! !var3!
start "" firefox.exe !var%%a!-1

ENDLOCAL

::echo "endlocal" %var1% %var2% %var3%

Method 2:

@echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
call echo %%var!count!%%

SET /a count=!count!+1
echo !count!
)

::echo !var1! !var2! !var3!
start "" firefox.exe ^
[i = 1]
[for i to !count! do (]
call echo %%var!count!%% & " " & " "^

ENDLOCAL

::echo "endlocal" %var1% %var2% %var3%
MC ND
  • 69,615
  • 8
  • 84
  • 126
Rolo
  • 220
  • 2
  • 13

4 Answers4

3

If you only have three values, you can directly retrieve them from the file

< input.txt (
    set /p "line1="
    set /p "line2="
    set /p "line3="
)
set "var=%line1% %line2% %line3%"
echo("%var%"

If you don't know the number of values, use a for /f command to read the lines and concatenate the contents into a variable.

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "var="
    for /f "usebackq delims=" %%a in ("input.txt") do set "var=!var!%%a "
    echo("%var%"

But if the data can contain exclamation signs, the delayed expansion state will remove them (and the text surounded by them). To avoid it, this can be used

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "var="
    for /f "usebackq delims=" %%a in ("input.txt") do (
        setlocal enabledelayedexpansion
        for /f "tokens=* delims=¬" %%b in ("¬!var!") do endlocal & set "var=%%b%%a "
    )
    echo("%var%"

where the setlocal/endlocal and the inner for are used to avoid problems with ! character

Or you can try something like this

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "delims=" %%a in ('
        "<nul cmd /q /c "for /f "usebackq delims=" %%z in ("input.txt"^) do (set /p ".=%%z "^)""
    ') do set "var=%%a"

    echo("%var%"

It runs a cmd instance to output the input lines as only one output line (<nul set /p is used to ouput the data without line feeds, so all data is in the same line). This is wrapped in a for to retrieve the output inside a variable

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Would this work for you?

@echo off

SET var=
SETLOCAL EnableDelayedExpansion
FOR /f %%i in (afile.txt) DO (
   SET var=!var!%%i 
)
echo !var!
ENDLOCAL

Notice there is a space after the SET var=!var!%%i[Space Here] to separate each word in each row

EDIT: If you want to display the current value in the loop just print %%i with NO concatenation. After the FOR loop finishes it will print the last value assigned to the variable

@echo off

SET var=
SETLOCAL EnableDelayedExpansion
for /f %%i in (input.txt) do (
   SET var=%%i 
   echo !var!
)
echo %var%

Will print:

Bob
Jack
Sam
Sam
Adolfo Perez
  • 2,834
  • 4
  • 41
  • 61
  • Wow! This works great and the code is so small / i.e. not so intimidating looking. If you don't mind, can you please give me a break down step by step of what happens when? – Rolo Sep 11 '14 at 11:29
  • If the file has three lines, why isn't the final output the value of var3 along with a space. What's doing the actual concatenation. – Rolo Sep 11 '14 at 11:37
  • The concatenation takes place inside the FOR loop. The difference is that when you use ! the variables are expanded at execution time instead of at parse time (using %). More info here: http://stackoverflow.com/questions/17265882/parse-time-vs-execution-time http://ss64.com/nt/delayedexpansion.html – Adolfo Perez Sep 11 '14 at 12:44
  • Thanks for the info, but I don't think you are addressing the part I am confused about. Using the names I have in the original question of Bob, Jack, Sam, if I were to put an `echo !var!` in the FOR loop after the SET command, why aren't the results "Bob " for the first echo, then "Jack " for the 2nd, and then "Sam " for the 3rd. Can you break down what side of the `var=!var!%%i` receives and passes the values first. Can you break it down the way it was done with the answer you linked to? Thanks. – Rolo Sep 11 '14 at 13:58
  • The best explanation is that if you put a `echo %var%` right below the SET command it will print out empty since the expansion happens before the block is executed. However if you do a `echo !var!` you will see the variable concatenated contents for each loop. Look at this answer here: http://stackoverflow.com/questions/9102422/windows-batch-set-inside-if-not-working – Adolfo Perez Sep 11 '14 at 14:34
  • Let me put it another way. What if I wanted no concatenation. I just wanted to echo each line of the text file in the for loop and outside of it, I wanted to echo the last line. What would need to change? – Rolo Sep 11 '14 at 15:13
  • 1
    Thank you. I wasn't understanding what was storing the values of the file. I kept on assuming there was an automatic counter involved and that i was the line number. I experimented with echoing %%i before you responded and discovered it was echoing the values of the file. I also looked up some examples online and asked you about the 2nd scenario which helped me to see exactly what's happening. I now understand how and why the concatenation takes place. One thing I am curious about. Within the For loop I am able to `echo %%i`, but outside of it I can't. Is the only way by echoing `var`? – Rolo Sep 11 '14 at 16:17
  • Yes the scope of `%%i` is only inside the FOR loop. – Adolfo Perez Sep 11 '14 at 16:22
0

Try this: Post Edited.

@Echo off
Set "File=Recent_Log.txt"
Set "Output=My_Log.txt"

(
  For /F %%F in (%File%) Do (
    Set /p ".=%%~F "
  )
) > "%Output%" < Nul

Goto :Eof
Honguito98
  • 664
  • 8
  • 13
  • Thanks for trying to help, but this isn't what I asked for. I want to join each line in one text file to one line with one space in between. I decided to experiment with this just to see what it does, as it looks like it would merge the contents of two text files, but all it does is open the file mentioned for the variable File. – Rolo Sep 11 '14 at 11:58
0

Because this is the first link after a Google search, I post my solution(Windows 7/10 .bat):

for %%f in (h:\blaba\*.*) do (type "%%f" & echo.) >> h:\sfsdf\dd.txt

NOTE: When your directory/filename contains spaces use double quotes.

GuyT
  • 4,316
  • 2
  • 16
  • 30