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

发布于 2024-12-04 09:34:04 字数 277 浏览 1 评论 0原文

我有一个文本文件,其中有 200 多行,我只想在第 4 行之前添加一个新行。我使用的是 Windows XP。

输入前的示例文本文件:

header 1
header 2
header 3
details 1
details 2

输出后:

header 1
header 2
header 3
<----- This is new line ---->
details 1
details 2

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小情绪 2024-12-11 09:34:04

我相信你正在使用该

echo Text >> Example.txt 

功能?

如果是这样,答案就是简单地添加一个“。” (点)直接在回声之后,没有其他任何东西。

例子:

echo Blah
echo Blah 2
echo. #New line is added
echo Next Blah

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.

Example:

echo Blah
echo Blah 2
echo. #New line is added
echo Next Blah
梦回旧景 2024-12-11 09:34:04

免责声明:以下解决方案不会保留尾随制表符。


如果您知道文本文件中的确切行数,请尝试以下方法:

@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%

循环从原始文件中逐行读取行并将其输出。输出被重定向到临时文件。当到达某一行时,在其前面输出一个空行。

完成后,原始文件将被删除,临时文件将被指定为原始名称。


UPDATE

如果事先未知行数,可以使用以下方法获取:(

FOR /F %%C IN ('FIND /C /V "" ^<%origfile%') DO SET totallines=%%C

此行仅替换上述脚本中的 SET Totallines=200 行。 )

该方法有一个小缺陷:如果文件以空行结尾,则结果将是实际行数减一。如果您需要解决方法(或者只是想安全起见),可以使用 这个答案

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.)

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.

天生の放荡 2024-12-11 09:34:04

您可以使用:

type text1.txt >> combine.txt
echo >> combine.txt
type text2.txt >> combine.txt

或类似的东西:

echo blah >> combine.txt
echo blah2 >> combine.txt
echo >> combine.txt
echo other >> combine.txt

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
救星 2024-12-11 09:34:04

假设您要插入特定的文本行(不是空行):

@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%

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%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文