如何枚举某一目录下的所有文件名?

dd97911 2005-07-06 09:25:09
如何枚举某一目录下的所有文件名?例如:枚举E:\BIN\下的所有文件,包括扩展名,而后显示出来!!!
...全文
558 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sugengnn 2005-12-28
  • 打赏
  • 举报
回复
学习
hxy2003 2005-09-08
  • 打赏
  • 举报
回复
学习!!
xgwx 2005-08-13
  • 打赏
  • 举报
回复
学习!!
dd97911 2005-07-06
  • 打赏
  • 举报
回复
多谢各位!
bobob 2005-07-06
  • 打赏
  • 举报
回复
功能:列举给定目录和其子目录下的所有文件
调用方法:ListFolder("c:\\");//列举所有c盘的文件
void ListFolder(CString sPath)
{
CFileFind ff;
BOOL bFound;
bFound = ff.FindFile(sPath + "\\*.*");
while(bFound)
{
bFound = ff.FindNextFile();
CString sFilePath = ff.GetFilePath();

if(ff.IsDirectory())
{
if(!ff.IsDots())
ListFolder(sFilePath);
}
else
{
AfxMessageBox(sFilePath);
}
}
ff.Close();
}
pp_ipr 2005-07-06
  • 打赏
  • 举报
回复
下面的例子是删除一个文件夹下的所有文件及子文件夹,可以参考一下。

// this function will delete the specified folder(all files in it and all)
void CDeleteFolderDlg::DeleteFolder(CString strPath)
{
if (strPath.Right(1) != "\\")
{
strPath += _T("\\");
}

CFileFind fileFind;
CString strFilePath = _T("");
BOOL bWorking = fileFind.FindFile(strPath + "*.*");
// find all folder and file in folder
// delete all of them
while (bWorking)
{
bWorking = fileFind.FindNextFile();
if (fileFind.IsDots())
{
continue;
}

strFilePath = fileFind.GetFilePath();
if (fileFind.IsReadOnly())
{
SetFileAttributes(strFilePath, FILE_ATTRIBUTE_NORMAL);
}

if (fileFind.IsDirectory())
{
DeleteFolder(strFilePath);
}
else
{
DeleteFile(strFilePath);
}
}
fileFind.Close();

// remove current folder
RemoveDirectory(strPath);
}
sad_4978 2005-07-06
  • 打赏
  • 举报
回复
/*****************************************************************************
* Function Explain: The function copy an existing directory.
* Input Parameter: lpSourceDirName
* Output Parameter: lpDestDirName
* Return Values: BOOL
******************************************************************************/
BOOL CEditFile::CopyDir(const char *lpSourceDirName,const char *lpDestDirName)
{
if(NULL == lpSourceDirName || NULL == lpDestDirName)
{
return FALSE;
}
DWORD dwError;
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;

char strSeachDirName[BufLength] = {0};
sprintf(strSeachDirName,"%s\\*.*",lpSourceDirName);

CreateDirectory(lpDestDirName,NULL);

hFind = FindFirstFile(strSeachDirName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
if(FILE_ATTRIBUTE_DIRECTORY == (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if((strcmp(FindFileData.cFileName,".") != 0) &&
(strcmp(FindFileData.cFileName,"..") !=0) )
{
char strSrcFileName[BufLength] = {0};
char strDstFileName[BufLength] = {0};
sprintf(strSrcFileName,"%s\\",lpSourceDirName);
sprintf(strDstFileName,"%s\\",lpDestDirName);
strcat(strSrcFileName,FindFileData.cFileName);
strcat(strDstFileName,FindFileData.cFileName);
CopyDir(strSrcFileName,strDstFileName);
}
}
else
{
char strSrcFileName[BufLength] = {0};
char strDstFileName[BufLength] = {0};
sprintf(strSrcFileName,"%s\\",lpSourceDirName);
sprintf(strDstFileName,"%s\\",lpDestDirName);
strcat(strSrcFileName,FindFileData.cFileName);
strcat(strDstFileName,FindFileData.cFileName);
CopyFiles(strSrcFileName,strDstFileName);
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if(FILE_ATTRIBUTE_DIRECTORY == (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
if((strcmp(FindFileData.cFileName,".") != 0) &&
(strcmp(FindFileData.cFileName,"..") !=0) )
{
char strSrcFileName[BufLength] = {0};
char strDstFileName[BufLength] = {0};
sprintf(strSrcFileName,"%s\\",lpSourceDirName);
sprintf(strDstFileName,"%s\\",lpDestDirName);
strcat(strSrcFileName,FindFileData.cFileName);
strcat(strDstFileName,FindFileData.cFileName);
CopyDir(strSrcFileName,strDstFileName);
}
}
else
{
char strSrcFileName[BufLength] = {0};
char strDstFileName[BufLength] = {0};
sprintf(strSrcFileName,"%s\\",lpSourceDirName);
sprintf(strDstFileName,"%s\\",lpDestDirName);
strcat(strSrcFileName,FindFileData.cFileName);
strcat(strDstFileName,FindFileData.cFileName);
BOOL b = FALSE;
b = CopyFiles(strSrcFileName,strDstFileName);
}
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
return FALSE;
}
}
return TRUE;
}
/*****************************************************************************
* Function Explain: The function copies an existing file to a new file.
* Input Parameter: lpSourceFileName
* Output Parameter: lpDestFileName
* Return Values: BOOL
******************************************************************************/
BOOL CEditFile::CopyFiles(const char *lpSourceFileName,const char *lpDestFileName)
{
if(NULL == lpSourceFileName || NULL == lpDestFileName)
{
return FALSE;
}

BOOL bFailIfExists = FALSE;

if(0 == CopyFile(lpSourceFileName,lpDestFileName,bFailIfExists))
{
//DWORD aa = GetLastError();
return FALSE;
}
return TRUE;
}
给你提供一个拷贝函数的方法,可以拷贝文件夹。你看看,其中一部分能实现你想要的功能。
Zhymax 2005-07-06
  • 打赏
  • 举报
回复
CFileFind finder;
BOOL bWorking = finder.FindFile("*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
}
idAnts 2005-07-06
  • 打赏
  • 举报
回复

#include <windows.h>
#include <stdio.h>

WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];

BOOL fFinished = FALSE;

// Create a new directory.

if (!CreateDirectory(szDirPath, NULL))
{
printf("Couldn't create new directory.");
return;
}

// Start searching for .TXT files in the current directory.

hSearch = FindFirstFile("*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No files found.");
return;
}

// Copy each .TXT file to the new directory
// and change it to read only, if not already.

while (!fFinished)
{
printf(FileData.cFileName);

if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}

// Close the search handle.

FindClose(hSearch);


an_bachelor 2005-07-06
  • 打赏
  • 举报
回复
FindFirstFile FindNextFile FindClose

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧