Searching for a file using FindFirstFile() ?

This is a discussion on Searching for a file using FindFirstFile() ? within the Windows Programming forums, part of the Platform Specific Boards category; I know salem has discussed this before, but I am using LCC-Win32 at the moment and need to find out ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    Searching for a file using FindFirstFile() ?

    I know salem has discussed this before, but I am using LCC-Win32 at the moment and need to find out how to search the entire hard drive for sol.exe (solitare).

    Here's a snippit of code that works with my compiler:
    Code:
    int findSolitaire(void)
    {
     WIN32_FIND_DATA solitaire;
     FindFirstFile("sol.exe",&solitaire);
     MessageBox(NULL,solitaire.cFileName,solitaire.cFileName,0);
     return 0;
    }
    This only works if solitaire is in THE SAME DIRECTORY as the running program. Please post an example of one that searches the entire hard drive.

    PS: chdir() doesn't work in my compiler (LCC is C-Only, so C++ functions don't work)
    Last edited by Brian; 01-27-2002 at 12:46 PM.

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb I think maybe...

    I was wondering the same question before, but you might try this:


    WIN32_FIND_DATA solitaire;

    FindFirstFile ("C:\*.*\sol.exe", &solitaire);

    I'm not sure it works, but you can try it.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    yeah but if that works I still wont get the full path. Oh - and it doesn't work.
    Last edited by Brian; 01-27-2002 at 12:47 PM.

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Ohhhhhh...

    I didn't see that part. Well, you can try GetFullPathName(), but I don't know how to use it. Look for it at MSDN. It shows the parameters for it.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Helpful, but you still need to have found the file before GetFullPathName is of any use.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    FindFirstFile() returns directories aswell as files. The file attribute (dwFileAttributes) returned in the WIN32_FIND_DATA struct will contain the FILE_ATTRIBUTE_DIRECTORY flag. When you encounter a directory you could recursively call your file searching function with the directory that you've found appended to your existing path.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Please give an example, I'm dumb .

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    I've not done alot of testing (and it is a quick hack), but this appears to work -

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    void find(char* path,char* file)
    {
    	static int found =0;
    	HANDLE fh;
    	WIN32_FIND_DATA wfd;
    	int i=0;
    	int j=0;
    	fh=FindFirstFile(path,&wfd);
    	if(fh)
    	{
    		if(strcmp(wfd.cFileName,file)==0)
    		{
    			path[strlen(path)-3]='\0';
    			strcat(path,file);
    			FindClose(fh);
    			return;
    		}
    		else
    		{
    			while(FindNextFile(fh,&wfd) && found ==0)
    			{				
    				if(strcmp(wfd.cFileName,file)==0)
    				{
    					path[strlen(path)-3]='\0';
    					strcat(path,file);
    					FindClose(fh);
    					found =1;
    					return;
    				}
    				if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
    					strcmp(wfd.cFileName,"..")!=0 && strcmp(wfd.cFileName,".")!=0)
    				{
    					path[strlen(path)-3]='\0';
    					strcat(path,wfd.cFileName);
    					strcat(path,"\\*.*");
    					find(path,file);
    				}
    			}
    			
    			if(found==0)
    				{
    				for(i=strlen(path)-1;i>0;i--)
    				{
    					if(j==1 && path[i]=='\\')
    					{
    						path[i]='\0';
    						strcat(path,"\\*.*");
    						break;
    					}
    					if(path[i]=='\\')
    						j=1;
    				}
    			}
    		}
    		FindClose(fh);
    	}
    		
    
    
    }
    
    int main()
    {
    	TCHAR path[512] = "C:\\*.*";
    	find(path,"notepad.exe");
    	printf("%s\n",path);
      
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21