 |
|
 |
Hi all,
i have the problem in getting file names one by one from folder.
I am having alld .dll files in folder and i want it to unregister so i need the file names to be retrieved .
Waiting for the reply,
reply as soon as possible i need it urgent..
best regards
ganesh
|
|
|
|
 |
|
 |
I have a need to find out the number of files & folders existing in a main folder,the same way the windows exploer gives in the status bar, for which I have browsed net, several sites, but I have't got the details, not even some idea & then I have written the following function to get the required result & I have decided to post it in this site so that if any other guy needs the same.
I've no doubt that it took you a bit of time to find a solution to your problem (some things are just like that when you don't know where to even start), but I have to ask, in the nicest way possible, once you found out what it took to accomplish your goal, why did you not go back and do a search again? Searching for FindFirstFile() and WIN32_FIND_DATA would return more Web sites than you can shake a stick at. What you would have hopefully found is that your project, while not necessarily a trivial one, has been thoroughly researched.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
 |
|
 |
I liked your idea and have added file size and a few other bells n whistles. Like the other people that have responded, you did make a few mistakes in your logic and use of the language: One thing, the function Folder and the structure pointer have the same name which confused my compile. I made the stuct pointer global to fix that and changed the name of the function. The file size variable I added it not supported my all OS or compilers but __int64 works for me. If you would like, I'll e-mail you the finished project.
Roger L. McElfresh rlmcelfresh@charter.net
|
|
|
|
 |
|
 |
There's simply no point in using free store allocation here. There are several calls to new in this code, but no calls to delete. Simply returning the MyFolder struct by value would make it faster, safer and consume less memory at runtime.
|
|
|
|
 |
|
 |
No need to take much risk. Here is simple code to count number of folders and files in a directory(folder)
//Define member variables
int m_nFolders, m_nFiles;
void GetFolderInfo(CString strPath) //path should be provided.
{
CFileFind ff;
BOOL bFlag = ff.FindFile(strPath + "\\*.*");
m_nFolders=0;
m_nFiles=0;
while(bFlag)
{
bFlag = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots()) //IsDots determines parent directory.
m_nFolders++; // counts number of folders
else
m_nFiles++; // counts number of files
}
ff.Close();
}
Call the function like this:
GetFolferInfo("C:");
OR
GetFolferInfo("C:\\My Documents");
Thanks and regards
Bhushan
|
|
|
|
 |
|
 |
Thank you very much for your suggestion
It is my first article & I am not that much experienced, & it is also a good way to get the result.
thank you once again.
regards
baji.
|
|
|
|
 |
|
 |
& aso what I felt is, CFileFind is wrapping out FindFirstFile of win32_Find_data & I feel that It will give more coding comfort than performance.
baji..
|
|
|
|
 |
|
 |
It's okay. I was also building complex logics instead of simple one in the begining stages.
Regards
Bhushan
|
|
|
|
 |
|
 |
Bhushan:
Your code didn't find the files and folders recursively.
What you need is something like this:
void GetFolderInfo(CString strPath,int &m_nFolders, int &m_nFiles)
{
CFileFind ff;
BOOL bFlag = ff.FindFile(strPath + "\\*.*");
while(bFlag)
{
bFlag = ff.FindNextFile();
if(ff.IsDots()) continue;
if(ff.IsDirectory())
{
m_nFolders++; // counts number of folders
CString subFolderPath=ff.GetFilePath();
GetFolderInfo(subFolderPath,m_nFolders,m_nFiles);
}
else
{
m_nFiles++; // counts number of files
}
}
ff.Close();
}
In the main you can call something like:
int nFolders=0;
int nFiles=0;
GetFolderInfo("C:\\My Documents";,nFolders, nFiles);
Regards
Kiran
|
|
|
|
 |
|
 |
thax for the code.
but i am not getting the actual file count here.
but getting the currect folder count..
can u pls check that code again and resend it?
Praveen Chandran
|
|
|
|
 |
|
 |
In VC++, I think it's better use CFileFind class. It would be more easy and clear.
|
|
|
|
 |
|
 |
Go down load the unix DU or FIND commands, and don't waste our time and add clutter to this site with such newbish stuff.
|
|
|
|
 |
|
|
 |
|
 |
er... is the age of politness totally dead? Just because one thinks that a contributor - or, even, a repondent - is a total dickhead, doesn't mean that one has to say so. And surely such an experienced programmer can respond with a little generosity to a newcomer who's made an effort to share.
brian
|
|
|
|
 |
|
 |
brian scott wrote:
er... is the age of politness totally dead? Just because one thinks that a contributor - or, even, a repondent - is a total dickhead, doesn't mean that one has to say so. And surely such an experienced programmer can respond with a little generosity to a newcomer who's made an effort to share
500% agreed,Every body have right to contribute and Experience programmer must help the InExperience.
ThatALL the Codeproject about,For helping each Other
-----------------------------
"I Think this Will Help"
-----------------------------
Alok Gupta
visit me at http://www.thisisalok.tk
|
|
|
|
 |
|
 |
Try putting it in a demo if you need proof.
Steve
|
|
|
|
 |
|
 |
thanks for u r mail,
actually, I am using the same function in my code, & I have't got any probhlems there, it is functioning properly..
|
|
|
|
 |
|
 |
I have forgotten to change the name of threadCreation(--) to Folder(---)
just I am calling it recursively.
thank u very much.
|
|
|
|
 |
|
 |
Could you please double check the function in your code with the one you posted. The noted lines do not compile. Oh, and have you addressed the memory leak? Please update the code corrections. else if(pdest != NULL && result == 3 && length == 3) { strcat(strFolderName,"\*.*"); // Does not compile Drive = TRUE; }
{ Folder->Folders += 1; MyFolder *Folder2 = new MyFolder; CString subFolderPath; subFolderPath.Empty(); subFolderPath += strFolderPathLocal; subFolderPath += "\\"; subFolderPath += FindFileData.cFileName; //it is a subfolder so again //recursively call the subFolder also & add no of files Folder2=ThreadCreation(subFolderPath);//,nthtime++); // Does not compile Folder->Folders += Folder2->Folders; Folder->Files += Folder2->Files; }
Thanks, Steve
|
|
|
|
 |
|
 |
Steve,
it will give a warning for the first one u have shown, that is just because if the input is passed as C:\ instead U can pass C:, if U want to find out the number of folders & files existing in entire C drive
& the second error u have shown, is the exact one I have forgotten to change & I want to call the same function Folder(---) recursively there,
I have updated it just now,
& thanks for u r mail..
|
|
|
|
 |
|
 |
Thanks for your response, strcat(strFolderName,"\*.*"); Folder2 = Folder(subFolderPath); d:\htpccommon\utilities.cpp(1040) : warning C4129: '*' : unrecognized character escape sequence d:\htpccommon\utilities.cpp(1075) : error C2064: term does not evaluate to a function The first does not cause a warning is does not compiled I had already tried the second change you had suggested in another response it still does not compile. PS: and what about the memory leak? Thanks, Steve
|
|
|
|
 |
|
 |
Steve,
use two backslashes because this represents the char '\' in C++. so, it will be:
strcat(strFolderName, "\\*.*");
One backslash and a special char can represent a special char, like carriage return or line feed ('\n' and '\r').
Cheers,
Dan Lobo
|
|
|
|
 |
|
 |
MyFolder* Folder(CString strFolderPathLocal) { char strFolderName[_MAX_PATH] = {0}; strcpy(strFolderName,strFolderPathLocal); ...
pdest = strchr(strFolderName,ch); ...
if(strFolderName[strlen(strFolderName)] == '\\') strcat(strFolderName,"*.*"); else if(pdest != NULL && result == 3 && length == 3) strcat(strFolderName,"\*.*"); Drive = TRUE; else ...
if((stricmp(FindFileData.cFileName,".")!=0) {
...
MyFolder *Folder2 = new MyFolder; ...
Folder2 = Folder(subFolderPath);
Folder->Folders += Folder2->Folders;
Folder->Files += Folder2->Files;
...
if(!FindNextFile(hFile,&FindFileData))
|
|
|
|
 |
|
 |
struct _MyFolder
{
int Folders;
int Files;
CString Path;
};
#define FolderIsFolder( sFind ) ( sFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0
#define FolderIsFile( sFind ) ( sFind.dwFileAttributes & (FILE_ATTRIBUTE_TEMPORARY|FILE_ATTRIBUTE_HIDDEN) ) == 0
#define FolderIsDot( sFind ) ( sFind.cFileName[0] == TEXT('.') && (sFind.cFileName[1] == TEXT('\0') ||\
(sFind.cFileName[1] == TEXT('.') && sFind.cFileName[2] == TEXT('\0'))) )
void FolderTraverse( struct _MyFolder& Folder )
{
int nLength = Folder.Path.GetLength();
if ( Folder.Path[nLength - 1] == TEXT('\\') )
{
Folder.Path += TEXT("*.*");
}
else
{
Folder.Path += TEXT("\\*.*");
nLength ++;
}
WIN32_FIND_DATA FindFileData;
HANDLE hFile = ::FindFirstFile( Folder.Path, &FindFileData );
if ( hFile )
{
do
{
if ( FolderIsDot( FindFileData ) )
continue;
if ( FolderIsFolder( FindFileData ) )
{
Folder.Folders ++;
Folder.Path += FindFileData.cFileName;
FolderTraverse( Folder );
Folder.Path.ReleaseBuffer( nLength );
}
else if ( FolderIsFile( FindFileData ) )
{
Folder.Files ++;
}
} while ( ::FindNextFile( hFile, &FindFileData ) );
::FindClose( hFile );
}
}
|
|
|
|
 |
|
 |
thanks for your comments & sepcail thanks for giving some good suggestions
|
|
|
|
 |