批处理文件可以更改系统日期吗?保存带有属性的文件;将日期更改回当前日期?
批处理文件可以更改系统日期吗?保存带有属性的文件;将日期更改回当前日期?
目标是使用批处理命令保存日期为 01-01-2010 的 MYFILE.TXT
。
我尝试设置 date=01-01-2010
然后保存文件,但它不起作用。
这不可能吗?
@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
注意:如果我们没有“以管理员身份运行” 它会创建一条错误消息“客户端不拥有所需的权限”。
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
using Batch commands.
I have tried to set 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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好的选择是从 GNUWin32 获取
touch
并使用来改变时间戳。通过更改系统日期来执行此操作就像使用大锤敲开坚果一样。Your best bet is to probably grab
touch
from GNUWin32 and use that to change the timestamps. Doing this by changing the system date is like using a sledgehammer to crack a nut.是的,批处理文件可以做到这一点,但我不推荐它。要在 Windows 中设置日期,可以使用 DATE 命令。要将日期设置为 01/01/2010,您可以在命令提示符处执行此命令:
但是,您需要管理员权限才能使其工作。
要更改时间,命令是 TIME。
您可以使用帮助工具查找这两个命令。即
也可以使用批处理文件获取当前日期,以便您可以在进行更改后重新设置日期。这有点复杂。 这是一种方法。
综上所述,我同意 Jack Kelly 的观点:获取一个
touch
实用程序。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:
However, you will need administrator privileges in order for that to work.
To change the time, the command is TIME.
You can look up both commands by using the HELP facility. i.e.
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 Jack Kelly: get a
touch
utility.