windows 批处理文件可以更改系统日期吗?用属性保存文件;将日期改回当前日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3672079/
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
Can a batch file change the system date; save file with attributes; change date back to current date?
提问by Edoctoor
Can a batch file change the system date; save file with attributes; change date back to current date?
批处理文件可以更改系统日期吗?用属性保存文件;将日期改回当前日期?
Goal to save MYFILE.TXT with the date of 01-01-2010
目标保存日期为 01-01-2010 的 MYFILE.TXT
using Batch commands.
使用批处理命令。
I have tried to set date=01-01-2010
我试图设置 date=01-01-2010
and then save the file, but it didn't work.
然后保存文件,但是没有用。
Is this impossible?
这是不可能的吗?
@echo off
rem to Run this Batch file as administrator
date 01-01-2010
echo %date%
pause
echo Hello World > test.txt
date 09-08-2010
echo %date%
pause
goto :eof
Note: If we didn't "Run as Administrator" It creates an error message of "A required privilege is not held by the client."
注意:如果我们没有“以管理员身份运行”,它会创建一条错误消息“客户端不持有所需的权限”。
回答by Hyman Kelly
Your best bet is to probably grab touch
from GNUWin32and use that to change the timestamps. Doing this by changing the system date is like using a sledgehammer to crack a nut.
最好的办法是touch
从GNUWin32 中获取并使用它来更改时间戳。通过更改系统日期来执行此操作就像使用大锤敲碎坚果一样。
回答by Jim Mischel
Yes, a batch file can do it, but I wouldn't recommend it. To set the date in Windows, you use the DATE command. To set the date to 01/01/2010, you would execute this command at the command prompt:
是的,批处理文件可以做到,但我不推荐它。要在 Windows 中设置日期,请使用 DATE 命令。要将日期设置为 01/01/2010,您需要在命令提示符下执行以下命令:
date 01-01-2010
However, you will need administrator privileges in order for that to work.
但是,您需要管理员权限才能使其工作。
To change the time, the command is TIME.
要更改时间,命令是 TIME。
You can look up both commands by using the HELP facility. i.e.
您可以使用 HELP 工具查找这两个命令。IE
help date
help time
It's also possible to get the current date using a batch file so that you can re-set the date after making your change. That's somewhat more complicated. Here's one way to do it.
还可以使用批处理文件获取当前日期,以便您可以在更改后重新设置日期。那稍微复杂一些。 这是一种方法。
All that said, I agree with Hyman Kelly: get a touch
utility.
综上所述,我同意Hyman凯利的观点:获取touch
实用程序。