Increment access time of file

China☆狼群 提交于 2020-01-05 08:05:51

问题


I would like to increment the access time of a file by a given number of hours, e.g. 12 hours.

I found a way to hack it using Unxutils touch.exe tool and calculate the new date manually:

touch.exe -a -t MMDDhhmmCCYY my_file.txt

However, I would like to automatize this without having to enter the new date manually. So here are my questions:

  1. How can I retrieve the access time of a file in DOS?
  2. How can I increment it?
  3. How can I put that together into a script/batch-file?
  4. Or are there better approaches?

Thanks a lot!


回答1:


Since you've got it tagged for Powershell:

$file = Get-ChildItem testfile.txt
$file.lastwritetime

$file.LastWriteTime = ($file.LastWriteTime).AddHours(12)
$file.LastWriteTime

Tuesday, November 19, 2013 5:27:18 PM
Wednesday, November 20, 2013 5:27:18 AM



回答2:


You can get the last access time in batch using this example:

for /f %%A in ( ' dir /b /TA "c:\test.txt" ' ) do set accesstime=%%~tA
echo %accesstime%

but then to increment it correctly, you are going to have to do something like this: Adding to %TIME% variable in windows cmd script

And account for jumping dates, etc.

In powershell, you can read and write the LastAccessTime of a file like so:

$file = Get-Item "c:\test.txt"
$file.LastAccessTime = ($file.LastAccessTime).AddHours(12)


来源:https://stackoverflow.com/questions/20459102/increment-access-time-of-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!