How can I search for a file?
hello!
I have 4 strings which represent 4 file names...
I want to search my computer for these strings, so if one of them is
found, the search function will break and the full path of the file will be
returned...
how can I do that in C++?
thanks!!
:wave:
Re: How can I search for a file?
Use the API FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData)
path etc stored in lpFindFileData struct
Re: How can I search for a file?
hi bilm_ks!
I'm pretty new to c++...
can you please post a sample code for the api and more information about
the use of it?
including how do I register it in the code?
thanks!
:wave:
Re: How can I search for a file?
MSDN has an example which shows the usage of FindFirstFile as well as FindNextFile here: listing the files in a directory
Hope that helps!
Edit: This task might be more difficult than I first thought, if I now understand your question right. You'll need to use the Find*File to recursively search each directory on the computer until you've found the file. The psuedocode would be something like this:
Code:
// checks if the file is present in the directory passed
// or any subdirectories.
// returns: the full path of the file, or FAILED.
string find_file(string file_name, string directory)
{
file_list = list_directory(directory);
if (file_list contains file_name)
return directory + file_name;
else
{
for each (subdirectory sub in directory)
{
string result = find_file(file_name, sub);
if (result != FAILED)
return result;
}
return FAILED;
}
}
Re: How can I search for a file?
There's also a way to programmatically access Explorer's search function, but I forgot how.
Re: How can I search for a file?
found this,
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;
}
Re: How can I search for a file?
Code:
#include "stdafx.h"
#include <windows.h>
#include <string>
using namespace std;
BOOL g_bFileFound;
string g_sPath;
void SearchFile(string sFile, string sPath)
{
string sFullName;
WIN32_FIND_DATA findData;
HANDLE hFile;
sFullName.assign(sPath);
sFullName.append(sFile);
hFile = FindFirstFile(sFullName.data(), &findData);
if (hFile != INVALID_HANDLE_VALUE) // file found
{
FindClose(hFile); // close handle
g_bFileFound = TRUE;
g_sPath = sPath;
return;
}
// file not found - must search in subdirs
string sDirName;
sDirName.assign(sPath);
sDirName.append("*");
hFile = FindFirstFile(sDirName.data(), &findData);
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // is dir
{
if (findData.cFileName[0] == L'.') // dont search "." and ".."
continue;
string sNewPath;
sNewPath.assign(sPath);
sNewPath.append(findData.cFileName);
sNewPath.append("\\");
SearchFile(sFile, sNewPath); // recursion
}
if (g_bFileFound)
{
FindClose(hFile);
return;
}
} while (FindNextFile(hFile, &findData));
}
int _tmain(int argc, _TCHAR* argv[])
{
string sFile("a file.txt");
string sPath("C:\\");
g_bFileFound = FALSE;
SearchFile(sFile, sPath);
if (g_bFileFound)
printf("found in %s\n", g_sPath.data());
return 0;
}