I am writing a file system program in C++. Now I try to write a file finding function. First I want program be able to search the file in all system. I use FindFirstFile and FindNextFile Windows API functions. First I should call FindFirstFile , and give it the directory, where it must search the file. But I don't know, how to specify the diirectory so that FindFirstFile searches in all the system. Please, help me with that question. I will be very grateful for any help.
1 Answers
this is what I've found here:
you cannot use a trailing backslash () in the lpFileName input string for FindFirstFile, therefore it may not be obvious how to search root directories. If you want to see files or get the attributes of a root directory, the following options would apply: To examine files in a root directory, you can use
"C:\*"
and step through the directory by using FindNextFile. To get the attributes of a root directory, use the GetFileAttributes function. Note Prepending the string "\?\" does not allow access to the root directory.
to get a list of available drives you might use GetLogicalDriveStrings()
. This returns a double-null terminated list of null-terminated strings. E.g., say you had drives A, B
and C
in your machine. The returned string would look like this:
A:\<nul>B:\<nul>C:\<nul><nul>
-
1Thank you for the answer, computer. Let me ask you a question. As I understand, "C:\" is a root directory of computer in Windows system ? Am I right? – Victor Sep 08 '13 at 13:11
-
yes. Unix abstracts the nature of this tree hierarchy entirely and in Unix and Unix-like systems the root directory is denoted by / sign. Though the root directory is conventionally referred to as /, the directory entry itself has no name—its name is the "empty" part before the initial directory separator character (/). All filesystem entries, including mounted filesystems are "branches" of this root. – 4pie0 Sep 08 '13 at 13:16
-
Under DOS, OS/2, and Microsoft Windows, each partition has a drive letter assignment (labeled C:\ for a particular partition C) and there is no common root directory above that. DOS, OS/2, and Windows do support more abstract hierarchies, with partitions mountable within a directory of another drive, though this is rarely seen. from: http://en.wikipedia.org/wiki/Root_directory – 4pie0 Sep 08 '13 at 13:16
-
is this enough for you to solve your problem or do you have some additional questions? – 4pie0 Sep 08 '13 at 13:19
-
To say truth, I have some questions, and have to take your time some more. :) . So , in Windows there is no common directory. But how Windows search file in MyComputer when you go to Start->search ? – Victor Sep 08 '13 at 13:42
-
we don't know what it's developers implemented, but for example we see that it can be done the way I answered below: listing all drives and iterating with FindFirstFile then – 4pie0 Sep 08 '13 at 13:48
-
O, now I understand You. Thank you very much for! You really help me! – Victor Sep 08 '13 at 15:06