windows 如何从bat文件中的文件中获取日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1422781/
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
How to get the date from a file in a bat file?
提问by Alceu Costa
I'm writing a bat script and I have to save the date from a file to a variable. How can I do that?
我正在编写一个 bat 脚本,我必须将日期从文件保存到变量。我怎样才能做到这一点?
I will use the variable to check wether the file is older than N days, so it would be nice to save the date in a format that would allow me to do such test.
我将使用该变量来检查文件是否早于 N 天,因此最好以允许我进行此类测试的格式保存日期。
回答by Joey
Date manipulation in batch files it nearly impossible to get right in a sensible way. The only thing you can do is support your culture and ignore the rest.
批处理文件中的日期操作几乎不可能以合理的方式正确处理。你唯一能做的就是支持你的文化,而忽略其他的。
However, if you have the name of the file in a parameter to the batch (or a subroutine), you can use %~t1
. Otherwise the following:
但是,如果批处理(或子例程)的参数中有文件名,则可以使用%~t1
. 否则如下:
for %%x in (%file%) do set datetime=%%~tx
On my machine this comes out as "2009-08-28 16:13" which can then be parsed into its individual parts:
在我的机器上,它显示为“2009-08-28 16:13”,然后可以将其解析为各个部分:
:dissect_date_time
for /f "tokens=1-5 delims=-: " %%a in (%1) do set year=%%a&set month=%%b&set day=%%c&set hour=%%d&set minute=%%e
goto :eof
this is a subroutine you can call with
这是一个你可以调用的子程序
call :dissect_date_time %datetime%
and then you have the individual parts in their respective environment variables. Adapt accordingly to suit your date/time format. This one makes heavy use of for
as a tokenizer.
然后您在各自的环境变量中拥有各个部分。相应地进行调整以适合您的日期/时间格式。这个大量for
用作标记器。
Once you have the individual parts you can try figuring out how old the file is by simply doing the same process with the current date/time and subtracting. Won't be much fun, but works.
获得各个部分后,您可以尝试通过简单地对当前日期/时间执行相同的过程并减去来计算文件的年龄。不会很有趣,但有效。
回答by wierob
回答by gimel
The batch command language is not really up to the task. Microsoft suggests PowerShell
.
批处理命令语言并不能真正胜任这项任务。微软建议PowerShell
。
Microsoft Windows PowerShell command line shell and scripting language helps IT professionals achieve greater control and productivity. Using a new admin-focused scripting language, more than 130 standard command line tools, and consistent syntax and utilities, Windows PowerShell allows IT professionals to more easily control system administration and accelerate automation. Windows PowerShell is easy to adopt, learn, and use, because it works with your existing IT infrastructure and existing script investments, and because it runs on Windows XP, Windows Vista, and Windows Server 2003.
Microsoft Windows PowerShell 命令行外壳和脚本语言可帮助 IT 专业人员实现更好的控制和生产力。Windows PowerShell 使用一种新的以管理员为中心的脚本语言、130 多种标准命令行工具以及一致的语法和实用程序,使 IT 专业人员能够更轻松地控制系统管理并加速自动化。Windows PowerShell 易于采用、学习和使用,因为它适用于您现有的 IT 基础结构和现有的脚本投资,并且因为它可以在 Windows XP、Windows Vista 和 Windows Server 2003 上运行。
For a discussion of a similar task using PowerShell, see powershell-script-to-delete-old-files.
有关使用 PowerShell 的类似任务的讨论,请参阅powershell-script-to-delete-old-files。