Getting directories that aren't there

Feb 6, 2010 at 7:13pm
Hi,

I have a function that returns a list of all directories which are in a certain directory. But my problem is that the first and second item of that list always contain respectively the directory "." and ".." . I can't figure out why, and when I was trying to make a workaround. The workaround is that it checks if the first character is '.', but that causes an debug assertion errormessage.

This is the code which I made to return that list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <string>
#include <vector>
#include "Exception.h"

using namespace std;

#ifdef OS_WINDOWS

#include <windows.h>

namespace PEARL
{
	void ListDirectories(string path, vector<string>& dirs)
	{
		//Initializing
		unsigned int dirNum = 1;
		HANDLE hFile = INVALID_HANDLE_VALUE;
		WIN32_FIND_DATAA wfd;
		path += "/*";

		//Start with the first file
		hFile = FindFirstFileA(path.c_str(), &wfd);
		if (hFile == INVALID_HANDLE_VALUE)
		{
			if (errno != ERROR_FILE_NOT_FOUND)
			{
				string errorMessage = "Unable to find the first directory for listing directory \"";
				errorMessage += path;
				errorMessage += "\"";
				throw new Exception(errno, errorMessage);
			}
			else //Return empty.
			{
				dirs.clear();
				return;
			}
		}

		if ( ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) > 0)
			dirs.insert(dirs.begin(), wfd.cFileName);
	
		//And the rest of the files
		while (FindNextFileA(hFile, &wfd) != 0)
		{
			if ( ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) > 0)
			{
				dirs.insert(dirs.begin() + dirNum, wfd.cFileName);
				dirNum++;
			}
		}
		if (errno != ERROR_NO_MORE_FILES && errno != 0)
		{
			string errorMessage = "Unable to find a directory for listing directory \"";
			errorMessage += path;
			errorMessage += "\"";
			throw new Exception(errno, errorMessage);
		}
	}

}

#endif 


Can anybody help me?
Feb 6, 2010 at 9:00pm
How long have you been using computers??
You do know that . and .. are used by the system to represent the current directory and parent directory respectively????
Feb 6, 2010 at 9:30pm
wel... now you say that, indeed. But I didn't expect that those would be added to the list.
Feb 6, 2010 at 11:18pm
Unfortunately you can't do .../, so you have to do ../../.
Feb 7, 2010 at 3:45am
closed account (S6k9GNh0)
What is the point in defining a OS_WINDOWS when the OS itself already defines such a variable?

@topic: If you ever run ls or dir on a *nix or Windows system, you'll notice that it returns "." and "..". You can choose to leave it out by checking to see if it's called "." or ".." but it's more of a hassle and should probably be left in.
Feb 7, 2010 at 1:59pm
What is the point in defining a OS_WINDOWS when the OS itself already defines such a variable?

Ah, I didn't know that, does all OSes already define one?

@topic: If you ever run ls or dir on a *nix or Windows system, you'll notice that it returns "." and "..". You can choose to leave it out by checking to see if it's called "." or ".." but it's more of a hassle and should probably be left in.

I made this function skip the first two directories, that helps too.
Topic archived. No new replies allowed.