0

I have approximately 100,000 photographs taken each minute of every day for several months.

I would like to be able to delete the ones taken after say 8PM and before 6AM with either a batch or powershell script.

Is this possible?

  • Everything is possible, the question is: what have you tried? This is not a place where you ask something without showing that you have done any research or attempts to solve the given problem. If you need actual solution, hire an programmer. You can modify this solution to fit your needs: http://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell – Erti-Chris Eelmaa Jul 16 '14 at 14:31
  • Thanks for your help, but I'm not sure that is entirely helpful. I have various scripts I have already created/adapted with operations on creation/modified date/time. If I just wanted to delete files between 8PM and 6AM on one day, that would be fairly trivial, however, I need to delete files between 8PM and 6AM _every_ day. I could not figure out how to select files that were created between two times, regardless of the date they were created? I simply did not know where to start. Hence asking the question here. – Mike Hughes Jul 16 '14 at 14:40
  • The comment above is referring to the only question in your post 'Is this Possible?' Typically questions here need to show that you've tried something to solve your own problem, or code that isn't performing what you expect. Also, if you have scripts that already delete on 1 day, why not just schedule them to run once per day? – Mike Gardner Jul 16 '14 at 14:58

2 Answers2

0

The file property CreationTime is what i am assuming you are looking for.

PS M:\Scripts> (Get-Item .\Clean-UninstallKeysLog.vbs).CreationTime | fl


Date        : 6/24/2014 12:00:00 AM
Day         : 24
DayOfWeek   : Tuesday
DayOfYear   : 175
Hour        : 15
Kind        : Local
Millisecond : 928
Minute      : 18
Month       : 6
Second      : 47
Ticks       : 635392199279286738
TimeOfDay   : 15:18:47.9286738
Year        : 2014
DateTime    : Tuesday, June 24, 2014 3:18:47 PM

Using that you can extract the hour property and edit your preexisting script looking for hours < 6 and > 20. In the example above i get 15 for the hour

((Get-Item .\Clean-UninstallKeysLog.vbs).CreationTime).Hour
15
Matt
  • 45,022
  • 8
  • 78
  • 119
0

I went into my downloads folder and did this in PowerShell:

> $all_files = Get-ChildItem
> $all_files.Count
471

> $some_files = (Get-ChildItem) | Where-Object {$_.CreationTime.TimeOfDay.Hours -gt 18 -and $_.CreationTime.TimeOfDay.Hours -lt 20}
> $some_files.Count
21

You could also do this to see the available properties under CreationTime:

PS D:\Downloads> (gci .\somefile.zip).CreationTime | Get-Member -MemberType Property


TypeName: System.DateTime

Name        MemberType Definition
----        ---------- ----------
Date        Property   datetime Date {get;}
Day         Property   int Day {get;}
DayOfWeek   Property   System.DayOfWeek DayOfWeek {get;}
DayOfYear   Property   int DayOfYear {get;}
Hour        Property   int Hour {get;}
Kind        Property   System.DateTimeKind Kind {get;}
Millisecond Property   int Millisecond {get;}
Minute      Property   int Minute {get;}
Month       Property   int Month {get;}
Second      Property   int Second {get;}
Ticks       Property   long Ticks {get;}
TimeOfDay   Property   timespan TimeOfDay {get;}
Year        Property   int Year {get;}
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • 1
    Just what I was looking for, this is what I ended up with ` $root = "C:\test" `(Get-ChildItem -Path $root) | Where-Object {$_.lastWriteTime.TimeOfDay.Hours -gt 20 -or $_.LastWriteTime.TimeOfDay.Hours -lt 06} | Remove-Item` – Mike Hughes Jul 16 '14 at 15:36