1

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!

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work for more than one file, since you are using the Get-ChildItem it is just matter of changing the filter to save more than one file in the $file variable. This is how it may be done for more files. $file = Get-ChildItem -path \temp -filter *.log; $file | foreach { $_.LastWriteTime = ($_.LastWriteTime).AddHours(12); $_.LastWriteTime }
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.