Change the last accessed time stamp

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Change the last accessed time stamp

Post by MigrationUser »

23 Feb 2009 23:34
strict


i want to create a script that will go through and change the last accessed time stamp to the created timestamp... i found how to do this on a file by file basis:

Code: Select all

$(Get-Item test.txt).lastaccesstime=$(Get-Item test.txt).creationtime
works awesome... but i need this to happen to hundreds of files in many sub directories

any help out there on this?

-mark

----------------------------

#2 25 Feb 2009 19:52
Simon Sheppard


All you need is to pipe the results of a Get-ChildItem (the powershell equivalent of DIR) into a ForEach-Object loop that will then makes the change to each file.

Try this - I've also added a where clause so you can exclude folders and restrict the changes to a specific file extension.

Code: Select all

# Change the last access time of all TXT files in the Source folder (and subfolders)
#  to match the creation time (-force will include any hidden files )

$sourceRoot = 'D:\demo\'

Get-ChildItem -LiteralPath $sourceRoot -Force -Recurse `
    | Where-Object {$_.extension -eq '.txt' -and (-not $_.PSIsContainer)} `
    | ForEach-Object {
        # change last access time
        $_.lastaccesstime=$_.creationtime

        # Echo the changes to screen so you can see what happened
        $newtime=$_.lastaccesstime
        "filename: $_    LastAccessTime: $newtime"
        }
note that powershell won't let you write that last line as
"filename: $_ LastAccessTime: $_.lastaccesstime"
within the quotes powershell will just render everything as text.

----------------------------

#3 25 Feb 2009 20:18
strict


didnt work for me. the script completed without error. however it reported the unchanged last access times... all i modified was the source:

Code: Select all

::begin script::
# Change the last access time of all TXT files in the Source folder (and subfolders)
#  to match the creation time (-force will include any hidden files )

$sourceRoot = 'c:\test\'

Get-ChildItem -LiteralPath $sourceRoot -Force -Recurse `
    | Where-Object {$_.extension -eq '.txt' -and (-not $_.PSIsContainer)} `
    | ForEach-Object {
        # change last access time
        $_.lastaccesstime=$_.creationtime

        # Echo the changes to screen so you can see what happened
        $newtime=$_.lastaccesstime
        "filename: $_    LastAccessTime: $newtime"
        }
::end script::
here was the output:

PS C:\Documents and Settings\mreid\My Documents\tools\scripts> .\changeaccesstime.ps1
filename: fps.txt LastAccessTime: 02/25/2009 11:01:03
filename: mreid_MSIALog.txt LastAccessTime: 02/25/2009 11:00:58
filename: New Notepad++ Document.txt LastAccessTime: 02/25/2009 11:01:07
PS C:\Documents and Settings\mreid\My Documents\tools\scripts>

hey thanks for your help on this!

-mark

----------------------------

#4 25 Feb 2009 20:20
strict


i suppose i should have put that in code:

Code: Select all

# Change the last access time of all TXT files in the Source folder (and subfolders)
#  to match the creation time (-force will include any hidden files )

$sourceRoot = 'c:\test\'

Get-ChildItem -LiteralPath $sourceRoot -Force -Recurse `
    | Where-Object {$_.extension -eq '.txt' -and (-not $_.PSIsContainer)} `
    | ForEach-Object {
        # change last access time
        $_.lastaccesstime=$_.creationtime

        # Echo the changes to screen so you can see what happened
        $newtime=$_.lastaccesstime
        "filename: $_    LastAccessTime: $newtime"
        }
----------------------------

#5 26 Feb 2009 20:33
Simon Sheppard


Thats strange, it worked for me
try getting it to echo out the .creationtime as well as lastaccesstime

I have sometimes seen explorer fail to update without an F5 to refresh it

----------------------------

#6 10 Mar 2009 17:20
strict


it did work. thank you for your time...
Post Reply