| |

VerySource

 Forgot password?
 Register
Search
View: 1046|Reply: 8

Traversing a specific directory so that the files in it are processed separately?

[Copy link]

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-19 13:00:01
| Show all posts |Read mode
In C ++, how do I iterate through a specific directory so that the files in it are processed separately?
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-4-29 16:15:01
| Show all posts
findfirst / findnext, see the help of these 2 functions
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-29 19:30:01
| Show all posts
Excerpt from a piece of code on the Internet: very easy to understand
int SearchDirectory (std :: vector <std :: string>&refvecFiles,
                    const std :: string&refcstrRootDirectory,
                    const std :: string&refcstrExtension,
                    bool bSearchSubdirectories = true)
{
std :: string strFilePath; // Filepath
std :: string strPattern; // Pattern
std :: string strExtension; // Extension
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information


strPattern = refcstrRootDirectory + "\\*. *";

hFile = :: FindFirstFile (strPattern.c_str (),&FileInformation);
string temp (FileInformation.cFileName);
if (hFile! = INVALID_HANDLE_VALUE)
{
do
{
if (FileInformation.cFileName [0]! = '.')
{
strFilePath.erase ();
strFilePath = refcstrRootDirectory + "\\" + temp;

if (FileInformation.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if (bSearchSubdirectories)
{
// Search subdirectory
int iRC = SearchDirectory (refvecFiles,
                                      strFilePath,
                                      refcstrExtension,
                                      bSearchSubdirectories);
if (iRC)
return iRC;
}
}
else
{
// Check extension
strExtension = FileInformation.cFileName;
strExtension = strExtension.substr (strExtension.rfind (".") + 1);

if (strExtension == refcstrExtension)
{
// Save filename
refvecFiles.push_back (FileInformation.cFileName);
}
}
}
}
while (:: FindNextFile (hFile,&FileInformation) == TRUE);

    // Close handle
    :: FindClose (hFile);

    DWORD dwError = :: GetLastError ();
    if (dwError! = ERROR_NO_MORE_FILES)
return dwError;
}
return 0;
}
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-30 15:00:01
| Show all posts
Under the unix system, support opendir and readdir functions, you can use these two functions to write directory routines
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-5-1 15:00:01
| Show all posts
class Cbrowse
{
public:
Cbrowse ();
bool SetInitDir (char * szdir);

void BeginBrowse ();

DWORD dwfilenum;
DWORD dwdirnum;
char szSetInitDir [MAX_PATH];

protected:
bool BrowseDir (char * dir);
};

Cbrowse :: Cbrowse ()
{
dwfilenum = 0;
dwdirnum = 0;
}

bool Cbrowse :: SetInitDir (char * szdir)
{
if (chdir (szdir)! = 0)
{
return FALSE;
}
if (_fullpath (szSetInitDir, szdir, MAX_PATH)! = NULL)
{
int dwLenOfInitDir;
strcmp (szSetInitDir, szdir);
dwLenOfInitDir = strlen (szSetInitDir);
if (szSetInitDir [dwLenOfInitDir-1]! = '\\')
{
strcat (szSetInitDir, "\\*");
}
else
{
strcat (szSetInitDir, "*");
}
return TRUE;
}
return FALSE;
}

void Cbrowse :: BeginBrowse ()
{
Cbrowse :: BrowseDir (szSetInitDir);
}

bool Cbrowse :: BrowseDir (char * dir)
{
HANDLE hFile;
WIN32_FIND_DATAA FindData;
hFile = FindFirstFile (dir,&FindData);

if (hFile! = INVALID_HANDLE_VALUE)
{
do
{
if (FindData.dwFileAttributes! = FILE_ATTRIBUTE_DIRECTORY)
{
dwfilenum ++;
}
} while (FindNextFile (hFile,&FindData));
}

hFile = FindFirstFile (dir,&FindData);
if (hFile! = INVALID_HANDLE_VALUE)
{
do
{
if (FindData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp (FindData.cFileName, ".")! = 0&&strcmp (FindData.cFileName, "..")! = 0)
{
dwdirnum ++;
char szFullSubDir [MAX_PATH];
StrCpyN (szFullSubDir, dir, strlen (dir));
strcat (szFullSubDir, FindData.cFileName);

strcat (szFullSubDir, "\\*");
// Recursively traverse subdirectories
BrowseDir (szFullSubDir);
}
}
} while (FindNextFile (hFile,&FindData));
}
FindClose (hFile);
return TRUE;
}

The point of writing is not standardized,
Instead,
Personal opinion, expert advice
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-1 17:45:01
| Show all posts
#! / bin / sh
for file in `find.`
do
    echo $ file
done


shell version
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-5 08:15:01
| Show all posts
void SearchDir (CString dir)
{
   CFileFind file;
   BOOL bRet = FALSE;

   if ('\\'! = dir [dir.GetLength ()-1])
   {
         dir = dir + "\\*. *";
The
   }

   bRet = file.FindFile ((LPCTSTR) dir, 0);
   if (! bRet)
return;

   do
   {
bRet = file.FindNextFile ();

if (file.IsDots ())
{
}
else if (file.IsDirectory ())
{
// string strTemp = file.GetFilePath ();
SearchDir (file.GetFilePath ());
}
else
{
std :: cout << file.GetFilePath () << std :: endl;
}
   } while (bRet);
   
}
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-6 15:30:01
| Show all posts
Note the use of recursive thinking
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-6 17:00:01
| Show all posts
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <cstring>
#include <list>
#include <algorithm>
#include "windows.h"
#include "stdlib.h"
#include "Chdir.h"
#include "file.h"
//
std :: list <std :: string> dirs, files;
//
void path_list (const char * fname) {
using namespace std;
  Chdir set_path;
  set_path.cd (fname);
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind = INVALID_HANDLE_VALUE;
  DWORD dwError;
  // cout << "Target directory is" << path << ".\n";
  char aim [MAX_PATH];
  strncpy (aim, fname, strlen (fname) +1);
  strncat (aim, "\\*", 3);
  hFind = FindFirstFile (aim,&FindFileData);
  if (hFind == INVALID_HANDLE_VALUE)
  {
    cerr << "FindFirstFile (" << aim << ",&FindFileData) run error!\n";
    exit (GetLastError ());
  }
  else
  {
    // cout << "First file name is" << FindFileData.cFileName << "\n";
    char s [MAX_PATH];
    if (0! = strcmp (".", FindFileData.cFileName)) {
      _fullpath (s, FindFileData.cFileName, MAX_PATH);
files.push_back (s);
    }
    while (FindNextFile (hFind,&FindFileData)! = 0)
    {
      if (0 == strcmp ("..", FindFileData.cFileName)) continue;
      if (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {
      _fullpath (s, FindFileData.cFileName, MAX_PATH);
      dirs.push_back (s);
    }
    else {
    _fullpath (s, FindFileData.cFileName, MAX_PATH);
      files.push_back (s);
    }
    }
    dwError = GetLastError ();
    FindClose (hFind);
    if (dwError! = ERROR_NO_MORE_FILES)
    {
      cerr << "FindNextFile error!\n";
      exit (dwError);
    }
  }
}
//
void full_path_list (const char * fname) {
using namespace std;
  Chdir set_path;
  set_path.cd (fname);
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind = INVALID_HANDLE_VALUE;
  DWORD dwError;
  // cout << "Target directory is" << path << ".\n";
  char aim [MAX_PATH];
  strncpy (aim, fname, strlen (fname) +1);
  strncat (aim, "\\*", 3);
  hFind = FindFirstFile (aim,&FindFileData);
  if (hFind == INVALID_HANDLE_VALUE)
  {
    cerr << "FindFirstFile (" << aim << ",&FindFileData) run error!\n";
    exit (GetLastError ());
  }
  else
  {
    // cout << "First file name is" << FindFileData.cFileName << "\n";
    char s [MAX_PATH];
    if (0! = strcmp (".", FindFileData.cFileName)) {
      _fullpath (s, FindFileData.cFileName, MAX_PATH);
files.push_back (s);
    }
    while (FindNextFile (hFind,&FindFileData)! = 0)
    {
      if (0 == strcmp ("..", FindFileData.cFileName)) continue;
      if (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {
      _fullpath (s, FindFileData.cFileName, MAX_PATH);
      dirs.push_back (s);
      full_path_list (s);
    }
    else {
    _fullpath (s, FindFileData.cFileName, MAX_PATH);
      files.push_back (s);
    }
    }
    dwError = GetLastError ();
    FindClose (hFind);
    if (dwError! = ERROR_NO_MORE_FILES)
    {
      cerr << "FindNextFile error!\n";
      exit (dwError);
    }
  }
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list