Searching program files problem.

Hello,
this is my code for logging all mp3 file and all exe files in a computer. It works like a charm EXCEPT, when the directory to search is set to "c:\\" it doesn't search Program Files, Windows, Users or any system related folders. I need it to and I'm not sure if it is a permission issue or if its a problem in my code. Although, if i set the directory directly to "c:\\Program Files\\" it will search through it fine. Help very much apreciated.
INFO:
Running: Windows Vista 32
Compiler: Dev-C++
API: Windows
Errors: NONE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string>
#include <vector>
#include <iostream>
#include <fstream>

#include <windows.h>
#include <conio.h>

#include "scanclass.h" // class's header file

using namespace std;




bool ScanClass::searchDirectory(ofstream &exeFiles, ofstream &mp3Files, string basePath, string pathWithFile)

{
    string tempStr = "";
    HANDLE          hFile;                   // Handle to file
    WIN32_FIND_DATA fileInfo;         // File information
    

    tempStr = basePath + "*";
    hFile = FindFirstFile(tempStr.c_str(), &fileInfo);
    if (GetLastError() != 0)
    {
        //cout << "Error " << GetLastError() <<  " with FindFirstFile(" << tempStr << ", " << "fileInfo);" << endl;
    }
    
    while (FindNextFile(hFile, &fileInfo) != 0)//While there are still files in the folder
    {
        if (fileInfo.cFileName[0] != '.')
        {
            //system("PAUSE");
            if (fileInfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)//If a it is a folder
            {
                tempStr = basePath + fileInfo.cFileName + "\\";
                
                //cout << "Going into sub directory with '" << basePath << "'." << endl;
                
                searchDirectory(exeFiles, mp3Files, tempStr, pathWithFile);
            }
        
            else//A file is found
            {
                pathWithFile = basePath + fileInfo.cFileName;
                tempStr = fileInfo.cFileName;
                tempStr = tempStr.substr(tempStr.rfind(".") + 1);
                
                //cout << "Checking if '" << fileInfo.cFileName << "' has an extension exe" << endl;

                if (tempStr == "exe" || tempStr == "EXE")
                {
                    cout << "About to add: " << pathWithFile << endl;
                    exeFiles << pathWithFile << endl;
                }
                    
                else if (tempStr == "mp3" || tempStr == "MP3")
                {
                    cout << "About to add: " << pathWithFile << endl;
                    mp3Files << pathWithFile << endl;
                }
            }
        }
    }
    FindClose(hFile);

    return true;
}

int ScanClass::startSearch()
{
    ofstream exeFiles("exeFiles.txt");
    ofstream mp3Files("mp3Files.txt");
    cout << searchDirectory(exeFiles, mp3Files, "c:\\", "") << endl;
    return 1;
}

Last edited on
FindNextFile works differently for a root directory. You should play around with it to see how to call it.
I am not quite sure what you mean. Are there different ways to call FindNextFile? I don't see any info in the MSDN reference. A long time ago I saw something like adding SYSTEM|SUB_DIRECTORY to the call but I cant seem to find anything about it. Please be a bit more specific.
Thanks for the reply.
-Favor

PS. How is my code? I have never used recursive functions before.
Last edited on
See any of the MS samples from SDK for a recursive directory search. (WinBase or WMSDK samples)
Topic archived. No new replies allowed.