Select objects in folder by date range

This topic contains 9 replies, has 4 voices, and was last updated by  Daniel Matei 1 year, 3 months ago.

  • Author
    Posts
  • #32768

    crashrebootrepeat
    Participant

    I am trying to select objects in a folder by date range

    Function Get-DateRange
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    out-null

    $WinForm = New-Object Windows.Forms.Form
    $WinForm.text = "Calendar Control"
    $WinForm.Size = New-Object Drawing.Size(411,190)

    $Calendar = New-Object System.Windows.Forms.MonthCalendar
    $Calendar.MaxSelectionCount = 356
    $Calendar.SetCalendarDimensions([int]2,[int]1)
    $WinForm.Controls.Add($Calendar)

    $WinForm.Add_Shown($WinForm.Activate())
    $WinForm.showdialog() | Out-Null
    $Calendar.SelectionRange
    } #end function Get-DateRange

    # *** Entry point to script ***
    $dates = Get-DateRange

    Get-ChildItem -path "\\server.domain.com\path" | Where-Object { $_.LastWriteTime -like ($dates).start -and $_.LastWriteTime -like ($dates).end}

    When I run this the calender comes up and when I select my date range and then close but the result of the files to not populate.. I do not get any errors it just returns nothing. so I think what I am doing wrong is just in the last bit.

    Can someone help me sort this out?

  • #32771

    Curtis Smith
    Participant

    You problem is here with your Where-Object. -like is not the correct operator to use. You need -gt and -lt

    Get-ChildItem -path "\\server.domain.com\path" | Where-Object { $_.LastWriteTime -gt ($dates).start -and $_.LastWriteTime -lt ($dates).end}
    
  • #32772

    crashrebootrepeat
    Participant

    Thanks Curtis but that still doesn't work. I am still getting no results returned and I have verified there are objects in the folder for if I manually run

    Get-ChildItem -path "\\server.domain.com\path"

    it will return what is there and I can see the LastWriteTime property.

    But when I try to add in the dates I get nothing.

  • #32773

    Lauras Juozulynas
    Participant

    Hi,

    Looking at last bit where you do logical compare makes me wonder.

    You take an object (where-object) and then say that this object has to have LastWriteTime same as date.start and same as date.end. This is because you use "-and". This is possible only if you select 1 day range.

    I think you should say:
    Where-Object { $_.LastWriteTime -ge ($dates).start -and $_.LastWriteTime -le ($dates).end}

    This would translate to LastWriteTime is same or higher than start date AND is lower or equal as date.end.

    Didn't test this 🙂

    And ups, Curtis beat me to it 🙂

  • #32774

    Curtis Smith
    Participant

    This is my run of your code with nothing more that the -gt -lt changes and a path that is local for me. I selected a range of Dec 1 – Dec 9. I also tested both PoSh 2.0 and 4.0. Worked in both versions.

    Function Get-DateRange
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    out-null
    
    $WinForm = New-Object Windows.Forms.Form
    $WinForm.text = "Calendar Control"
    $WinForm.Size = New-Object Drawing.Size(411,190)
    
    $Calendar = New-Object System.Windows.Forms.MonthCalendar
    $Calendar.MaxSelectionCount = 356
    $Calendar.SetCalendarDimensions([int]2,[int]1)
    $WinForm.Controls.Add($Calendar)
    
    $WinForm.Add_Shown($WinForm.Activate())
    $WinForm.showdialog() | Out-Null
    $Calendar.SelectionRange
    } #end function Get-DateRange
    
    # *** Entry point to script ***
    $dates = Get-DateRange
    
    #Get-ChildItem -path "\\server.domain.com\path" | Where-Object { $_.LastWriteTime -like ($dates).start -and $_.LastWriteTime -like ($dates).end}
    Get-ChildItem -path "F:\Temp\Powershell\part" | Where-Object { $_.LastWriteTime -gt ($dates).start -and $_.LastWriteTime -lt ($dates).end}
    

    Results:

        Directory: F:\Temp\Powershell\part
    
    
    Mode                LastWriteTime     Length Name                                                                                                                                                                               
    ----                -------------     ------ ----                                                                                                                                                                               
    -a---         12/2/2015   2:19 AM        174 test.ps1                                                                                                                                                                           
    -a---         12/2/2015  12:25 PM         79 FileP.txt.alsdfjlas                                                                                                                                                                
    -a---         12/2/2015  12:26 PM         88 FileD.txt.g65fg4                                                                                                                                                                   
    -a---         12/2/2015  12:26 PM         78 FileR.txt.af8a9etaf                                                                                                                                                                
    -a---         12/2/2015  12:26 PM         78 FileR.txt - Copy.af8a9etaf                                                                                                                                                         
    -a---         12/2/2015  12:25 PM         79 FileP.txt - Copy.alsdfjlas                                                                                                                                                         
    -a---         12/2/2015  12:26 PM         88 FileD.txt - Copy.g65fg4 
    
  • #32775

    crashrebootrepeat
    Participant

    Here is what I discovered...

    If I just select a range up until today so 1-9 it returns no results.

    However if I add one day into the future (december 1 – 10) then it returns the results

  • #32776

    Curtis Smith
    Participant

    That means your files were last modified today. If you look at the values returned by the MonthCalendar object, you will see that it returns a datetime object at 00:00 hours (12:00 AM) for the date you select. So selecting today's date as an ending date does not inlcude today's files.

    Function Get-DateRange
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    out-null
    
    $WinForm = New-Object Windows.Forms.Form
    $WinForm.text = "Calendar Control"
    $WinForm.Size = New-Object Drawing.Size(411,190)
    
    $Calendar = New-Object System.Windows.Forms.MonthCalendar
    $Calendar.MaxSelectionCount = 356
    $Calendar.SetCalendarDimensions([int]2,[int]1)
    $WinForm.Controls.Add($Calendar)
    
    $WinForm.Add_Shown($WinForm.Activate())
    $WinForm.showdialog() | Out-Null
    $Calendar.SelectionRange
    } #end function Get-DateRange
    
    # *** Entry point to script ***
    $dates = Get-DateRange
    $dates | FT -AutoSize
    

    Results:

    Start                 End                  
    -----                 ---                  
    12/1/2015 12:00:00 AM 12/9/2015 12:00:00 AM
    
  • #32777

    Curtis Smith
    Participant

    One easy work around is to add 1 day to your end value. That also means you should probably use -ge and -lt, rather than -gt and -lt in your Where-Object.

    Function Get-DateRange
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    out-null
    
    $WinForm = New-Object Windows.Forms.Form
    $WinForm.text = "Calendar Control"
    $WinForm.Size = New-Object Drawing.Size(411,190)
    
    $Calendar = New-Object System.Windows.Forms.MonthCalendar
    $Calendar.MaxSelectionCount = 356
    $Calendar.SetCalendarDimensions([int]2,[int]1)
    $WinForm.Controls.Add($Calendar)
    
    $WinForm.Add_Shown($WinForm.Activate())
    $WinForm.showdialog() | Out-Null
    $Calendar.SelectionRange
    } #end function Get-DateRange
    
    # *** Entry point to script ***
    $dates = Get-DateRange
    $dates | FT Start, End -AutoSize
    
    $dates.Start
    $dates.End
    $dates.End.AddDays(1)
    

    Results:

    Start                 End                  
    -----                 ---                  
    12/1/2015 12:00:00 AM 12/9/2015 12:00:00 AM
    
    Tuesday, December 01, 2015 12:00:00 AM
    Wednesday, December 09, 2015 12:00:00 AM
    Thursday, December 10, 2015 12:00:00 AM
    
  • #32796

    crashrebootrepeat
    Participant

    Hi Curtis thanks again for your help, your suggestion ended up doing the trick for me.

  • #53386

    Daniel Matei
    Participant

    Hello,

    Interesting enough but I would like to use these kind of forms and specify the start time as well. Because I want to implement them in maintenance plans and usually those have specific times in them. For example:

    Maintenance window: Saturday 10/09/2016 8:00AM -> 11/09/2016 12PM
    How can one add the time as selection as well?

    Thanks

You must be logged in to reply to this topic.