File listing/sorting within a directory

N

Nathan Young

Hello.

Perhaps this isn't really a .NET question, but I'm hoping there is an
easy solution within .NET/VC++.

The problem: I have a directory full of different types of data files
and I need to grab the latest copy of data to load into my program.

Example:

ABC123.txt 01-01-2004 1:01a
ABC124.txt 01-02-2004 1:02a
ABC125.txt 01-03-2004 1:03a
ABC126.txt 01-04-2004 1:04a
ABC127.txt 01-04-2004 1:02a
XYZ123.txt 01-01-2004 1:01a
XYZ124.txt 01-02-2004 1:02a
XYZ125.txt 01-03-2004 1:04a
.... and so on.

I want to be able to search this directory, detect the latest ABC*.txt
file, and then open it. The filename is not coded with the date
information - that is the ABC123.txt filename has no significance vs
ABC124.txt. So, I have to retrieve it from the date/time stamp on the
file. In this case, the goal would be to identify the ABC126.txt file
generated on 01-04-2004 at 1:04a as it is the latest copy of the ABC*
files.

In order to accomplish this - I use a MS-DOS 'dir' command called by
system (as follows):

sprintf((char *)&shellCommand,"dir c:\\files\\ABC*.txt /b /o-d >
filelisting.txt");
system(shellCommand);
in = fopen("metarfiles.txt","r");
fscanf(in,"%s",&filename);
fclose(in);

I then open 'filename' and can access the data I want. This works
fine, but is a giant kludge. Nothing like seeing the DOS command
shell pop on top of my program for a brief second to remind me my
programming skills are not that good...

Anyway, there has to be an easier way to do this without leaving
..NET/VC++, right?

If it helps, the directory name is hardcoded, I do not have to
traverse the C:\ directories to find my subdirectory.

Thanks,
-Nathan
 
D

David Lowndes

The problem: I have a directory full of different types of data files
and I need to grab the latest copy of data to load into my program.

In Win32 (I don't know the .net equivalent but there will be
something), you can use FindFirstFile, FindNextFile, FindClose to
enumerate the directory for your files. If you only need the single
newest file keep a record of the newest file as you enumerate the
directory (the timestamps are returned in the structure you pass to
the APIs). If you need a sorted list, you'll have to save each item
and sort them by timestamp yourself.

Dave
 
N

Nathan Young

In Win32 (I don't know the .net equivalent but there will be
something), you can use FindFirstFile, FindNextFile, FindClose to
enumerate the directory for your files. If you only need the single
newest file keep a record of the newest file as you enumerate the
directory (the timestamps are returned in the structure you pass to
the APIs). If you need a sorted list, you'll have to save each item
and sort them by timestamp yourself.

Thanks, exactly what I needed.
 
A

Austin Ehlers

Hello.

Perhaps this isn't really a .NET question, but I'm hoping there is an
easy solution within .NET/VC++.

The problem: I have a directory full of different types of data files
and I need to grab the latest copy of data to load into my program.

Example:

ABC123.txt 01-01-2004 1:01a
ABC124.txt 01-02-2004 1:02a
ABC125.txt 01-03-2004 1:03a
ABC126.txt 01-04-2004 1:04a
ABC127.txt 01-04-2004 1:02a
XYZ123.txt 01-01-2004 1:01a
XYZ124.txt 01-02-2004 1:02a
XYZ125.txt 01-03-2004 1:04a
... and so on.
<snip>

How about:

using namespace System;
using namespace System::IO;
FileInfo* GetLatestFile(String* dir, String* pattern) //"c:\\",
"abc*"
{
DirectoryInfo* di=new DirectoryInfo(dir);
FileInfo* files[]=di->GetFiles(pattern);
FileInfo* latest=files[0];
for(int x=1;x<files->Length;x++)
{
if ((files[x]->LastWriteTimeUtc) >
(latest->LastWriteTimeUtc))
{
latest=files[x];
}
}
return latest;
}
Call one of the Open methods to get a FileStream so you can read it.

Austin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top