Retrieve the file name from path based on time stamp

This topic contains 1 reply, has 2 voices, and was last updated by  Poshoholic 4 years, 5 months ago.

  • Author
    Posts
  • #8415

    Robin16
    Participant

    Hi,

    i'm trying to retrieve the file name ( here xml file ) from a given path based on the last updated/created time and place that file name in a script.

    I got this script in the internet....

    
    $path = "C:\scripts"
    
    gci $path –recurse |   ? { -not ($_.Attributes -band [System.IO.FileAttributes]::Directory) } |? { $_.LastWriteTime -gt (Get-Date) } |   foreach { $_.Name }
    
    

    with this script i can get all the file names for one single day, i'm trying to get it based on seconds...

    Please let me know your thoughts...

    Thanks in advance...

  • #8416

    Poshoholic
    Participant

    You need to do some datetime arithmetic so that you can work with timespans. Something like this should do the trick:

    $targetTime = Get-Date -Date '2013-07-13 12:20:11'
    Get-ChildItem -Recurse -File | Where-Object {[Math]::Abs(($_.LastWriteTime - $targetTime).TotalSeconds) -lt 1}
    

    That will find any files that were written within one second of the time specified.

    If you instead want to determine if they were written during that particular second, do this instead:

    $targetTime = Get-Date -Date '2013-07-13 12:20:11'
    Get-ChildItem -Recurse -File | Where-Object {
        $offset = ($_.LastWriteTime - $targetTime).TotalSeconds
        $offset -ge 0 -and $offset -lt 1
    }
    

You must be logged in to reply to this topic.