Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2004

Tip: Looking for answers? Try searching our database.

File listing/sorting within a directory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Young - 06 Oct 2004 01:35 GMT
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
David Lowndes - 06 Oct 2004 07:56 GMT
>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
Signature

MVP VC++ FAQ: http://www.mvps.org/vcfaq

Nathan Young - 06 Oct 2004 15:25 GMT
>>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.
[quoted text clipped - 6 lines]
>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.
Austin Ehlers - 07 Oct 2004 22:55 GMT
>Hello.
>
[quoted text clipped - 15 lines]
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.