|
-
October 30th, 2008, 08:28 AM
#1
solved: Find all directories in a directory
hi all,
i did not find any answer to my following question: how can i find all directories which are inside a specified directory using the MFC.
I wanted to use a similar approach to finding a file inside a directory but i did not find an analogon to FindFile.
Sounds strange, but i hope you will understand me! 
Does anybody know how i can realize this ?!
best regards,
#50
Last edited by Raute50; October 30th, 2008 at 10:51 AM.
Reason: problem was solved
-
October 30th, 2008, 09:32 AM
#2
Re: Find all directories in a directory
You keep using FindFile, it also returns directories. Use CFindFile::IsDirectory to check if the returned file is actually a directory.
-
October 30th, 2008, 09:41 AM
#3
Re: Find all directories in a directory
i tried to but it did not work ?!
my code for finding files looks like this:
Code:
void CMyDlg::OnButton1()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
CString argv = "C:\\MyDirectory\\Textfiles\\*.txt";
hFind = FindFirstFile(argv, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
if (ERROR_FILE_NOT_FOUND == GetLastError())
MessageBox("ERROR_FILE_NOT_FOUND");
}
else
{
// List all the files in the directory with some info about them.
LARGE_INTEGER filesize;
CString files = "found these files : \n";
do
{
if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
files += FindFileData.cFileName;
files += "\n";
}
}
while (FindNextFile(hFind, &FindFileData) != 0);
MessageBox(files);
DWORD dwError=0;
dwError = GetLastError();
if (dwError == ERROR_NO_MORE_FILES)
{ MessageBox("ERROR_NO_MORE_FILES"); }
}
}
this does already work for finding txt files!!!
i tried to change the CString argv to "C:\\MyDirectory\\Textfiles\\*" or "C:\\MyDirectory\\Textfiles\\*\\"; but both did not work.
with other things i tried i got errors.
can you tell me more explicitly how i have to manage this ?!
best regards,
#50
Last edited by Raute50; October 30th, 2008 at 09:52 AM.
-
October 30th, 2008, 09:50 AM
#4
Re: Find all directories in a directory
Well,If what you want is to find files in a directory u could do something like
Code:
//assign Pathname variable with the full path name
//*.log = delete all files that has the .log extension or you could put filename.* to find all the files with that name no matter the extension
CString PathName = "C:\\WINDOWS\\*.log";
//Find the file Info
hnd = FindFirstFile((LPCSTR) PathName,&info);
//Start the loop
do
{
//Assign csFile with Full path
CString csFile = "C:\\WINDOWS\\";
//here you can get the files name,size,creating time,attributes,acces time,etc.
csFile += info.cFileName;
//You can delete,move,copy,rename,write,read or what ever you want.
DeleteFile(csFile);
}
//While findnextfile keep asking for findnextfile keep looping
while( FindNextFile(hnd, &info) );
//Close file search
FindClose(hnd);
Hope that helped if that's what u where looking for.
-
October 30th, 2008, 09:54 AM
#5
Re: Find all directories in a directory
no ... it doesn't help anyway ... because i already know how to find files .... i want to know how i find directories.
Thats why i chose the title of this thread! 
#50
-
October 30th, 2008, 09:58 AM
#6
Re: Find all directories in a directory
Well,Can you post the errors that its giving you?
-
October 30th, 2008, 10:13 AM
#7
Re: Find all directories in a directory
the only other way i tried to was "C:\\MyDirectory\\Textfiles\\*\";
error C2001: newline in constant
error C2146: syntax error : missing ';' before identifier 'hFind'
but as i wrote down i understood the error ... 
any hints ?!
#50
-
October 30th, 2008, 10:51 AM
#8
Re: Find all directories in a directory
solving my own problem:
i used WIN32_FIND_DATA FindFileData; in my code, which has to be replaced by CFileFind FindFileData; !!!
Afer some more changes and with the right use of FindFileData.IsDirectory() everything works correctly!
Thanks for all answers ...
best regards,
#50
-
October 30th, 2008, 10:56 AM
#9
Re: Find all directories in a directory
 Originally Posted by Raute50
i tried to but it did not work ?!
...
i tried to change the CString argv to "C:\\MyDirectory\\Textfiles\\*" or "C:\\MyDirectory\\Textfiles\\*\\"; but both did not work.
with other things i tried i got errors.
Define "not work".
Victor Nijegorodov
-
October 30th, 2008, 11:39 AM
#10
Re: Find all directories in a directory
one of them caused the following MessageBox
MessageBox("ERROR_FILE_NOT_FOUND");
the other caused a MessageBox where the only entry was:
found these files : \n
i did not remember which case of "...\\?" caused which MessageBox ?
hope i could help you ?!
#50
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|