Batch Files: How to concatenate each line in a text file?

别来无恙 提交于 2019-12-12 19:10:50

问题


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%

回答1:


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




回答2:


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



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/25751630/batch-files-how-to-concatenate-each-line-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!