如何使用批处理从txt文件中连接新行上的单词?

时间:2016-08-11 12:27:35

标签: batch-file

我有一个名为test.txt的文本文件,其中包含以下内容,

  

您好
  再见

我有一个batch文件可以回复它们:

  @echo off
  SETLOCAL ENABLEEXTENSIONS
  FOR /F %%x in (test.txt) DO echo %%x

所以打印出来:

Hello 
Goodbye

这是我期望它做的,但我怎样才能接受这个想法并制作一个等于" Hello Goodbye"一起?

1 个答案:

答案 0 :(得分:0)

您可以收集变量中的每一行并在循环后输出。为此,您需要delayed expansion,因为在同一个带括号的代码块中读取和修改变量:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Reset variable:
set "string="
for /F "delims=" %%X in (test.txt) do (
    rem Concatenate strings, separated by a SPACE:
    set "string=!string!%%X "
)
rem Return collected string, removing last SPACE:
echo(%string:~,-1%
相关问题