Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Obtener fichero mas nuevo en carpeta. Ayudita (https://www.clubdelphi.com/foros/showthread.php?t=86933)

joeyjr 24-10-2014 09:46:37

Obtener fichero mas nuevo en carpeta. Ayudita
 
Hola, necesito saber cual es el fichero más nuevo creado en una carpeta determinada para poder abrirlo y tratarlo.

Lo estoy intentnado con FileListBox pero no veo la posiblidad de ordenar por fecha de creación.

¿alguien me puede echar una mano?

Muchas gracias amigos.

duilioisola 24-10-2014 12:29:12

Escribes en el Foro de C, por lo que no te puedo dar una solución completa, pero supongo que habrá algo parecido a como se hace en Delphi.

Mediante FindFirst..FindNext..FindClose puedes recorrer los archivos de una carpeta.
Esos archivos quedan en una estructura:
Código Delphi [-]
TSearchRec = record
    Time: Integer;
    Size: Integer;
    Attr: Integer;
    Name: TFileName;
    ExcludeAttr: Integer;
    FindHandle: THandle;
    FindData: TWin32FindData;
end;

Esa estructura tiene el campo Time, que te servirá para saber cual es el fichero más nuevo.
Sería algo parecido a esto:

Código Delphi [-]
var
  sr: TSearchRec;
  FileAttrs: Integer;
  TimeMenor integer;
  ArchivoMasNuevo : string;
begin
...
    FileAttrs := faAnyFile;
    if FindFirst(EditCarpeta.Text, FileAttrs, sr) = 0 then
    begin
      repeat
        if (sr.Time < TimeMenor) then
           ArchivoMasNuevo  = sr.Name;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;
end;

ecfisa 24-10-2014 13:20:53

Hola joeyjr, bienvenido a Club Delphi :)

Tál como te comenta duilioisola, se puede hacer de modo muy similar en C++ Builder.
Código PHP:

AnsiString GetNewestFile(AnsiString folder)
{
  
TSearchRec sr;
  
TDateTime fileAgemaxAge 0;
  
AnsiString newest "";

  
folder IncludeTrailingPathDelimiter(folder);
  if (
FindFirst(folder "*.*"faAnyFilesr) == 0) {
    do {
      if (
sr.Name != "." && sr.Name != ".." && sr.Attr faArchive) {
        
fileAge FileDateToDateTime(FileAge(folder sr.Name));
        if (
fileAge maxAge) {
          
maxAge fileAge;
          
newest folder sr.Name;
        }
      }
    } while (
FindNext(sr) == 0);
    
FindClose(sr);
  }
  return 
newest;


Saludos :)

joeyjr 28-10-2014 11:46:27

gracias maquina!!


La franja horaria es GMT +2. Ahora son las 09:17:50.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi