|
-
Jan 7th, 2006, 06:43 AM
#1
Thread Starter
Fanatic Member
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!!
-
Jan 7th, 2006, 09:40 AM
#2
Lively Member
Re: How can I search for a file?
Use the API FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData)
path etc stored in lpFindFileData struct
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Jan 7th, 2006, 12:09 PM
#3
Thread Starter
Fanatic Member
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!
-
Jan 7th, 2006, 01:16 PM
#4
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;
}
}
Last edited by sunburnt; Jan 7th, 2006 at 01:25 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jan 8th, 2006, 05:44 AM
#5
Re: How can I search for a file?
There's also a way to programmatically access Explorer's search function, but I forgot how.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 8th, 2006, 10:11 AM
#6
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;
}
-
Jan 8th, 2006, 01:03 PM
#7
Lively Member
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;
}
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|