This is what I write small programs to traverse the directory, specially that come out and share.
// need several header files
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
/**
* Features: realize the depth traverse directories and files
* Parameters: char *, it means that the incoming is a path to traverse start
* Return type: void
**/
void FindFileAndDirectory(char * path)
{
char FilePath[MAX_PATH] = {0};
WIN32_FIND_DATA FindData;
HANDLE FindHandle;
ZeroMemory(&FindData,sizeof(WIN32_FIND_DATA));
strcpy(FilePath,path);
if (FilePath[strlen(FilePath) - 1] != '\\')
{
strcat(FilePath,"\\");
}
strcat(FilePath,"*");
FindHandle = FindFirstFile(FilePath,&FindData);
if (FindHandle == INVALID_HANDLE_VALUE)
{
cout<<"查找失败,程序退出!"<<endl;
return;
}
do
{
if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& strcmp(FindData.cFileName,".")
&& strcmp(FindData.cFileName,".."))
{
char TempPath[MAX_PATH] = {0};
strcpy(TempPath,path);
if (TempPath[strlen(TempPath) - 1] != '\\')
{
strcat(TempPath,"\\");
}
strcat(TempPath,FindData.cFileName);
cout<<"The Path Is: "<<TempPath<<endl;
FindFileAndDirectory(TempPath);
}
else if (strcmp(FindData.cFileName,".") && strcmp(FindData.cFileName,".."))
{
char TempPath[MAX_PATH] = {0};
strcpy(TempPath,path);
TempPath[strlen(TempPath) - 1] = 0;
strcat(TempPath,FindData.cFileName);
cout<<"The File Is: "<<TempPath<<endl;
}
} while(FindNextFile(FindHandle,&FindData));
FindClose(FindHandle);
}
When an incoming file back to the path it traversed all the folders and files. We are interested can discuss.
This is new version of the c ++
#include<iostream>
#include<io.h>
using namespace std;
#define BUF_SIZE 256
void findfileanddirectory(char * path)
{
_finddata_t filedata;
long lf;
char lpath[256] = {0};
strcpy(lpath,path);
if (lpath[strlen(lpath) - 1] != '\\')
{
strcat(lpath,"\\");
}
strcat(lpath,"*.*");
lf = _findfirst(lpath,&filedata);
if (lf == -1)
{
cout<<"查找文件失败,返回!"<<endl;
return;
}
do
{
if ((filedata.attrib & _A_SUBDIR)&&strcmp(filedata.name,".") && strcmp(filedata.name,".."))
{
char bufpath[BUF_SIZE] = {0};
strcpy(bufpath,path);
if (bufpath[strlen(bufpath) - 1] != '\\')
{
strcat(bufpath,"\\");
}
strcat(bufpath,filedata.name);
cout<<"The Path Is: "<<bufpath<<endl;
findfileanddirectory(bufpath);
}
else if (strcmp(filedata.name,".") && strcmp(filedata.name,".."))
{
char bufpath[BUF_SIZE] = {0};
strcpy(bufpath,path);
if (bufpath[strlen(bufpath) - 1] != '\\')
{
strcat(bufpath,"\\");
}
strcat(bufpath,filedata.name);
cout<<"The Path Is: "<<bufpath<<endl;
}
} while(_findnext(lf,&filedata) == 0);
_findclose(lf);
}