在命令提示符中使用echo创建文本文件

8

我正在使用命令提示符中的echo命令,在桌面上创建hello_world.txt文件,并提供以下输入:

echo hello world > C:\Users\user\Desktop\hello_world.txt

第一个代码有效,然后我尝试使用%userprofile%。比如,我想把它给另一个用户。

echo hello world > C:\Users\%userprofile%\Desktop\hello_world.txt

这个方法不起作用。是否有简单的echo命令行可以创建文本文件?


你使用什么命令行?在Windows下使用Cygwin还是cmd.exe? - Binkan Salaryman
如果您习惯于使用Linux shell,可以安装git bash。您将拥有所有的Linux命令,并且像echo hello world > C:\Users\user\Desktop\hello_world.txt这样的命令通常都能正常工作 ;) - Guillaume Munsch
1
你能否在命令行中执行“echo %userprofile%”来尝试打印变量“%userprofile%”? - bmscomp
3个回答

11
您的命令应该是: echo hello world > %userprofile%\Desktop\hello_world.txt 如果在%userprofile%之前添加任何内容,它将不起作用,因为%userprofile%是完整路径,所以C:\Users\%userprofile%\Desktop\hello_world.txt会被替换为C:\Users\C:\Users\cromix\Desktop\hello_world.txt

3

1. 创建新文件并添加新文本,或者覆盖现有文件中的所有内容:

echo > file_name text_you_want_to_in_your_file

2. 添加新行的下一段文本:

echo >> file_name text_you_want_to_add_in_your_file

-2

你可以输入:

touch <文件名>

非常重要的注意事项: 你只能在cmder或Mac OS上使用这个命令。

而在Windows中,你需要下载cmder才行

这是一个例子


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接