怎样知道一个文件夹有那些文件 怎样知道一个文件夹有那些文件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 _findfirst(...,"*.*",...);_findnext(...,"*.*",...);...这样会找出两个目录出来 "."和"..",你把它们去掉就好了. void IndexFile::CreateFileList( AnsiString& srcpath ){ long findhandle = 0; _finddata_t fd; HANDLE hf; FILETIME ft; AnsiString path = srcpath; if( path.Length() > 0 && path[path.Length()] != '\\') path += '\\'; AnsiString str = path + "*.*"; AnsiString perrecode; char attbuf[15] = "\x003\x004\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n"; if( m_Stop ) { Form2->Label1->Caption = "正在停止..."; return; } findhandle = _findfirst(str.c_str(), &fd); if( findhandle != -1 ) { if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR ) { if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..") { m_ItemCount ++; perrecode = path + fd.name; fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile ); fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile); CreateFileList(perrecode); } } else { m_ItemCount++; perrecode = path + fd.name; ft.dwLowDateTime = 0; ft.dwHighDateTime = 0; hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( hf != INVALID_HANDLE_VALUE ) { GetFileTime( hf, NULL, NULL, &ft ); CloseHandle( hf ); } memcpy( attbuf+2, &(fd.size), 4); memcpy( attbuf+6, &(ft.dwLowDateTime), 4); memcpy( attbuf+10, &(ft.dwHighDateTime), 4); fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile ); fwrite( attbuf, 1, 15, m_IdxFile) ; } int fr = 0; MSG msg; do { if( m_Stop ) { Form2->Label1->Caption = "正在停止..."; return; } if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } fr = _findnext(findhandle, &fd); if( fr!=0) break; if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR ) { if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..") { m_ItemCount++; perrecode = path + fd.name; fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile ); fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile); CreateFileList(perrecode); } } else { m_ItemCount++; perrecode = path + fd.name; ft.dwLowDateTime = 0; ft.dwHighDateTime = 0; hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( hf != INVALID_HANDLE_VALUE ) { GetFileTime( hf, NULL, NULL, &ft ); CloseHandle( hf ); } memcpy( attbuf+2, &(fd.size), 4); memcpy( attbuf+6, &(ft.dwLowDateTime), 4); memcpy( attbuf+10, &(ft.dwHighDateTime), 4); fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile ); fwrite( attbuf, 1, 15, m_IdxFile) ; } }while(fr==0); _findclose(findhandle); }}_____________这是整个函数.BCB下用的.ANSISTRING 是BCB的字符串类.ANSISTRING::C_STR()和CSTRING的LPCTSTR类似. EnumerateFile("e:\\songs\\");void EnumerateFile(CString sPath){ WIN32_FIND_DATA fd; CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1); HANDLE hFind = ::FindFirstFile(sPath + "*.*",&fd); if (hFind != INVALID_HANDLE_VALUE) { while (::FindNextFile(hFind,&fd)) { //判断是否为目录 if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) { CString name; name = fd.cFileName; //判断是否为.和.. if ((name != ".") && (name != "..")) { //如果是真正的目录,进行递归 EnumerateFile(sPath + fd.cFileName + "\\"); } } pListBox->InsertString(-1,fd.cFileName); } ::FindClose(hFind);} VS2008程序移植到vs2010编译出错,求助啊! 求救:MFC调用WebService错误 事件,信号量,互斥对象 是不是都可以在进程间使用呢? 使用API禁止笔记本电脑给电池充电的问题 CRichEditCtrl中可以显示图片吧?怎样做! 请教 请问在IOCP模型下如何建立一条堵塞方式的连接 用VC连ACCESS数据库执行一条插入语句怎么做? 谢谢 怎样控制菜单项的显示和隐藏? 如何在创建的对话框左上角添加图标? 请 aben456 (风轻扬) 来领分,十分感谢!(3) 一个关于类的问题(急,在线等待)
_findnext(...,"*.*",...);
...
这样会找出两个目录出来 "."和"..",你把它们去掉就好了.
{
long findhandle = 0;
_finddata_t fd;
HANDLE hf;
FILETIME ft;
AnsiString path = srcpath;
if( path.Length() > 0 && path[path.Length()] != '\\')
path += '\\';
AnsiString str = path + "*.*";
AnsiString perrecode;
char attbuf[15] = "\x003\x004\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n";
if( m_Stop )
{
Form2->Label1->Caption = "正在停止...";
return;
}
findhandle = _findfirst(str.c_str(), &fd);
if( findhandle != -1 )
{
if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR )
{
if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..")
{
m_ItemCount ++;
perrecode = path + fd.name;
fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile);
CreateFileList(perrecode);
}
}
else
{
m_ItemCount++;
perrecode = path + fd.name;
ft.dwLowDateTime = 0;
ft.dwHighDateTime = 0;
hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
if( hf != INVALID_HANDLE_VALUE )
{
GetFileTime( hf, NULL, NULL, &ft );
CloseHandle( hf );
}
memcpy( attbuf+2, &(fd.size), 4);
memcpy( attbuf+6, &(ft.dwLowDateTime), 4);
memcpy( attbuf+10, &(ft.dwHighDateTime), 4);
fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
fwrite( attbuf, 1, 15, m_IdxFile) ;
}
int fr = 0;
MSG msg;
do
{
if( m_Stop )
{
Form2->Label1->Caption = "正在停止...";
return;
}
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} fr = _findnext(findhandle, &fd);
if( fr!=0)
break;
if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR )
{
if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..")
{
m_ItemCount++;
perrecode = path + fd.name;
fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile);
CreateFileList(perrecode);
}
}
else
{
m_ItemCount++;
perrecode = path + fd.name;
ft.dwLowDateTime = 0;
ft.dwHighDateTime = 0;
hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
if( hf != INVALID_HANDLE_VALUE )
{
GetFileTime( hf, NULL, NULL, &ft );
CloseHandle( hf );
}
memcpy( attbuf+2, &(fd.size), 4);
memcpy( attbuf+6, &(ft.dwLowDateTime), 4);
memcpy( attbuf+10, &(ft.dwHighDateTime), 4);
fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
fwrite( attbuf, 1, 15, m_IdxFile) ;
}
}while(fr==0);
_findclose(findhandle);
}
}
_____________
这是整个函数.BCB下用的.ANSISTRING 是BCB的字符串类.ANSISTRING::C_STR()和CSTRING的LPCTSTR类似.
{
WIN32_FIND_DATA fd; CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
HANDLE hFind = ::FindFirstFile(sPath + "*.*",&fd);
if (hFind != INVALID_HANDLE_VALUE)
{
while (::FindNextFile(hFind,&fd))
{
//判断是否为目录
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
CString name;
name = fd.cFileName;
//判断是否为.和..
if ((name != ".") && (name != ".."))
{
//如果是真正的目录,进行递归
EnumerateFile(sPath + fd.cFileName + "\\");
}
}
pListBox->InsertString(-1,fd.cFileName);
}
::FindClose(hFind);
}