在命令行或批处理文件中创建带有日期和时间的文件名的最简单方法
Simplest way to create filename with date and time in command line or batch file
我需要将 date/time 附加到一些一天内生成多次的测试日志文件。有人建议这样做:
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
然后拼接在一起,但我觉得不对。我想知道是否有更好或更简洁的方法?我想会有一个简单的解决方案,因为这种需求很普遍。
谢谢!
处理时间戳时,您可以检索我在此演示 shell 文件中显示的日期(使用 bash):
#!/bin/bash
#gets the current timestamp
current_time=$(date "+%Y%m%d-%H%M%S")
echo "Current Time : $current_time"
#crafts the new filename appending $current_time to the original filename
original_filename="filename.log"
new_fileName=$original_filename.$current_time
echo "New FileName: " "$new_fileName"
#renames the file
#mv $original_filename $new_fileName
嗯,首先,data/time 格式化实际上在任何编程语言中都是一件大事。例如,看看 C# DateTime 中的一长串 ToString() 方法 class,您就会知道它是什么样的。
对于您的特定任务,假设您的区域设置日期使用“-”,您的区域设置时间使用“:”作为分隔符(您可以回显 date/time 来验证:)
echo %date% %time%
如果你不挑剔,最简单的生成文件名的方法如下,你可以根据自己的语言环境替换'-'和':'字符,这将给你一个有效的文件名大多数系统。
echo "testResult_%date:-=%_%time::=%.xml"
"testResult_20220409_ 84841.28.xml"
如果您只想在命名中使用字母数字字符,那么可能需要一组更简洁的命令:
set shortTime=%time:~0,8% ##eliminate the milliseconds in time string
set shortTime=%shortTime: =0% ##replace empty space with zero in morning hours
echo testResult_%date:-=%_%shortTime::=%.xml ##replace '-' and ':' with nothing
那应该会给你类似的东西
testResult_20220409_084841.xml
但是,如果您需要处理应用程序全球化,最好首先使用 PowerShell(在 Windows 上)之类的工具将 date/time 格式化为预定义的字符串格式。然后它将使任何后续的字符串操作更容易。
powershell get-date -format "{dd-MM-yyyy_HH:mm:ss}"
我需要将 date/time 附加到一些一天内生成多次的测试日志文件。有人建议这样做:
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%
然后拼接在一起,但我觉得不对。我想知道是否有更好或更简洁的方法?我想会有一个简单的解决方案,因为这种需求很普遍。
谢谢!
处理时间戳时,您可以检索我在此演示 shell 文件中显示的日期(使用 bash):
#!/bin/bash
#gets the current timestamp
current_time=$(date "+%Y%m%d-%H%M%S")
echo "Current Time : $current_time"
#crafts the new filename appending $current_time to the original filename
original_filename="filename.log"
new_fileName=$original_filename.$current_time
echo "New FileName: " "$new_fileName"
#renames the file
#mv $original_filename $new_fileName
嗯,首先,data/time 格式化实际上在任何编程语言中都是一件大事。例如,看看 C# DateTime 中的一长串 ToString() 方法 class,您就会知道它是什么样的。
对于您的特定任务,假设您的区域设置日期使用“-”,您的区域设置时间使用“:”作为分隔符(您可以回显 date/time 来验证:)
echo %date% %time%
如果你不挑剔,最简单的生成文件名的方法如下,你可以根据自己的语言环境替换'-'和':'字符,这将给你一个有效的文件名大多数系统。
echo "testResult_%date:-=%_%time::=%.xml"
"testResult_20220409_ 84841.28.xml"
如果您只想在命名中使用字母数字字符,那么可能需要一组更简洁的命令:
set shortTime=%time:~0,8% ##eliminate the milliseconds in time string
set shortTime=%shortTime: =0% ##replace empty space with zero in morning hours
echo testResult_%date:-=%_%shortTime::=%.xml ##replace '-' and ':' with nothing
那应该会给你类似的东西
testResult_20220409_084841.xml
但是,如果您需要处理应用程序全球化,最好首先使用 PowerShell(在 Windows 上)之类的工具将 date/time 格式化为预定义的字符串格式。然后它将使任何后续的字符串操作更容易。
powershell get-date -format "{dd-MM-yyyy_HH:mm:ss}"