locked
find most current file from today and yesterday RRS feed

  • Question

  •  

     How can I find the most current file from today, and the most current file from yesterday. The process that creates
    files to this folder could run multiple times in a day. SO you could have 5 files from yesterday and today, but I need the
    last one created yesterday, and the same for current day.

     This seems to get current day, but not sure how to pull yesterday's most current file.

    sample file names in folder. The files have date and time in the filename.
    kpi_cnt_06_20_2019_10_42_08.csv  **
    kpi_cnt_06_20_2019_10_14_08.csv
    kpi_cnt_06_19_2019_09_42_08.csv
    kpi_cnt_06_19_2019_20_14_08.csv **

    The ones(**) would be what I want latest and prevday.

    $dir = "C:\kpi_counts"
    $filter="k*.csv"
    $latest = Get-ChildItem -Path $dir -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $latest.name

     Thanks.

    Thursday, June 20, 2019 4:38 PM

All replies

  • You have to convert the file name to a date object using a RegEx.  after that you can group by day and extract the newest one from each group.

    Search for articles on how to use RegEx to extract times and convert. 

    help group-object -online


    \_(ツ)_/

    Thursday, June 20, 2019 5:09 PM
  • Here is the hard part.

    $filename = 'kpi_cnt_06_20_2019_10_14_08.csv'
    if($filename -match '(\d+_\d+_\d+_\d+_\d+_\d+)\.csv$'){
        $m, $d, $y, $hh, $mm, $ss = $matches[1] -split '_'
        [datetime]::New($y, $m, $d, $hh, $mm, $ss)
    }else{
        Write-Host 'no date found'
    }


    \_(ツ)_/


    Thursday, June 20, 2019 5:17 PM
  • Current Day

    Get-ChildItem  | where-object{$_.LastAccessTime.ToShortDateString() -eq (Get-Date).ToShortDateString() } |Sort-Object LastAccessTime -Descending | Select-Object -First 1

    Previous Day

    Get-ChildItem  | where-object{$_.LastAccessTime.ToShortDateString() -eq (Get-Date).adddays(-1).ToShortDateString() } |Sort-Object LastAccessTime -Descending | Select-Object -First 1

    Thursday, June 20, 2019 7:10 PM
  •   

     I did this and it pulled the correct file.

    $dir = 'c:\kpi_counts'
      $filter="k*.csv"
      $yesterday = Get-Childitem $dir -Filter $filter -recurse | sort LastWriteTime -Descending | Where-Object { $_.LastWriteTime.Date -eq (Get-Date).Date.Adddays(-1) } | Select -First 1
     Thanks.
    Thursday, June 20, 2019 7:11 PM
  • warning;  the LastWriteTime may not be synced to the file names date.  If it is then use it.  Maybe the "CreationTime" is the correct date.

    None of these issues are discussed and the OP seems to want to use the file names date.  Make those choices then pick a solution.


    \_(ツ)_/

    Thursday, June 20, 2019 7:17 PM
  •  (Get-Date).Date.Adddays(-1)

    will give date and time so your -eq will only be true for files created at midnight, my example compares date without time.

    Thursday, June 20, 2019 7:20 PM
  •  (Get-Date).Date.Adddays(-1)

    will give date and time so your -eq will only be true for files created at midnight, my example compares date without time.

    To get a specific day you must match only the normalized date.

    [datetime]::Today.AddDays(-1) -eq $_.CreationTime.Date

    This will match for the exact day for all files created on that day at any time.  You can also use "Date" property to group files by day.

    $files |  | Group-Object -Property {$_.CreationTIme.Date.ToString('yyyy-MM-dd')} | sort name


    \_(ツ)_/

    Thursday, June 20, 2019 7:29 PM
  • This gets all file by date into groups that can then be filtered for the first of last member of the group.

    Get-ChildItem *.Csv -File | 
        Where-Object{
            $_.CreationTime.Date -gt [datetime]::Today.AddDays(-1)
        } | 
        Group-Object -Property {$_.CreationTIme.Date.ToString('yyyyMMdd')}

    There will be only two groups.  One group for yesterday and one for today.


    \_(ツ)_/


    Thursday, June 20, 2019 7:34 PM
  • This extracts the last file from each group:

    Get-ChildItem *.Csv -File | 
        Where-Object{
            $_.CreationTime.Date -gt [datetime]::Today.AddDays(-1)
        } | 
        Group-Object -Property {$_.CreationTIme.Date.ToString('yyyyMMdd')} | 
            ForEach-Object{
                $_.Group | Sort-Object creationtime | Select-Object -last 1
            }

    The outcome will be two files...one for each day.

    This design pattern can be very easily updated to handle folders and other differentiating items.


    \_(ツ)_/



    Thursday, June 20, 2019 7:38 PM
  •  When I used above solution it produced a single file for today?

      "The outcome will be two files...one for each day."

    Thursday, June 20, 2019 9:46 PM
  •  When I used above solution it produced a single file for today?

      "The outcome will be two files...one for each day."

    When you used what code?  You have to be clear when posting in a forum.

    Are you sure there are two files that match that criteria?  Have you looked at the files?  Have you looked at the dates.  Perhaps the files are created before midnight but contain the following days date name>  You have to have an understanding of how these files are created.

    You can also try to use "LastWriteTime" to be sure the file was last used on the date in question.

    You have to do the thinking on this.  We do not know your system.  We cannot guess.


    \_(ツ)_/

    Thursday, June 20, 2019 9:59 PM
  •  

     The file names represent the creation times of the files when created on the share. 

     I thought this could was going to produce a today's file and yesterday's file.

    Get-ChildItem *.Csv -File | 
        Where-Object{
            $_.CreationTime.Date -gt [datetime]::Today.AddDays(-1)
        } | 
        Group-Object -Property {$_.CreationTIme.Date.ToString('yyyyMMdd')} | 
            ForEach-Object{
                $_.Group | Sort-Object creationtime | Select-Object -last 1
            }

    The outcome will be two files...one for each day. 

      Thanks.

    Friday, June 21, 2019 2:01 PM
  • That is what the code does.  I suspect the file name is not the creation time of the file.  We cannot see your files so we cannot help you understand this.  You have to inspect your files and decide what is wrong.


    \_(ツ)_/


    • Edited by jrv Friday, June 21, 2019 2:05 PM
    Friday, June 21, 2019 2:04 PM