PowerShell touch all files newer than

2

I have found the following question on ServerFault:

Windows recursive touch command

Which partially answers my question with this answer:

Windows recursive touch command

However, I would like to touch all files (in root and sub folders (recursively)) that are newer than 31st January 2013 (31/01/13). How would I go about doing this?

I have PowerShell 2 available.

UPDATE:

I have found that this scriptlet gets all of the files that I am after:

Get-ChildItem C:\path\to\files -recurse | Where-Object { $_.LastWriteTime -ge [DateTime] "1/31/2013 9:00AM" }

But I am not sure how to combine it with the "touch" command:

(ls file).LastWriteTime = DateTime.now

The following seems logical but I cannot test it as backing up my files will screw up my files' modified date/times:

(Get-ChildItem C:\path\to\files -recurse | Where-Object { $_.LastWriteTime -ge [DateTime] "1/31/2013 9:00AM" }).LastWriteTime = DateTime.now

So, will this work?

atwright147

Posted 2013-05-21T15:53:34.147

Reputation: 219

Answers

3

Powershell to use Unix touch seems silly to me.

Instead, just use native Powershell cmdlets.

This article covers it:

Essentially:

Get-ChildItem -Path $youFolder -Recurse | Foreach-Object {
    if ($_.LastWriteTime -ge [DateTime] "1/31/2013 9:00AM")
    { 
        $_.LastWriteTime = Get-Date
    }
}

Should do the trick.

Austin T French

Posted 2013-05-21T15:53:34.147

Reputation: 9 766

Sorry but I think you have missed the point of my question. I want to filter for files that are newer than a given date, then change their timestamp to now. I have code for both but am not sure how to put them together. Thanks for the help so far though. – atwright147 – 2013-05-21T16:30:09.377

Sorry, forgot the checking. Added now. This does essentially what you ask but uses an If statement instead of the where-object – Austin T French – 2013-05-21T17:00:29.840

Brilliant! It works. I had to remove all of the new lines to run it from the command line but it worked. – atwright147 – 2013-05-22T09:01:54.357

Yes, I had it in a script form so to run it from the console it would have had to be moved to a one-line form. – Austin T French – 2013-05-22T11:30:50.887