LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

The most current file writen on the disk

Hi.

In my project I have to sort the files from a directory using the date. I must find the most current created file( the file with the date closer to the current date of computer).

Is there a method to sort the files from a directory using their names?

I tryed to use the GetFirstFile and GetNextFile functions and than compare the date of files or the names of that files.
Is theire another method to do this?

Thanks.
0 Kudos
Message 1 of 4
(3,266 Views)
Kind of long, but this works:

#include
#include "toolbox.h"
#include
int main()
{
int result;
int seconds;
int minutes;
int hours;
int year;
int day;
int month;
char maxName[500];
char fileName[500];
char *filePath = "C:\\Filepath";
char fullPath[500];
char searchPath[500];
time_t fileTime;
time_t maxTime;
static struct tm currentTime;

strcpy(searchPath, filePath);
strcat(searchPath, "\\*");

result = GetFirstFile (searchPath, 1, 1, 0, 0, 0, 0, maxName);

strcpy(fullPath, filePath);
strcat(fullPath, "\\");
strcat(fullPath, maxName);

GetFileDate (fullPath, &month, &day, &year);
GetFileTime (fullPath, &hours, &minutes, &seconds);

currentTime.tm_year = year-1900;
currentTime.tm_mon = month;
currentTime.tm_mday = day;
currentTime.tm_hour = hours;
currentTime.tm_min = minutes;
currentTime.tm_sec = seconds;

maxTime = mktime (¤tTime);

while(!result)
{
result = GetNextFile (fileName);

strcpy(fullPath, filePath);
strcat(fullPath, "\\");
strcat(fullPath, fileName);

GetFileDate (fullPath, &month, &day, &year);
GetFileTime (fullPath, &hours, &minutes, &seconds);

currentTime.tm_year = year-1900;
currentTime.tm_mon = month;
currentTime.tm_mday = day;
currentTime.tm_hour = hours;
currentTime.tm_min = minutes;
currentTime.tm_sec = seconds;

fileTime = mktime (¤tTime);

if(fileTime>maxTime)
{
maxTime=fileTime;
strcpy(maxName, fileName);
}
}
strcpy(fullPath, filePath);
strcat(fullPath, "\\");
strcat(fullPath, maxName);

printf("Newest File: %s\n", maxName);
printf("Full Path: %s\n", fullPath);
getchar();
return 0;
}

You might be able to find a shorter way.

Regards,
Ryan K.
Message 2 of 4
(3,234 Views)
Eh, stupid HTML cut out my includes, the two includes at the top are utility.h and ansi_c.h
0 Kudos
Message 3 of 4
(3,233 Views)
The simplest method I know to do this is to use the DIR command redirecting output to a file. Dir command has several options that permit you to:
- select which file(s) consider using standard wildcard caracters and/or file attributes
- select the type and direction of sorting (name, dimension, date, extension; ascending or descending order)
- select which date to consider in sorting (creation, modify or access)

For example:
dir *.c /b /o:-d /t:w > file.txt
creates a file with all source files in the directory, sorted by last access starting from the most recent.

You can next read in the file and use collected informations.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 4
(3,220 Views)