CMD添加新文本行

时间:2014-02-20 20:26:28

标签: batch-file cmd newline

好的,所以我基本上需要一个能做类似这样的代码:

@echo off
start:
echo Type what you want me to remember!
set /p newline=
<add %newline% to Line 4 in safe.txt>
goto start

我不想替换safe.txt中的现有第4行。

1 个答案:

答案 0 :(得分:0)

如果要将输入附加到文件末尾:

@echo off
start:
echo Type what you want me to remember!
set /p newline=
Echo %newline% >> safe.txt
goto start

如果您想在第4行之后添加

@echo off
start:
echo Type what you want me to remember!
set /p newline=

setlocal enabledelayedexpansion
set count=0
ren safe.txt safe.tmp
for /f "delims=" %%a in (safe.tmp) do (
set /a count+=1
Echo %%a >> safe.txt
if !count!==4 Echo !newline! >> safe.txt
)
del safe.tmp
goto start

其中一个代码可以帮助您解决问题。

莫纳