Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. group by latest file

group by latest file

Scheduled Pinned Locked Moved C#
sysadminhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scottichrosaviakosmos
    wrote on last edited by
    #1

    We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.

    OriginalGriffO S 2 Replies Last reply
    0
    • S scottichrosaviakosmos

      We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      There is no "simple" built-in way to do that. You will have to read all the file names, and group them by name and date. You can then easily sort the times. I'd suggest using a regex to "split" the file name from the date and time, then using Linq to group the values.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S scottichrosaviakosmos

        We have some list of files to copy from a ftp server. Sometimes the file in the source folder have files arrived on same date but @ different time and I would like to copy only latest file for each date. Ex: abc_sky_2014-01-15XYZ03-05-00.Dat abc_sky_2014-01-15XYZ03-08-00.Dat abc_sky_2014-01-16XYZ008-10-00.Dat abc_sky_2014-01-16XYZ008-13-00.Dat abc_sky_2014-01-16XYZ008-16-00.Dat From above files I want to fetch only the latest files for each date : abc_sky_2014-01-15XYZ03-08-00.Dat, abc_sky_2014-01-16XYZ008-16-00.Dat Can someone please help.

        S Offline
        S Offline
        Sebastiaan Lubbers
        wrote on last edited by
        #3

        var directory = new DirectoryInfo(@"F:\Root\");

        var files = from f in directory.EnumerateFiles("*.dat")
        group f by f.Name.Substring(0, 21) into g
        select g.Last();

        foreach (var file in files)
        {
        Console.WriteLine(file.Name);
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups