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
  • 219
  • 3
  • 7

1 Answers1

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.

atwright147
  • 219
  • 3
  • 7
Austin T French
  • 10,505
  • 27
  • 42
  • 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 May 21 '13 at 16:30
  • 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 May 21 '13 at 17:00
  • Brilliant! It works. I had to remove all of the new lines to run it from the command line but it worked. – atwright147 May 22 '13 at 09:01
  • 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 May 22 '13 at 11:30