#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12

    Using FindFirstFile to search other directories


    I am attempting to search for ipconfig.exe in the system directory. In this case the file is located "C:\windows\system32\ipconfig.exe", I would like it to be more portable.

    Below is a snippet of code from function where I pass the command and the system directory.

    I am able to locate the file if specify the direct path, but that kind of defeats what I am attempting to do.

    The Error Value from GetLastError is:

    6UL (0x000000006)


    If I have left anything out that is needed please let me know.

    Thanks


    Code:
    
    string filename("ipconfig.exe"); 
    string file_path(szWindowDir); 
    
    { 
    
    string full_file_path = file_path + "\\" + filename; 
    
    WIN32_FIND_DATA FindFileData; 
    HANDLE FindCmdApp(FindFirstFile( 
    (char *)full_file_path.c_str(); 
    &FindFileData)); 
    
    FindNextFile(FindCMdApp, &FindFileData) 
    
    
    DWORD error(GetLastError()); 
    
    
    string IpconfigCmd(FindFileData.cfilename); 
    
    FindClose(FindCmdApp); 
    
    
    return error; 
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Since you're specifying an specific filename (not wildcards), there's no need to call FindNextFile(). That's why your code is getting frobbed, because FindFirstFile() will find the file, and then FindNextFile() won't find anything else, since there's no other file that matches your argument. Since you're looking for a specific file, simply call FindFirstFile() and then check the return code to see if the file exists or not.
    Code:
    string filename("ipconfig.exe"); 
    string file_path(szWindowDir); 
    string IpConfigCmd("");
    { 
    
    string full_file_path = file_path + "\\" + filename; 
    
    WIN32_FIND_DATA FindFileData; 
    HANDLE FindCmdApp(FindFirstFile( 
    (char *)full_file_path.c_str(); 
    &FindFileData)); 
    DWORD dwRetval;
    
    if (FindCmdApp != INVALID_HANDLE_VALUE) {
       IpconfigCmd = FindFileData.cfilename; 
       dwRetval = TRUE;
       FindClose(FindCmdApp);
    } else 
       dwRetval = FALSE;
    
    return dwRetval;
    }
    Hope this helps :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    33
    Rep Power
    12
    Yes it does help. Thank you very much!

IMN logo majestic logo threadwatch logo seochat tools logo