Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

File Searching

VC5.0:
How do i search and retrieve the path of a file  TEST.CPP ?  The drive and the directory which it sits in is unknown.


 
0
engllf
Asked:
engllf
  • 3
  • 2
  • 2
1 Solution
 
engllfAuthor Commented:
Edited text of question
0
 
mpewjgCommented:
Please try class CFileFind

FindFile() can begin the search.
GetFilePath() can fetch the found file's path name.
0
 
mpewjgCommented:
CFileFind is an MFC class.  Are you working in MFC?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nietodCommented:
Prefer direct call to API.
0
 
engllfAuthor Commented:
I am working in MSVC.
But I prefer direct call to API.
I am trying not to rely to much on MFC.  

0
 
engllfAuthor Commented:
answer coming in a minute.
0
 
nietodCommented:
First you need a starting point for your search, possibly the root directory of a specified drive, or some other directory depending on you needs.  You will search that directory and if you find the file you are done.  If not you will find each sub-directory in the directory and search them recursively until one search succeeds.  

To search a directory for a file or for other directories use the FindFirstFile() and FindNextFile() API commands (there are also extended versions, that might be a little faster.)  

for a particular directory use FindFirstFile() to get information about the file and then enter a loop that checks the information to see if it is a the right file.  If it is, return true from your search procedure.  If not, use FindNextFile() and continue the loop until you find the file or FindNextFile(0 fails (the FindFirstFile() could also fail) indicating there are no more files.  In that run a second loop that is like the first but this time check the attributes of the files found to see which one are directories.  If a file is a directory call the search function recursively.  If it returns true, return true. If it returns false, continue the loop.  When this loop runs out of files, return false, indicating the file was not found in the directory or its sub-directories.

let me know if you need any details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now