If sentence doesn't work.

I want list only directories that doesn't have . or .. in they're name. I made if sentence but it still shows me directories that have . and .. in they're name.
How can I hide directories . and ..?

if ((FindFileData.cFileName != ".") || (FindFileData.cFileName != "..")){

here's full code
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// del2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Shlwapi.h"


using namespace std;

//bool DirectoryRecursive(char * path = new char[261])
bool DirectoryRecursive(char path[261])
{
	WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
	DWORD Attributes;
	strcat(path,"\\*");
	//path[strlen(path)] = '*';

	hFind = FindFirstFile(path, &FindFileData);
	printf("---Recursive: %s\n",path);
			do{
				if (!(FindFileData.cFileName == ".") || (FindFileData.cFileName == "..")){
				//Str append Example
				char str[MAX_PATH];
				strcpy (str,path);
				str[strlen(path)-1] = '\0';
				strcat (str,FindFileData.cFileName);
				_tprintf (TEXT("---File Found: %s\n"),str);
				Attributes = GetFileAttributes(str);
				_tprintf (TEXT("---File Attributes: %d\n"),Attributes);
				if (Attributes & FILE_ATTRIBUTE_DIRECTORY)
				{
				 //is directory
				printf("---Directory\n");
				DirectoryRecursive(str);
				}
				else
				{
				 //not directory
				printf("---File\n");
				}
				}
			}while(FindNextFile(hFind, &FindFileData));
	return true;
}

int main()
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind;

   //Read list file
   FILE *filein;
   long lSize;
   char * buffer;
   size_t result;
   DWORD Attributes;
   //Get path to list.txt
   	 char szFileName[MAX_PATH];
     HINSTANCE hInstance = GetModuleHandle(NULL);
     GetModuleFileName(hInstance, szFileName, MAX_PATH);
	 PathRemoveFileSpec(szFileName);
	 PathAppend(szFileName,"\\list.txt");
	 //Open file
   filein = fopen(szFileName, "r");
   //if (filein==NULL) {fputs ("File error",stderr); exit (1);}

    // obtain file size:
    fseek (filein , 0 , SEEK_END);
    lSize = ftell (filein);
    rewind (filein);

    // allocate memory to contain the whole file:
    buffer = (char*) malloc ((sizeof(char)*lSize)+1);

	// copy the file into the buffer:
    result = fread (buffer,1,lSize,filein);
	buffer[result] = '\0'; // make extra byte nul
	/*//Print some info
    printf ("%s\n",buffer);// list.txt
	printf ("%s\n",szFileName);//Path
    fclose(filein);
	*/

	//token apc
	char *token;
	token = strtok(buffer,"\r\n");
	   while( token != NULL )
       {
           // While there are tokens in "string"
           printf( "%s\n", token );

		   //List files
			hFind = FindFirstFile(token, &FindFileData);
			do{
				if ((FindFileData.cFileName != ".") || (FindFileData.cFileName != "..")){
				//Str append Example
				char str[MAX_PATH];
				strcpy (str,token);
				str[strlen(token)-1] = '\0';
				strcat (str,FindFileData.cFileName);
				_tprintf (TEXT("File Found: %s\n"),str);
				Attributes = GetFileAttributes(str);
				_tprintf (TEXT("File Attributes: %d\n"),Attributes);
				if (Attributes & FILE_ATTRIBUTE_DIRECTORY)
				{
				 //is directory
				printf("Directory\n");
				//DirectoryRecursive(str);
				}
				else
				{
				 //not directory
				printf("File\n");
				}
				}
			}while(FindNextFile(hFind, &FindFileData));
			FindClose(hFind);

           // Get next token: 
           token = strtok( NULL,"\r\n"); // C4996
       }
	free(buffer);
return	0;
}
if ((FindFileData.cFileName != ".") || (FindFileData.cFileName != "..")) This code skips the current directory / parent directory as they're returned by FindFirstFile()/FincNextFile().

You need to check FindFileData.cFileName for the dot. You can use strchr().
I tried it

if (strchr(FindFileData.cFileName,".") == NULL && strchr(FindFileData.cFileName,"..") == NULL){

Causes error on comple.

1
2
3
4
5
6
1>d:\development\cplusplus\del\del2\del2\del2.cpp(102) : error C2665: 'strchr' : none of the 2 overloads could convert all the argument types
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\string.h(110): could be 'const char *strchr(const char *,int)'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\string.h(183): or       'char *strchr(char *,int)'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\string.h(110): or       'const char *strchr(const char *,int)'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\string.h(183): or       'char *strchr(char *,int)'
1>        while trying to match the argument list '(CHAR [260], const char [2])'
Sorry, my mistake.

First, a directory that has . or .. in it's name is different from a directoy who's name is . or ..

. is the current directory and .. is the parent directory.

If you want to ignore the current and parent directory in your traversal, you need to use strcmp.
1
2
if (strcmp(FindFileData.cFileName, ".") == NULL || strchr(FindFileData.cFileName, "..") == NULL)
    continue;

I changed ur's if a bit (both are strcmp now)

(strcmp(FindFileData.cFileName, ".") == NULL || strcmp(FindFileData.cFileName, "..") == NULL)

Now the output is:
1
2
File Found: C:\users\rain\.
File Found: C:\users\rain\..
I'm sure you can work out which way to use the if.
if (strcmp(FindFileData.cFileName, ".") != 0 || strcmp(FindFileData.cFileName, "..") != 0)
also returns directories with . and ..

if (strcmp(FindFileData.cFileName, ".") == 0 || strcmp(FindFileData.cFileName, "..") == 0)
returns directories only with . or ..

if (strcmp(FindFileData.cFileName, ".") != NULL || strcmp(FindFileData.cFileName, "..") != NULL)
also returns directories with . and ..

if (strcmp(FindFileData.cFileName, ".") == NULL || strcmp(FindFileData.cFileName, "..") == NULL)
returns directories only with . or ..

Any other ideas?

EDIT:
if (strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0) is solution
Last edited on
1
2
3
4
5
6
if (find first on wildcard)
    do
        if (name is .) continue;
        if (name is ..) continue;
        process(name);
    while (find next)
Topic archived. No new replies allowed.