问题
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:
- How can I retrieve the access time of a file in DOS?
- How can I increment it?
- How can I put that together into a script/batch-file?
- 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