C++实现遍历一个文件夹



#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
void FindFile(string strPath);
int main()
{
 string disk;
    string dir;
 cout<<"请输入文件夹所在盘:(如在D盘输入D或d)"<<endl;
 cin>>disk;
 cout<<"请输入文件夹名:"<<endl;
 cin>>dir;
 string strFilePath=disk+":\\"+dir+"\\";
 string strFindPath=strFilePath+"*.*";
 WIN32_FIND_DATA findData;
 HANDLE hFindFile=FindFirstFile(strFindPath.c_str(),&findData);
 if(INVALID_HANDLE_VALUE==hFindFile)
  printf("Error:%d\n",GetLastError());
 while(FindNextFile(hFindFile,&findData))
 {
  if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
  {
   if(findData.cFileName[0]=='.')
   {
    continue;
   }
   cout<<"在此文件夹 "<<" 找到文件夹: "<<findData.cFileName<<endl;
   string strNeedFindPath=strFilePath+findData.cFileName;
   FindFile(strNeedFindPath);
   
  }
  else
  {
   cout<<"在此文件夹 "<<" 找到文件: "<<findData.cFileName<<endl;
  }
  
 }
 FindClose(hFindFile);
 system("pause");
 return 0;
}

void FindFile(string strPath)
{
 string strFindPath=strPath+"\\*.*";
 WIN32_FIND_DATA findData;
 HANDLE hFindFile=FindFirstFile(strFindPath.c_str(),&findData);
 if(INVALID_HANDLE_VALUE==hFindFile)
  printf("Error:%d",GetLastError());
 while(FindNextFile(hFindFile,&findData))
 {
  if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
  {
   if(findData.cFileName[0]=='.')
   {
    continue;
   }
   cout<<"在 "<<strPath<<" 找到文件夹:"<<findData.cFileName<<endl;
   string strNeedFindPath=strPath+"\\"+findData.cFileName;
   FindFile(strNeedFindPath);
  }
  else
  {
   cout<<"在 "<<strPath<<" 找到文件:"<<findData.cFileName<<endl;
  }
  
 }
 FindClose(hFindFile);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值