### Why do i get dots in path by FindNextFile() ?

Hi everyone,
Im new to this forum...
Can you tell me why i get dots in file paths by FindNextFile...
I got the out put like this:
C:\Program Files\.\...\file.exe
why?
Can you explain further? That output could be caused by anything. Can you show how you are using FindFirstFile/FindNextFile?

That said... FindFirstFile and FindNextFile will enumerate over these 2 directories (in addition to any other directories/files in the path you're looking in):

"." (the current directory)
".." (the parent directory)

Whether or not that's the cause of your problem, I couldn't say without seeing code.
@Disch:
Code:
 1234567891011121314 int PrintAllFiles(string folder) { WIN32_FIND_DATA ffd; HANDLE hfile=FindFirstFile(string(folder+"\\*.*").c_str(), &ffd); do{ if(!ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { cout << folder << "\\"<< ffd.cFileName << endl; }else{ string newpath=folder+"\\"+ffd.cFileName; PrintAllFiles(newpath.c_str()); } }while(FindNextFile(hfile, &ffd)) }

Here is how i use it :
PrintAllFiles("C:");
I get the output like this:
C:\Program Files\.\..\program.exe
What I want is all the files in C:
drive...
Last edited on
Yeah so my previous post was correct.

Start by just printing the files without recursion to see what I mean:

 12345678  WIN32_FIND_DATAA ffd; HANDLE hfile=FindFirstFileA("C:\\Program Files\\*.*", &ffd); do { cout << ffd.cFileName << endl; }while(FindNextFileA(hfile,&ffd)); FindClose(&ffd);

Notice how you get two extra directories here:

"."
and
".."

Windows puts these in every directory (except for top-level directories, apparently). "." is this directory, and ".." is the parent directory.

If you are iterating over all files... you want to ignore these entries. So you'll have to explicitly look for them and skip over them.

Also:

You're using the wrong character type. FindFirstFile takes TCHAR strings. If you are using regular char strings, you should be using:
FindFirstFileA
FindNextFileA
WIN32_FIND_DATAA

(notice how they all end with and extra 'A'). Details here:

http://www.cplusplus.com/forum/windows/106683/

Also, remember to close your handles with FindClose when you're done with them.

And consistent indenting really makes it easier to read the code.
Last edited on
@ OP: I just want to support Disch's claim that you are in fact using the wrong version of "FindFirstFile()". My guess is that you are not seeing an error only because you are using the MingW compiler where the definition of 'TCHAR' in the version of "winnt.h" that comes with it is different then it is in the version of "WinNt.h" that comes with Microsoft compilers. I only know this because I've recently transitioned to an MS compiler myself and I'm having to realize these little nuances as I update my older code. It's worth learning these things correctly the first time because, trust me, unlearning things like this is difficult.
Computergeek01 wrote:
MingW compiler where the definition of 'TCHAR' in the version of "winnt.h" that comes with it is different then it is in the version of "WinNt.h" that comes with Microsoft compilers

What do you mean by that ? TCHAR macro is the same on both compilers, it is only that MinGW does not define UNICODE and _UNICODE macros by default, when Microsoft compiler does.
That's exactly what I mean. That macro affects the definition of TCHAR. I guess I could have put more detail into my post.
you need to ignore directory "."
and directory ".."

(as I already mentioned ;P )
Guys, what I really wanted was
all the files in the c:\ drive...
I could use the dir "c:\*.*" /s /b >> file.txt
but in that i get directory paths
I only want file paths...
Thats why i use that method...
how will I ignore the "." directory in the listing?
Last edited on
Do you know how to compare two strings?

All you have to do is compare the filename string to "." and ".."
If it's equal to either of those, you simply don't print it.

EDIT:

Since these are char arrays, you can use strcmp to compare them:

 12345678 do { // check to make sure the file name is neither "." nor ".." if( strcmp(ffd.cFileName,".") && strcmp(ffd.cFileName,"..") ) { // do the whatever else you were doing here } }while( FindNextFileA(hfile, &ffd) );

EDIT: fixed mistake. That's what I get for copy-pasting. Thanks Computergeek.

EDIT: fixed another mistake
Last edited on
@ Disch: Psst ypotay in odecay.
Okay,
I made an AV thats the truth!
so if I ignore the "." directory,
do viruses hide in that dir?
 do viruses hide in that dir?

"." is just an alias for "this" directory
".." is just an alias for the parent directory.

So no. Nothing "hides" in them.

C:\somepath\.\program.exe
and
C:\somepath\program.exe

are the same path and file. As are:

C:\foo\bar\..\baz.exe
and
C:\foo\baz.exe

EDIT: removing snark comment.
Last edited on
@Disch: Thankyou very much!
@Disch: Perhaps it should have been strcmp.
doh. Yes.
I am also new to this forum, nice to meet you, but I was a novice programmer, so I thought I might not be able to help you if you have a google talk account, please tell me
Last edited on
Topic archived. No new replies allowed.