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?

1 Answer 1

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.

4
  • 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. Commented May 21, 2013 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 Commented May 21, 2013 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. Commented May 22, 2013 at 9: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. Commented May 22, 2013 at 11:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.