Thread: Given tha path, find an exe(given the size) into it

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Given tha path, find an exe(given the size) into it

    Hi to all

    I've done a little program that looks for a specific file on the disk.
    Once I've found that file (file_to_find.txt) I've done half work, beacuse now I want to look for an *.exe (I dont know the name beacuse it's quite allways renamed by the user, I only know 3 sizes of it that means 3 version of the same exe) that is in the same folder where I found file_to_find.txt (that the user can't rename). I want to look for that *.exe and I want know it's NAME.EXTENSION (or only the name.....I know it's an exe)

    The code for the search in C is this one :

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    
    //Looking for a File by BianConiglio // 
    
    int look4aFile (char* strStartPath)  
    { 
       HANDLE hFileSearch; 
       WIN32_FIND_DATA hFileSearchData; 
    
       int lastError = 0; 
       char* currentSearchPattern = (char*) malloc( MAX_PATH ); 
       char* reservedBuffer; 
    
       if (currentSearchPattern) 
       { 
          if (!strStartPath) 
             strStartPath = "C:\\"; 
    
          strcpy (currentSearchPattern, strStartPath); 
          strcat (currentSearchPattern, "*"); 
    
          if ( (hFileSearch = FindFirstFile(currentSearchPattern, &hFileSearchData)) != INVALID_HANDLE_VALUE ) 
          { 
             do { 
                if ( strcmp(hFileSearchData.cFileName, ".") && strcmp(hFileSearchData.cFileName, "..") ) 
                { 
                   if ( hFileSearchData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { 
    
                      reservedBuffer = (char*) malloc( MAX_PATH+1 ); 
                      if ( reservedBuffer ) 
                      { 
                         strcpy (reservedBuffer, strStartPath); 
                         strcat (reservedBuffer, hFileSearchData.cFileName); 
                         strcat (reservedBuffer, "\\"); 
    
                         lastError = look4aFile (reservedBuffer); 
    
                         free (reservedBuffer); 
    
                      } else 
                         lastError = -1; 
            
                   } else { 
                                          
                      if ( !stricmp(hFileSearchData.cFileName, "file_to_find.txt" )) 
                      
                      printf ("I've found a file \"%s\" in the folder \"%s\"\n", hFileSearchData.cFileName, strStartPath); 
                        
                     } 
                } 
                  
             } 
        
              while ( FindNextFile(hFileSearch, &hFileSearchData) ); 
          } else 
             lastError = 1; 
    
          free (currentSearchPattern); 
          FindClose (hFileSearch); 
    
       } else 
          lastError = -1; 
    
       return lastError; 
    } 
    
    
    int main() 
    
    { 
    
      look4aFile (NULL); 
      
      system("PAUSE");    
      return 0; 
    }

    In that "for" I have to add the code for searching the exe (the exe size is standard, i have 3 sizes that i can define in the program)

    Code:
    if ( !stricmp(hFileSearchData.cFileName, "file_to_find.txt" )) 
                      
                      printf ("I've found a file \"%s\" in the folder \"%s\"\n", hFileSearchData.cFileName, strStartPath);
    strStartPath is the path found, I have to search in it an exe with one fo the 3 (defined) size... some help ?

    Thanx to everyone
    Last edited by BianConiglio; 04-01-2004 at 11:11 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    int find_exe(char directory[])
    /* directory is path in which to look for file */
    /* example:  \\dir\\subdir\\                   */
    {
       DWORD file_size[3] = {20000, 30000, 40000 };
       HANDLE hFileSearch; 
       WIN32_FIND_DATA hFileSearchData; 
       int lastError = 0; 
    
       char* currentSearchPattern = malloc( MAX_PATH ); 
       if (currentSearchPattern) 
       { 
          strcpy(currentSearchPattern,directory);
          strcat(currentSearchPattern,"*.EXE");
    
          if ( (hFileSearch = FindFirstFile(currentSearchPattern, &hFileSearchData)) != INVALID_HANDLE_VALUE ) 
          { 
             do { 
                if ( strcmp(hFileSearchData.cFileName, ".") && strcmp(hFileSearchData.cFileName, "..") ) 
                { 
                   if (hFileSearchData.nFileSizeLow == file_size[0] ||
                      hFileSearchData.nFileSizeLow == file_size[1] ||
                      hFileSearchData.nFileSizeLow == file_size[2])
                      printf("EXE found: %s  size: %ld\n",hFileSearchData.cFileName,
                         hFileSearchData.nFileSizeLow);
                } 
                  
             } while ( FindNextFile(hFileSearch, &hFileSearchData) ); 
             FindClose (hFileSearch); 
          } else 
             lastError = 1; 
    
          free (currentSearchPattern); 
    
       } else 
          lastError = -1; 
    }
    Last edited by swoopy; 04-01-2004 at 06:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  2. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM
  5. How to find file size?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 08-30-2001, 02:34 PM