Searching the directory for a unique extension?

Posted on 2007-04-04
Medium Priority
Last Modified: 2010-04-01
I have a program that creates a flat file that uses the current date/time to name the file.  This will be the only file in the folder with a ".651" extension.  I have another program that needs to pick-up this file.  Since, I can't use the file's name, is there any code that will allow me to seach the folder for that extension and open the file?

Below is an an example of the address.
Question by:jgoss1
LVL 86

Accepted Solution

jkr earned 300 total points
ID: 18852915
Check out http://msdn2.microsoft.com/en-us/library/aa365200.aspx ("Listing the Files in a Directory") which will show you how to do that. Basically, in

   // Find the first file in the directory.
   hFind = FindFirstFile(DirSpec, &FindFileData);

you need to pass "Y:\\INCOMING\\OSS\\*.651" as 'DirSpec'

Assisted Solution

bijopuli earned 100 total points
ID: 18852986
                    WIN32_FIND_DATA FindFileData;
                    HANDLE hFind;

                    printf ("Target file is %s.\n", argv[1]);

                    hFind = FindFirstFile("Y:\\INCOMING\\OSS\\*.651", &FindFileData);

                    if (hFind == INVALID_HANDLE_VALUE)
                        printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
                    } else
                        printf ("The first file found is %s\n", FindFileData.cFileName);

            LPVOID lpMsgBuf;
            FormatMessage(       FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                    FORMAT_MESSAGE_FROM_SYSTEM |
                                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                                    (LPTSTR) &lpMsgBuf,

            // Display the string.

            // Free the buffer.
            LocalFree( lpMsgBuf );
LVL 86

Expert Comment

ID: 18853441
And how is that different to what I suggested?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 100 total points
ID: 18853576
Using MFC:

 CFileFind objFlagFinder;
 char szFolderNameAbs[MAX_PATH];
 strcpy(szFolderNameAbs,"Y:\\INCOMING\\OSS\\*.651"); //Set the search path
 //Searching flag file
 bool bFlg = objFlagFinder.FindFile(szFolderNameAbs);
    bFlg = objFlagFinder.FindFile(szFolderNameAbs);
    if(!bFlg) //No file found
   //TO DO:
 while (bFlg) //If you want to Search further
 bFlg = objFlagFinder.FindNextFile();
 //TO DO :

Best Regards,
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18853598
What JKR suggested is right.These are the alternate ways of implementation.

Author Comment

ID: 18853676
Thank you for the solution help and the information on where to find it in MSDN.  I got the program to find the file.  Thanks!

DeepuAbrahamK and bijulpoli,
The examples help me understand how jkr's solution worked.  Thanks for the back-up support.

I might actually get this program finished by my deadline...well... maybe.  ;)


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question