#1 23 Feb 2009 23:34

strict
Member
Registered: 15 Jan 2009
Posts: 15

need help

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:

$(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

Offline

#2 25 Feb 2009 19:52

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013
Website

Re: need help

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.

# 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.

Offline

#3 25 Feb 2009 20:18

strict
Member
Registered: 15 Jan 2009
Posts: 15

Re: need help

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

::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

Offline

#4 25 Feb 2009 20:20

strict
Member
Registered: 15 Jan 2009
Posts: 15

Re: need help

i suppose i should have put that in code:

# 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"
        }

Offline

#5 26 Feb 2009 20:33

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,013
Website

Re: need help

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

Offline

#6 10 Mar 2009 17:20

strict
Member
Registered: 15 Jan 2009
Posts: 15

Re: need help

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

Offline

Board footer

Powered by FluxBB