Solved

Searching the directory for a unique extension?

Posted on 2007-04-04
6
232 Views
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.
Y:\\INCOMING\\OSS\\20070404131022.651
0
Comment
Question by:jgoss1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 75 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'
0
 
LVL 6

Assisted Solution

by:bijopuli
bijopuli earned 25 total points
ID: 18852986
     try
      {
                    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);
                        FindClose(hFind);
                    }

      }
      catch(...)
      {
            LPVOID lpMsgBuf;
            FormatMessage(       FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                    FORMAT_MESSAGE_FROM_SYSTEM |
                                    FORMAT_MESSAGE_IGNORE_INSERTS,
                                    NULL,
                                    GetLastError(),
                                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                                    (LPTSTR) &lpMsgBuf,
                                    0,
                                    NULL
                              );

            // Display the string.
            cout<<((LPCTSTR)lpMsgBuf)<<endl;;
            //TRACE0((LPCTSTR)lpMsgBuf);

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

Expert Comment

by:jkr
ID: 18853441
And how is that different to what I suggested?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 25 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);
 if(!bFlg)
 {
    bFlg = objFlagFinder.FindFile(szFolderNameAbs);
    if(!bFlg) //No file found
       return;      
   //TO DO:
 }
 while (bFlg) //If you want to Search further
 {      
 bFlg = objFlagFinder.FindNextFile();
 //TO DO :
 }
}

Best Regards,
DeepuAbrahamK
0
 
LVL 11

Expert Comment

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

Author Comment

by:jgoss1
ID: 18853676
jkr,
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.  ;)

 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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