0

我是批处理文件的新手,但一直在寻找一种将一行文本添加到文本文件的方法

测试文件称为 test.txt,我需要的只是批处理文件,以便在文件顶部添加一行文本让我们说“欢迎”而不用过度纠正文件

how would i achieve this?

目前我有一个空白的 install.bat,test.txt 文件中没有文本

4

2 回答 2

2

您可以在新文件中写入“欢迎”,将“test.txt”的内容附加到该新文件中,然后将新文件重命名为“test.txt”。

@echo off
echo Welcome > new.txt
echo. >> new.txt
type test.txt >> new.txt
copy /y new.txt test.txt
del new.txt
于 2012-10-25T12:12:44.713 回答
2

我是 Linux 用户,但过去我不得不处理 Windows 命令行解释器的限制。我仍然这样做,偶尔。无论如何,这是我的贡献:

@echo off
rem AddToTop - Add line to top of file
if [%2] == [] goto help
set file=%1
set line=%2 %3 %4 %5 %6 %7 %8 %9
if exist %file% (
echo %line%>%file%.tmp
type %file%>>%file%.tmp
del %file%
rename %file%.tmp %file%
) else (
echo File "%file%" not found.
)
goto end
:help
echo Syntax: addtotop.bat file line
:end

希望能帮助到你。这就是我能用这个糟糕的 CMD.EXE 做的一切...... ;-)

于 2012-10-25T12:54:23.640 回答