您的位置:首页 > 其它

VC-获取文件夹中的指定类型的所有文件名和文件大小

2008-09-11 15:27 761 查看
typedef CList<long,long> LongList;

typedef CList<CString,CString&> StringList;

int GetFileList(CString path,CString ext,LongList& sizelist,StringList& namelist)

{

CString FileName;

WIN32_FIND_DATA fdata = {0};

BOOL bFinished = FALSE;

int nIndex = 0;

FileName = path + "*." + ext;

HANDLE hSearch = FindFirstFile(FileName.GetBuffer(0),&fdata);

if(hSearch == INVALID_HANDLE_VALUE)

return 0;

while(!bFinished)

{

if(!(fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))

{

if (nIndex==0)

{

sizelist.AddHead((long)fdata.nFileSizeLow);

FileName.Format("%s%s",path,fdata.cFileName);

namelist.AddHead(FileName);

}

else

{

POSITION pos;

pos = sizelist.FindIndex(nIndex);

sizelist.InsertAfter(pos,(long)fdata.nFileSizeLow);

pos = namelist.FindIndex(nIndex);

FileName.Format("%s%s",path,fdata.cFileName);

namelist.InsertAfter(pos,FileName);

}

nIndex++;

}

if(!FindNextFile(hSearch, &fdata))

{

if (GetLastError() == ERROR_NO_MORE_FILES)

bFinished = TRUE;

}

}

FindClose(hSearch);

return nIndex;

}

取Long型CList列表中的最大值的索引:

int GetMaxFileIndex(LongList& sizelist,int nCount)

{

int nIndex = 0;

long nValMax = sizelist.GetHead();

for(int i=1;i<nCount;i++)

{

long nVal = sizelist.GetAt(sizelist.FindIndex(i));

if(nVal > nValMax){

nValMax = nVal;

nIndex = i;

}

}

return nIndex;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: