Click here to Skip to main content
16,006,378 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Tapi new telephony service provider Pin
Kiske18-Feb-02 7:20
Kiske18-Feb-02 7:20 
GeneralRe: Tapi new telephony service provider Pin
Michael P Butler18-Feb-02 9:09
Michael P Butler18-Feb-02 9:09 
GeneralCommon control macro's Pin
Onkie17-Feb-02 19:56
Onkie17-Feb-02 19:56 
GeneralRe: Common control macro's Pin
Michael Dunn18-Feb-02 6:29
sitebuilderMichael Dunn18-Feb-02 6:29 
GeneralFind File Pin
Member 1697717-Feb-02 18:11
Member 1697717-Feb-02 18:11 
GeneralRe: Find File Pin
Paul M Watt17-Feb-02 18:41
mentorPaul M Watt17-Feb-02 18:41 
GeneralRe: Find File Pin
Jon Hulatt17-Feb-02 22:02
Jon Hulatt17-Feb-02 22:02 
GeneralRe: Find File Pin
Jonathan Craig18-Feb-02 4:52
Jonathan Craig18-Feb-02 4:52 
Here is some code that will find find a file given the file name and a starting directory.
CString *FindFile(LPCTSTR szFileFind, LPCTSTR szStartPath)
{
    HANDLE hFind;
    WIN32_FIND_DATA fd;
    CString strPathName;
    CString strFileSpec;
    CString *pstrReturn = NULL;
 
    strPathName = szStartPath;
    //Make sure we have a trailing '\'.
    if(strPathName.Right(1) != "\\")
        strPathName += "\\";
TRACE("Looking in: %s\n", (LPCTSTR)strPathName);
 
    //Add wild cards to the search string.
    strFileSpec = strPathName;
    strFileSpec += "*.*";
            
    //Find files.
    if((hFind = ::FindFirstFile((LPCTSTR)strFileSpec, &fd)) != INVALID_HANDLE_VALUE)
    {
        do
        {
            //Get the name.
            CString strFileName = fd.cFileName;
 
            //Only look for directories and files.
            if((strFileName != ".") && (strFileName != ".."))
            {
                if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    //This is a subdirectory. Search it!
                    strFileName = strPathName + strFileName;
                    pstrReturn = FindFile(szFileFind, strFileName);
                    if(pstrReturn)
                        break; //Don't return here, must close hFile.
                }
                else //Not a directory it's a file.
                {
                    if(strFileName.CompareNoCase(szFileFind) == 0)
                    {
                        //Found it!
                        pstrReturn = new CString(strPathName + strFileName);
TRACE("FOUND!!! - %s\n", (LPCTSTR)*pstrReturn);
                        break; //Don't return here, must close hFile.
                    }
                }
            }
        } while(::FindNextFile(hFind, &fd)); //Get next.
        ::FindClose(hFind);
    }
    return pstrReturn;
}

Note that this is a recurvie function, but since it only pushes 3 pointers on the stack it can call itself a lot of times with out causing a stack overflow.

Note also it is returning a pointer to an allocated CString object. So if it doesn't return NULL (could not find the file), then you must delete the CString object when done with it.

Call it like this:
CString *pstr = FindFile("Win.ini", "C:\\");
if(pstr)
{
    //Do something with pstr.
    // .
    // .
    // .
    // Now I'm finished with pstr.
    delete pstr;
}

I hope this helps.Smile | :)

Jonathan Craig
www.mcw-tech.com
Questionhow long? Pin
redneckCoder17-Feb-02 15:47
redneckCoder17-Feb-02 15:47 
AnswerRe: how long? Pin
Christian Graus17-Feb-02 15:55
protectorChristian Graus17-Feb-02 15:55 
AnswerRe: how long? Pin
Jason Hooper17-Feb-02 16:04
Jason Hooper17-Feb-02 16:04 
AnswerRe: how long? Pin
Nish Nishant17-Feb-02 16:55
sitebuilderNish Nishant17-Feb-02 16:55 
GeneralRe: how long? Pin
Christian Graus17-Feb-02 17:09
protectorChristian Graus17-Feb-02 17:09 
GeneralRe: how long? Pin
Nish Nishant17-Feb-02 17:13
sitebuilderNish Nishant17-Feb-02 17:13 
GeneralRe: how long? Pin
Christian Graus17-Feb-02 17:20
protectorChristian Graus17-Feb-02 17:20 
GeneralRe: how long? Pin
Nish Nishant17-Feb-02 17:45
sitebuilderNish Nishant17-Feb-02 17:45 
GeneralRe: how long? Pin
Christian Graus17-Feb-02 17:51
protectorChristian Graus17-Feb-02 17:51 
AnswerRe: how long? Pin
Braulio Dez17-Feb-02 21:43
Braulio Dez17-Feb-02 21:43 
AnswerRe: how long? Pin
Michael P Butler18-Feb-02 3:42
Michael P Butler18-Feb-02 3:42 
GeneralLPT Port Pin
17-Feb-02 14:47
suss17-Feb-02 14:47 
GeneralRe: LPT Port Pin
Tim Smith17-Feb-02 15:21
Tim Smith17-Feb-02 15:21 
Generalanother one! Pin
17-Feb-02 14:32
suss17-Feb-02 14:32 
GeneralRe: another one! Pin
Derek Waters17-Feb-02 15:11
Derek Waters17-Feb-02 15:11 
GeneralRe: another one! Pin
17-Feb-02 16:14
suss17-Feb-02 16:14 
GeneralRe: another one! Pin
Derek Waters17-Feb-02 16:21
Derek Waters17-Feb-02 16:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.