Retrieve the file name from path based on time stamp

Welcome Forums General PowerShell Q&A Retrieve the file name from path based on time stamp

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
5 years, 4 months ago.

  • Author
    Posts
  • #8415

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    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
    }
    

The topic ‘Retrieve the file name from path based on time stamp’ is closed to new replies.