在命令行 (Windows) 中将文本添加到另一个文本文件开头的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/550728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easiest way to add a text to the beginning of another text file in Command Line (Windows)
提问by dr. evil
What is the easiest way to add a text to the beginning of another text file in Command Line (Windows)?
在命令行 (Windows) 中将文本添加到另一个文本文件开头的最简单方法是什么?
回答by DarkwingDuck
echo "my line" > newFile.txt
type myOriginalFile.txt >> newFile.txt
type newFile.txt > myOriginalFile.txt
Untested. Double >> means 'append'
未经测试。双 >> 表示“追加”
回答by dbenham
Another variation on the theme.
主题的另一个变化。
(echo New Line 1) >file.txt.new
type file.txt >>file.txt.new
move /y file.txt.new file.txt
Advantages over other posted answers:
与其他已发布答案相比的优势:
- minimal number of steps
- no temp file left over
- parentheses prevents unwanted trailing space in first line
- the move command "instantaneously" replaces the old version with the new
- the original file remains unchanged until the last instant when it is replaced
- the final content is only written once - potentially important if the file is huge.
- 最少的步骤
- 没有剩余的临时文件
- 括号可防止第一行出现不需要的尾随空格
- 移动命令“立即”用新版本替换旧版本
- 原始文件保持不变,直到被替换的最后一刻
- 最终内容只写一次 - 如果文件很大,可能很重要。
回答by paxdiablo
The following sequence will do what you want, adding the line "new first line
" to the file.txt
file.
以下序列将执行您想要的操作,将“ new first line
”行添加到file.txt
文件中。
ren file.txt temp.txt
echo.new first line>file.txt
type temp.txt >>file.txt
del temp.txt
Note the structure of the echo. "echo.
" allows you to put spaces at the beginning of the line if necessary and abutting the ">
" redirection character ensures there's no trailing spaces (unless you want them, of course).
注意回声的结构。“ echo.
” 允许您在必要时在行首放置空格,并且紧靠“ >
”重定向字符可确保没有尾随空格(当然,除非您需要它们)。
回答by anonperson
The following will also work:
以下也将起作用:
echo "my line" > newFile.txt
type newfile.txt myOriginalFile.txt > myOriginalFile.txt
In the first line you are writing my line into newfile.txt. In the second line you are replacing the text from myOriginalFile.txt by overwriting it with the text from newfile.txt and myOriginalFile.txt, creating a new myOriginalFile.txt that contains both.
在第一行中,您将我的一行写入 newfile.txt。在第二行中,您将替换 myOriginalFile.txt 中的文本,方法是用 newfile.txt 和 myOriginalFile.txt 中的文本覆盖它,创建一个包含两者的新 myOriginalFile.txt。
回答by user7351993
If the first part of the line can be sorted, such as date/time then use the SORT /R command to put the most recent entries at the top of the file. The following will place a date/time stamp in the form of "YYYY-MM-DD HH:DD:SS AM/PM" to the start of each line:
如果该行的第一部分可以排序,例如日期/时间,则使用 SORT /R 命令将最近的条目放在文件顶部。以下将在每行的开头以“YYYY-MM-DD HH:DD:SS AM/PM”的形式放置日期/时间戳:
echo %DATE:~-4%-%DATE:~7,2%-%DATE:~4,2% %TIME% "my text line" >> myOriginalFile.txt
sort /R myOriginalFile.txt /O myOriginalFile.txt
However, as files grow very large this method (as well as the ones above) become slow. Consider sorting only when required instead of with each entry - or use another scripting/programming language
然而,随着文件变得非常大,这种方法(以及上面的方法)变得很慢。仅在需要时才考虑排序,而不是对每个条目进行排序 - 或使用另一种脚本/编程语言
回答by Thomas H. Schmidt
If you want to process bigger files the accepted solution becomes pretty slow. Then it's faster to use copy with a '+'
如果你想处理更大的文件,接受的解决方案会变得非常慢。然后使用带有“+”的副本会更快
echo "my line" > newFile.txt
copy newFile.txt+myOriginalFile.txt combinedFile.txt
move /Y combinedFile.txt myOriginalFile.txt
del newFile.txt
回答by Madison Courto
Via PowerShell;
通过 PowerShell;
@("NEW Line 1","NEW Line 2") + (Get-Content "C:\Data\TestFile.txt") | Set-Content "C:\data\TestFile.txt"