windows 如何将输出重定向到名称为当前日期和时间的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3827153/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:20:18  来源:igfitidea点击:

How to redirect output to a file which name is the current date and time?

windowscommand-line

提问by Misha Moroshko

I would like to redirect the output of a command (in the Windows command line) to a file which name is the current date and time. For example:

我想将命令的输出(在 Windows 命令行中)重定向到名称为当前日期和时间的文件。例如:

my_path\mysqldump.exe my_database_name > auto_generated_file_name

where auto_generated_file_nameshould be something like 2010_09_30___11_41_58.txt.

哪里auto_generated_file_name应该是这样的2010_09_30___11_41_58.txt

This command will automatically run from time to time. This is the reason I need the file name to be automaticallygenerated.

此命令将不时自动运行。这就是我需要自动生成文件名的原因。

What is the easiest method to achieve this ?

实现这一目标的最简单方法是什么?

采纳答案by ars

The following command creates a blank file with the expected filename:

以下命令创建一个具有预期文件名的空白文件:

> type nul > %date:~10,4%_%date:~4,2%_%date:~7,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%.txt

> dir /b
2010_09_29__22_12_44.txt

You can use the part after type nul >in place of your auto_generated_file_name.

您可以type nul >在代替您的auto_generated_file_name.

回答by Seth

There's a nice solution here, and it almost matches your example formatting:

这里有一个很好的解决方案,它几乎与您的示例格式相匹配:

set dd = %date% %Time% 
my_command > MyFile__%dd:~0,2%_%dd:~3,2%_%dd:~6,4%___%dd:~11,2%_%dd:~14,2%.txt 

Output: "MyFile__22_05_2009__6_20.txt"

输出:“我的文件__22_05_2009__6_20.txt”