我正在运行一个批处理脚本,以在线路中断的文本文件中制作一行。这是用于测试&学习目的。
我有一个名为 file.txt
的文本文件,其中包含
this is line one
this is line two
this is line three
我正在使用代码运行批处理脚本,
type file.txt >> output.txt
echo >> output.txt
type file.txt >> output.txt
预期output.txt
,
this is line one
this is line two
this is line three
,但是,实际输出正在进入output.txt
,
this is line one
this is line two
this is line three
(Empty line here)
我所需要的只是每行之间的线断裂。任何帮助都将受到很多赞赏。
重定向操作员>>
将数据附加到重定向目标(您的情况下的文件output.txt
(,但无法将任何内容插入它们。
要实现目标,您需要按行读取输入文件file.txt
。可以通过for /F
循环来完成:
rem // Write to output file:
> "output.txt" (
rem // Read input file line by line; empty lines and lines beginning with `;` are skipped:
for /F "usebackq delims=" %%L in ("file.txt") do @(
rem // Return current line:
echo(%%L
rem // Return a line-break:
echo/
)
)
这将附加另一个线路破裂,因此在末尾有一个空线。如果要避免这种情况,则可以使用一个变量,例如:
rem // Reset flag variable:
set "FLAG="
rem // Write to output file:
> "output.txt" (
rem // Read input file line by line; empty lines and lines beginning with `;` are skipped:
for /F "usebackq delims=" %%L in ("file.txt") do @(
rem // Do not return line-break before first line:
if defined FLAG echo/
rem // Return current line:
echo(%%L
rem // Set flag variable to precede every following line by a line-break:
set "FLAG=#"
)
)