使用 Windows 批处理文件在文本文件中添加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7396680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add new line in text file with Windows batch file
提问by newbie18
I have a text file which has more than 200 lines in it, and I just want to add a new line before line 4. I'm using Windows XP.
我有一个包含 200 多行的文本文件,我只想在第 4 行之前添加一个新行。我使用的是 Windows XP。
Example text file before input:
输入前的示例文本文件:
header 1
header 2
header 3
details 1
details 2
After output:
输出后:
header 1
header 2
header 3
<----- This is new line ---->
details 1
details 2
回答by avitex
I believe you are using the
我相信你正在使用
echo Text >> Example.txt
function?
功能?
If so the answer would be simply adding a "." (Dot) directly after the echo with nothing else there.
如果是这样,答案就是简单地添加一个“。” (点)直接在 echo 之后,没有别的东西。
Example:
例子:
echo Blah
echo Blah 2
echo. #New line is added
echo Next Blah
回答by khaled
You can use:
您可以使用:
type text1.txt >> combine.txt
echo >> combine.txt
type text2.txt >> combine.txt
or something like this:
或类似的东西:
echo blah >> combine.txt
echo blah2 >> combine.txt
echo >> combine.txt
echo other >> combine.txt
回答by Andriy M
DISCLAIMER: The below solution does not preserve trailing tabs.
免责声明:以下解决方案不保留尾随制表符。
If you know the exact number of lines in the text file, try the following method:
如果您知道文本文件中的确切行数,请尝试以下方法:
@ECHO OFF
SET origfile=original file
SET tempfile=temporary file
SET insertbefore=4
SET totallines=200
<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
SETLOCAL EnableDelayedExpansion
SET /P L=
IF %%i==%insertbefore% ECHO(
ECHO(!L!
ENDLOCAL
)
) >%tempfile%
COPY /Y %tempfile% %origfile% >NUL
DEL %tempfile%
The loop reads lines from the original file one by one and outputs them. The output is redirected to a temporary file. When a certain line is reached, an empty line is output before it.
循环从原始文件中逐行读取并输出它们。输出被重定向到一个临时文件。当到达某一行时,在它之前输出一个空行。
After finishing, the original file is deleted and the temporary one gets assigned the original name.
完成后,原始文件被删除,临时文件被分配原始名称。
UPDATE
更新
If the number of lines is unknown beforehand, you can use the following method to obtain it:
如果事先不知道行数,可以使用以下方法获取:
FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C
(This line simply replaces the SET totallines=200
line in the above script.)
(此行只是替换SET totallines=200
了上述脚本中的行。)
The method has one tiny flaw: if the file ends with an empty line, the result will be the actual number of lines minus one. If you need a workaround (or just want to play safe), you can use the method described in this answer.
该方法有一个小缺陷:如果文件以空行结尾,结果将是实际行数减一。如果您需要解决方法(或只是想安全一点),您可以使用此答案中描述的方法。
回答by user2746443
Suppose you want to insert a particular line of text (not an empty line):
假设您要插入特定的文本行(不是空行):
@echo off
FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C
set /a totallines+=1
@echo off
<%origfile% (FOR /L %%i IN (1,1,%totallines%) DO (
SETLOCAL EnableDelayedExpansion
SET /p L=
IF %%i==%insertat% ECHO(!TL!
ECHO(!L!
ENDLOCAL
)
) >%tempfile%
COPY /Y %tempfile% %origfile% >NUL
DEL %tempfile%