如何使用C ++程序在目录中找到子文件夹? [英] how to find the sub folders in directory using c++ program?

查看:98
本文介绍了如何使用C ++程序在目录中找到子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到桌面上存在的文件夹.

我创建了此编码来获取文件夹.

I want to find the Folders present in the desktop.

I created this coding to get the folders.

#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;

}



现在,如何查找文件夹的大小?
FindFileData.nFileSizeLow()未显示.它始终为文件夹提供零值.

还有如何在我找到的那些文件夹中找到子文件夹?

请帮助我..



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..

推荐答案

您必须遍历子文件和子文件夹的大小以找到文件夹的大小,而不是文件夹本身的属性

并且要找到子文件夹,您必须在其中开始新的搜索...

对于递归解决方案来说,这是一个完美的问题-实际上,任何其他解决方案都将更加困难
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


我是这样自己完成的..
利用它..


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

使用名称空间std;

int FindFolder(string,string,int);

#define折叠16
typedef struct
{
字符串FolderName;
int Foldersize;
int FolderFiles;
} DeskFold;

int文件;

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

HWND hwnd;
HANDLE hFind;

向量< DeskFold> MyVect;
DeskFold SObj;

/*查找桌面的路径*/
SHGetSpecialFolderPath(hwnd,cArr,CSIDL_DESKTOP,0);
sTmp.assign(cArr);
sTmp.append("\\");
字符串sPath;
sPath.assign(sTmp);
sPath.append("*");

/*在指定的桌面内查找文件和文件夹*/
hFind = FindFirstFile(sPath.data(),& FindFileData);

{
if(FindFileData.dwFileAttributes == FOLD)//仅访问文件夹
{
icount ++;
if(icount> 2)
{
SObj.FolderName.assign(FindFileData.cFileName);
文件= 0;
SObj.Foldersize = FindFolder(sTmp,FindFileData.cFileName,0);
SObj.FolderFiles =文件;
MyVect.push_back(SObj);
}
}
}同时(FindNextFile(hFind,& FindFileData));

FindClose(hFind);


for(i = 0; i< MyVect.size(); i ++)
{
cout<<文件夹名称:"<< MyVect.at(i).FolderName.data()<< endl;
cout<<文件夹大小:"<< MyVect.at(i).Foldersize<< endl;
cout<<文件夹文件:"<< MyVect.at(i).FolderFiles<<< endl<< endl;
}

cout<<"\ n桌面中的文件夹总数:"<< icount-2<< endl;


返回0;
}

int FindFolder(字符串sPath,字符串FileName和int foldersize)
{
int icount = 0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
字符串sPath1;
sPath.append(FileName.data());
sPath.append("\\");
sPath1.append(sPath);
sPath1.append("*");
hFind = FindFirstFile(sPath1.data(),& FindFileData);

{
foldersize + = FindFileData.nFileSizeLow;
if(FindFileData.dwFileAttributes == 16)
{
icount ++;
if(icount> 2)
FindFolder(sPath,FindFileData.cFileName,FindFileData.nFileSizeLow);
}
其他
文件++;
}同时(FindNextFile(hFind,& FindFileData));

FindClose(hFind);
返回文件夹大小;
}

祝您有美好的一天:)
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:)


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


这篇关于如何使用C ++程序在目录中找到子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆