Click here to Skip to main content
15,921,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the Folders present in the desktop.

I created this coding to get the folders.

XML
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream.h>
#include <vector>
using namespace std;
int num_dirs(const char* path);
int main(int argc, char *argv[])
{
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    string sPath;
    vector<string> MyVect;
    sPath.assign("C:\\Documents and Settings\\yugesh\\Desktop\\*");
    hFind = FindFirstFile(sPath.data(), &FindFileData);
    do
    {
        if(FindFileData.dwFileAttributes == 16)
        {
            MyVect.push_back(FindFileData.cFileName);
        }
    }while (FindNextFile(hFind, &FindFileData));
    FindClose(hFind);
    for(int i =0; i<MyVect.size(); i++)
        cout<<MyVect.at(i).data()<<endl;
    return 0;
}


Now, How to find the size of the folder?
FindFileData.nFileSizeLow() is not displaying.. It always gives the value zero for folders.

Also how to find the sub folders in those folder i found?

Please help me..
Posted

I done it myself like this..
Make use of it..
 
#include "stdafx.h"
#include <windows.h>
#include <Shlobj.h>
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <conio.h>
using namespace std;
int FindFolder(string, string, int);
#define FOLD 16
typedef struct
{
string FolderName;
int Foldersize;
int FolderFiles;
}DeskFold;
int files;
int main(int argc, char *argv[])
{
/* DELCARATIONS */
WIN32_FIND_DATA FindFileData;
char cArr[1000];
int icount = 0;
int i;
string sTmp;
 
HWND hwnd;
HANDLE hFind;
 
vector<DeskFold> MyVect;
DeskFold SObj;
/* Finds the path of desktop */
SHGetSpecialFolderPath(hwnd,cArr,CSIDL_DESKTOP,0);
sTmp.assign(cArr);
sTmp.append("\\");
string sPath;
sPath.assign(sTmp);
sPath.append("*");
/* Finds the Files and Folder inside the specified desktop */
hFind = FindFirstFile(sPath.data(), &FindFileData);
do
{
if(FindFileData.dwFileAttributes == FOLD) // Get access only on folders
{
icount++;
if( icount > 2)
{
SObj.FolderName.assign(FindFileData.cFileName);
files = 0;
SObj.Foldersize = FindFolder(sTmp, FindFileData.cFileName, 0);
SObj.FolderFiles = files;
MyVect.push_back(SObj);
}
}
}while (FindNextFile(hFind, &FindFileData));
 
FindClose(hFind);
 
for(i = 0; i<MyVect.size(); i++)
{
cout<<"Folder name : "<<MyVect.at(i).FolderName.data()<<endl;
cout<<"Folder size : "<<MyVect.at(i).Foldersize<<endl;
cout<<"Folder files : "<<MyVect.at(i).FolderFiles<<endl<<endl;
}
cout<<"\nTotal number of folders in desktop : "<<icount-2<<endl;
 
return 0;
}
int FindFolder(string sPath, string FileName, int foldersize)
{
int icount = 0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string sPath1;
sPath.append(FileName.data());
sPath.append("\\");
sPath1.append(sPath);
sPath1.append("*");
hFind = FindFirstFile(sPath1.data(), &FindFileData);
do
{
foldersize += FindFileData.nFileSizeLow;
if(FindFileData.dwFileAttributes == 16)
{
icount++;
if( icount > 2)
FindFolder(sPath,FindFileData.cFileName,FindFileData.nFileSizeLow);
}
else
files++;
}while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return foldersize;
}
 
Have a nice day
 
Share this answer
 
I done it myself like this..
Make use of it..


#include "stdafx.h"
#include <windows.h>
#include <Shlobj.h>
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <conio.h>

using namespace std;

int FindFolder(string, string, int);

#define FOLD 16
typedef struct
{
string FolderName;
int Foldersize;
int FolderFiles;
}DeskFold;

int files;

int main(int argc, char *argv[])
{
/* DELCARATIONS */
WIN32_FIND_DATA FindFileData;
char cArr[1000];
int icount = 0;
int i;
string sTmp;

HWND hwnd;
HANDLE hFind;

vector<DeskFold> MyVect;
DeskFold SObj;

/* Finds the path of desktop */
SHGetSpecialFolderPath(hwnd,cArr,CSIDL_DESKTOP,0);
sTmp.assign(cArr);
sTmp.append("\\");
string sPath;
sPath.assign(sTmp);
sPath.append("*");

/* Finds the Files and Folder inside the specified desktop */
hFind = FindFirstFile(sPath.data(), &FindFileData);
do
{
if(FindFileData.dwFileAttributes == FOLD) // Get access only on folders
{
icount++;
if( icount > 2)
{
SObj.FolderName.assign(FindFileData.cFileName);
files = 0;
SObj.Foldersize = FindFolder(sTmp, FindFileData.cFileName, 0);
SObj.FolderFiles = files;
MyVect.push_back(SObj);
}
}
}while (FindNextFile(hFind, &FindFileData));

FindClose(hFind);


for(i = 0; i<MyVect.size(); i++)
{
cout<<"Folder name : "<<MyVect.at(i).FolderName.data()<<endl;
cout<<"Folder size : "<<MyVect.at(i).Foldersize<<endl;
cout<<"Folder files : "<<MyVect.at(i).FolderFiles<<endl<<endl;
}

cout<<"\nTotal number of folders in desktop : "<<icount-2<<endl;


return 0;
}

int FindFolder(string sPath, string FileName, int foldersize)
{
int icount = 0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string sPath1;
sPath.append(FileName.data());
sPath.append("\\");
sPath1.append(sPath);
sPath1.append("*");
hFind = FindFirstFile(sPath1.data(), &FindFileData);
do
{
foldersize += FindFileData.nFileSizeLow;
if(FindFileData.dwFileAttributes == 16)
{
icount++;
if( icount > 2)
FindFolder(sPath,FindFileData.cFileName,FindFileData.nFileSizeLow);
}
else
files++;
}while (FindNextFile(hFind, &FindFileData));

FindClose(hFind);
return foldersize;
}

Have a nice day:)
 
Share this answer
 
you have to iterate through the size of the child file and child folders to find the size of a folder, it's not an attribute of the folder itself

And to find the child folders you have to start a new search inside it ...

it's a perfect problem for a recursive solution - in fact, any other solution would be considerably more difficult
 
Share this answer
 
v2
Comments
IT-NEWBIE 26-Jul-11 7:52am    
Sorry.. I can't understand you? Actually I have another problem to get the sub folder details also. Only by resolving that i can implement your words. Do you have any idea to do that.

Please give me some guidelines..
barneyman 27-Jul-11 2:48am    
FindFileData.nFileSize is not stored for directories

you'll need a recursive solution - something like the following (it's pseudocode, it won't compile, don't even try :)

void MyFind(const char *path)
{
finddata fileinfo;
HANDLE findHandle=FindFirst(path,&fileinfo);
for(bool found=(findHandle?true:false);found;found=FindNext(findHandle,&fileinfo))
{
if(fileinfo.flags & DIRECTORY)
MyFind(fileinfo.path);
}
}

void main(...)
{
MyFind("c:\\*.*");
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900