简体   繁体   English

使用 Windows 批处理文件在文本文件中添加新行

[英]Add new line in text file with Windows batch file

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

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

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 .如果您需要解决方法(或只是想安全一点),您可以使用此答案中描述的方法。

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

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%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM