C++ simple implementation instance of traversing files under directory
- 2020-05-12 02:57:29
- OfStack
C++ traverses the files in the directory
function: traverses through all files in the directory, returning the total number of files, the total number of subfolders (modify 1 to get all file names, etc.).
Example code:
#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "io.h"
#include "stdio.h"
#include "iostream"
using namespace std;
class CBrowseDir
{
protected:
// Store the absolute path to the initial directory '\' At the end
char m_szInitDir[_MAX_PATH];
public:
// Default constructor
CBrowseDir();
// Set the initial directory to dir , if I return false , indicating that the directory is not available
bool SetInitDir(const char *dir);
// Start traversing the initial directory and its subdirectories by filespec Specifies the type of file
//filespec You can use wildcards * ? , cannot contain a path.
// If the return false , indicating that the traversal process was aborted by the user
bool BeginBrowse(const char *filespec);
protected:
// Directory traversal dir by filespec Specified file
// For subdirectories , Take an iterative approach
// If the return false, Stands for stop traversing the file
bool BrowseDir(const char *dir,const char *filespec);
// function BrowseDir For each 1 A file , Just call ProcessFile
// And pass the file name as a parameter
// If the return false, Stands for stop traversing the file
// The user can override this function , Add your own processing code
virtual bool ProcessFile(const char *filename);
// function BrowseDir Each into the 1 A directory , Just call ProcessDir
// And put the name of the directory you are working on 1 The level directory name is passed as a parameter
// If you're dealing with the initial directory , the parentdir=NULL
// The user can override this function , Add your own processing code
// For example, the user can count the number of subdirectories here
virtual void ProcessDir(const char *currentdir,const char *parentdir);
};
CBrowseDir::CBrowseDir()
{
// Class with the current directory m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);
// If at the end of the directory 1 Letter is not '\', I'm going to add it at the end 1 a '\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
}
bool CBrowseDir::SetInitDir(const char *dir)
{
// The first dir Convert to an absolute path
if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
return false;
// Determine if the directory exists
if (_chdir(m_szInitDir) != 0)
return false;
// If at the end of the directory 1 Letter is not '\', I'm going to add it at the end 1 a '\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
return true;
}
bool CBrowseDir::BeginBrowse(const char *filespec)
{
ProcessDir(m_szInitDir,NULL);
return BrowseDir(m_szInitDir,filespec);
}
bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)
{
_chdir(dir);
// In the first place to find dir Documents that meet the requirements
long hFile;
_finddata_t fileinfo;
if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is not , I'm going to process
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
cout << filename << endl;
if (!ProcessFile(filename))
return false;
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
// To find the dir Subdirectories in
// Because you're dealing with dir When a file is derived from a class ProcessFile It may have changed
// The current directory, so also reset the current directory to dir .
// Carried out _findfirst After that, the system may have recorded the relevant information and therefore changed the directory
// right _findnext No effect.
_chdir(dir);
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is , Check again . or ..
// If it is not , To iterate
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp
(fileinfo.name,"..") != 0)
{
char subdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"\\");
ProcessDir(subdir,dir);
if (!BrowseDir(subdir,filespec))
return false;
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return true;
}
bool CBrowseDir::ProcessFile(const char *filename)
{
return true;
}
void CBrowseDir::ProcessDir(const char
*currentdir,const char *parentdir)
{
}
// from CBrowseDir A subclass used to count the number of files and subdirectories in a directory
class CStatDir:public CBrowseDir
{
protected:
int m_nFileCount; // Number of saved files
int m_nSubdirCount; // Save the number of subdirectories
public:
// Default constructor
CStatDir()
{
// Initializes the data member m_nFileCount and m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}
// Number of files returned
int GetFileCount()
{
return m_nFileCount;
}
// Returns the number of subdirectories
int GetSubdirCount()
{
// Because when you go into the initial directory, you call the function ProcessDir .
// So cut 1 After is the true number of subdirectories.
return m_nSubdirCount-1;
}
protected:
// Override the virtual function ProcessFile Per call, 1 Time, the number of files plus 1
virtual bool ProcessFile(const char *filename)
{
m_nFileCount++;
return CBrowseDir::ProcessFile(filename);
}
// Override the virtual function ProcessDir Per call, 1 Second, the number of subdirectories plus 1
virtual void ProcessDir
(const char *currentdir,const char *parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};
void main()
{
// Get directory name
char buf[256];
printf(" Please enter the directory name to be counted :");
gets(buf);
// Construct class object
CStatDir statdir;
// Sets the directory to traverse
if (!statdir.SetInitDir(buf))
{
puts(" The directory does not exist. ");
return;
}
// To traverse the
statdir.BeginBrowse("*.*");
printf(" The total number of files : %d\n Total number of subdirectories :%d\n",statdir.GetFileCount(),statdir.GetSubdirCount());
}
It has been validated on windows.
Now I've added the BeginBrowseFilenames function, vector
<
char*
>
Returns all file names in the directory.
Example code:
#include "stdlib.h"
#include "direct.h"
#include "string.h"
#include "string"
#include "io.h"
#include "stdio.h"
#include <vector>
#include "iostream"
using namespace std;
class CBrowseDir
{
protected:
// Store the absolute path to the initial directory '\' At the end
char m_szInitDir[_MAX_PATH];
public:
// Default constructor
CBrowseDir();
// Set the initial directory to dir , if I return false , indicating that the directory is not available
bool SetInitDir(const char *dir);
// Start traversing the initial directory and its subdirectories by filespec Specifies the type of file
//filespec You can use wildcards * ? , cannot contain a path.
// If the return false , indicating that the traversal process was aborted by the user
bool BeginBrowse(const char *filespec);
vector<string> BeginBrowseFilenames(const char *filespec);
protected:
// Directory traversal dir by filespec Specified file
// For subdirectories , Take an iterative approach
// If the return false, Stands for stop traversing the file
bool BrowseDir(const char *dir,const char *filespec);
vector<string> GetDirFilenames(const char *dir,const char *filespec);
// function BrowseDir For each 1 A file , Just call ProcessFile
// And pass the file name as a parameter
// If the return false, Stands for stop traversing the file
// The user can override this function , Add your own processing code
virtual bool ProcessFile(const char *filename);
// function BrowseDir Each into the 1 A directory , Just call ProcessDir
// And put the name of the directory you are working on 1 The level directory name is passed as a parameter
// If you're dealing with the initial directory , the parentdir=NULL
// The user can override this function , Add your own processing code
// For example, the user can count the number of subdirectories here
virtual void ProcessDir(const char *currentdir,const char *parentdir);
};
CBrowseDir::CBrowseDir()
{
// Class with the current directory m_szInitDir
getcwd(m_szInitDir,_MAX_PATH);
// If at the end of the directory 1 Letter is not '\', I'm going to add it at the end 1 a '\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
}
bool CBrowseDir::SetInitDir(const char *dir)
{
// The first dir Convert to an absolute path
if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
return false;
// Determine if the directory exists
if (_chdir(m_szInitDir) != 0)
return false;
// If at the end of the directory 1 Letter is not '\', I'm going to add it at the end 1 a '\'
int len=strlen(m_szInitDir);
if (m_szInitDir[len-1] != '\\')
strcat(m_szInitDir,"\\");
return true;
}
vector<string> CBrowseDir::BeginBrowseFilenames(const char *filespec)
{
ProcessDir(m_szInitDir,NULL);
return GetDirFilenames(m_szInitDir,filespec);
}
bool CBrowseDir::BeginBrowse(const char *filespec)
{
ProcessDir(m_szInitDir,NULL);
return BrowseDir(m_szInitDir,filespec);
}
bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)
{
_chdir(dir);
// In the first place to find dir Documents that meet the requirements
long hFile;
_finddata_t fileinfo;
if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is not , I'm going to process
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
cout << filename << endl;
if (!ProcessFile(filename))
return false;
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
// To find the dir Subdirectories in
// Because you're dealing with dir When a file is derived from a class ProcessFile It may have changed
// The current directory, so also reset the current directory to dir .
// Carried out _findfirst After that, the system may have recorded the relevant information and therefore changed the directory
// right _findnext No effect.
_chdir(dir);
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is , Check again . or ..
// If it is not , To iterate
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp
(fileinfo.name,"..") != 0)
{
char subdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"\\");
ProcessDir(subdir,dir);
if (!BrowseDir(subdir,filespec))
return false;
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return true;
}
vector<string> CBrowseDir::GetDirFilenames(const char *dir,const char *filespec)
{
_chdir(dir);
vector<string>filename_vector;
filename_vector.clear();
// In the first place to find dir Documents that meet the requirements
long hFile;
_finddata_t fileinfo;
if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is not , I'm going to process
if (!(fileinfo.attrib & _A_SUBDIR))
{
char filename[_MAX_PATH];
strcpy(filename,dir);
strcat(filename,fileinfo.name);
filename_vector.push_back(filename);
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
// To find the dir Subdirectories in
// Because you're dealing with dir When a file is derived from a class ProcessFile It may have changed
// The current directory, so also reset the current directory to dir .
// Carried out _findfirst After that, the system may have recorded the relevant information and therefore changed the directory
// right _findnext No effect.
_chdir(dir);
if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
{
do
{
// Check if it's a directory
// If it is , Check again . or ..
// If it is not , To iterate
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp
(fileinfo.name,"..") != 0)
{
char subdir[_MAX_PATH];
strcpy(subdir,dir);
strcat(subdir,fileinfo.name);
strcat(subdir,"\\");
ProcessDir(subdir,dir);
vector<string>tmp= GetDirFilenames(subdir,filespec);
for (vector<string>::iterator it=tmp.begin();it<tmp.end();it++)
{
filename_vector.push_back(*it);
}
}
}
} while (_findnext(hFile,&fileinfo) == 0);
_findclose(hFile);
}
return filename_vector;
}
bool CBrowseDir::ProcessFile(const char *filename)
{
return true;
}
void CBrowseDir::ProcessDir(const char
*currentdir,const char *parentdir)
{
}
// from CBrowseDir A subclass used to count the number of files and subdirectories in a directory
class CStatDir:public CBrowseDir
{
protected:
int m_nFileCount; // Number of saved files
int m_nSubdirCount; // Save the number of subdirectories
public:
// Default constructor
CStatDir()
{
// Initializes the data member m_nFileCount and m_nSubdirCount
m_nFileCount=m_nSubdirCount=0;
}
// Number of files returned
int GetFileCount()
{
return m_nFileCount;
}
// Returns the number of subdirectories
int GetSubdirCount()
{
// Because when you go into the initial directory, you call the function ProcessDir .
// So cut 1 After is the true number of subdirectories.
return m_nSubdirCount-1;
}
protected:
// Override the virtual function ProcessFile Per call, 1 Time, the number of files plus 1
virtual bool ProcessFile(const char *filename)
{
m_nFileCount++;
return CBrowseDir::ProcessFile(filename);
}
// Override the virtual function ProcessDir Per call, 1 Second, the number of subdirectories plus 1
virtual void ProcessDir
(const char *currentdir,const char *parentdir)
{
m_nSubdirCount++;
CBrowseDir::ProcessDir(currentdir,parentdir);
}
};
void main()
{
// Get directory name
char buf[256];
printf(" Please enter the directory name to be counted :");
gets(buf);
// Construct class object
CStatDir statdir;
// Sets the directory to traverse
if (!statdir.SetInitDir(buf))
{
puts(" The directory does not exist. ");
return;
}
// To traverse the
vector<string>file_vec = statdir.BeginBrowseFilenames("*.*");
for(vector<string>::const_iterator it = file_vec.begin(); it < file_vec.end(); ++it)
std::cout<<*it<<std::endl;
printf(" The total number of files : %d\n",file_vec.size());
system("pause");
}
Thank you for reading, I hope to help you, thank you for your support of this site!