我有一个.txt文件,其中包含一个列表,比方说,数字,每行一个。 我想解析该.txt文件并将所有这些数字放在一行中,用空格分隔。我到目前为止尝试的只是为了看看我是否可以解析文本中的行,但它似乎不起作用。 我试过这个命令:
C:\Users\Adi>for /F "tokens=*" %A in ("file.txt") do (set line=%G echo !line!)
但结果我得到了:
C:\Users\Adi>(set line=file.txt echo !line!)
我的.txt文件的内容:
685576 685569 685564 685265 685229 685222 685121 684189 683905 681508 681321 680799 680436 679650 679424
我想将这些全部放在一行中,用空格分隔。你能告诉我哪里出错了吗? 谢谢!
要提到我的.txt有超过100行。不确定这是否会以某种方式影响! 谢谢!
Adi C
答案 0 :(得分:1)
这是一个完整的批处理文件,希望能够实现您的目标:
@For /F "UseBackQ Delims=" %%A In ("myFiles.txt") Do @Call Set "line=%%line%% %%A"
@Echo(%line:~1%&@Pause
答案 1 :(得分:0)
将文本文件的多行附加到单个行的简单方法是将它们附加到变量中并立即输出其内容,如other answer所示 - 尽管我更喜欢{{ 3}}超过call
由于更好的性能和对特殊字符的安全性:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=%~dpn0.txt" & rem // (path of source file)
set "_TARGET=%~dp0line.txt" & rem // (path of target file)
set "_SEPCHR= " & rem // (separator character)
rem // Initialise buffers:
set "COLL=" & set "SEP="
for /F usebackq^ delims^=^ eol^= %%L in ("%_SOURCE%") do (
rem // Store current line:
set "LINE=%%L"
rem /* Use delayed expansion to append lint to buffer in order to avoid
rem trouble with special characters; use a `for /F` loop to transport
rem the buffer beyond the `endlocal` barrier: */
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%K in ("!COLL!!SEP!!LINE!") do (
endlocal & set "COLL=%%K"
)
rem // Apply separator character only after the first line:
set "SEP=%_SEPCHR%"
)
rem // Write collected buffer data into target file:
setlocal EnableDelayedExpansion
> "!_TARGET!" echo(!COLL!
endlocal
endlocal
exit /B
但是,如果构建输出行的总长度超过大约8190字节或字符,则上述方法将失败。
这是一个改进版本,它不限制输出行的长度;输入文件的行长度仍然限制在大约8190字节/字符,但是:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=%~dpn0.txt" & rem // (path of source file)
set "_TARGET=%~dp0line.txt" & rem // (path of target file)
set "_TMPLIN=%~dp0line.tmp" & rem // (path of temporary file)
set "_SEPCHR= " & rem // (separator character)
rem // Store an EOF (end-of-file) character in a variable:
for /F %%K in ('forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo/0x1A"') do set "EOF=%%K"
rem // Ensure the target file to exist and to be empty:
> "%_TARGET%" rem/
rem // Read source file line by line:
set "FIRST=#"
for /F usebackq^ delims^=^ eol^= %%L in ("%_SOURCE%") do (
rem // Write current line into temporary file with an EOF character appended:
> "%_TMPLIN%" (
if defined FIRST (
rem // Do not prepend a separator character to first line:
echo(%%L%EOF%
set "FIRST="
) else (
rem // Do prepend a separator character to every other line:
echo(%_SEPCHR%%%L%EOF%
)
)
rem /* Append content of temporary file to target file; since EOF characters and
rem everything behind are ignored in text mode (`/A`), also the line-breaks
rem introduced by `echo` are not included in the output; to avoid a final EOF
rem character to be appended, the target file must be declared binary (`/B`): */
> nul copy /A "%_TARGET%" + "%_TMPLIN%" "%_TARGET%" /B
)
rem // Clean up temporary file:
del "%_TMPLIN%"
endlocal
exit /B