Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Text 使用.bat向文本文件添加一行代码_Text_Batch File - Fatal编程技术网

Text 使用.bat向文本文件添加一行代码

Text 使用.bat向文本文件添加一行代码,text,batch-file,Text,Batch File,我是批处理文件的新手,但一直在寻找一种向文本文件添加一行文本的方法 测试文件名为test.txt,我只需要一个批处理文件,在文件顶部添加一行文本,让我们说“欢迎”,而不需要对文件进行过度校正 how would i achieve this? 目前我有一个install.bat,它是空的,test.txt文件中没有文本您可以在一个新文件中写入“欢迎”,将“test.txt”的内容附加到该新文件中,然后将新文件重命名为“test.txt” 您可以在新文件中写入“欢迎”,将“test.txt”的内

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

测试文件名为test.txt,我只需要一个批处理文件,在文件顶部添加一行文本,让我们说“欢迎”,而不需要对文件进行过度校正

how would i achieve this?
目前我有一个install.bat,它是空的,test.txt文件中没有文本

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

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


我是一名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所能做的一切…;-)

我是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所能做的一切…;-)

您可以将del+ren与
copy/y new.txt test.txt
@AlexK组合。是的,我已经相应地更新了我的答案,谢谢;)非常感谢:)但我如何在欢迎下添加一个空行?@thenashone更新了答案以添加一个空行。您可以将del+ren与
copy/y new.txt test.txt
@AlexK组合。是的,我已经相应地更新了我的答案,谢谢;)太好了,非常感谢:)但是我如何在欢迎下添加一个空行呢?@thenashone更新了答案,添加了一个空行。