#include "stdafx.h"
#include "Windows.h"
#include <vector>
#include <string>
#include "iostream"
using namespace Std;
typedef std::vector<std::string> File_lists;
static int str_compare (const void *ARG1, const void *ARG2)
{
Compare strings Arg1 and arg2
Return strcmp ((* (std::string*) arg1). C_str (), (* (std::string*) arg2). C_STR ());
}
File_lists scandirectory (const std::string &path, const std::string &extension)
{
Win32_find_data:contains information about the file, which is found
By the Findfirstfile,findfirstfileex, or FindNextFile function
Win32_find_data WFD;
HANDLE Hhandle;
String SearchPath, Searchfile;
File_lists Vfilenames;
int nbfiles = 0;
SearchPath = path + "/*" + extension;
Searches a directory for a file or subdirectory with a name that matches a specific name
Hhandle = FindFirstFile (Searchpath.c_str (), &WFD);
if (Invalid_handle_value = = Hhandle)
{
fprintf (stderr, "ERROR (%s,%d): Cannot find (*.%s) files in directory%s/n", __file__, __line__, Extension.c_str (), path.c_ STR ());
Exit (0);
}
Do
{
//. Or..
if (wfd.cfilename[0] = = '. ')
{
Continue
}
if exists sub-directory
Dwfileattributes:the file attributes of a file
if (Wfd.dwfileattributes & File_attribute_directory)
{
File_attribute_directory:the handle identifies a DIRECTORY
Continue
}
else//if file
{
Searchfile = path + "/" + wfd.cFileName;
Vfilenames.push_back (Searchfile);
nbfiles++;
}
}while (FindNextFile (Hhandle, &WFD));
Call the member function to continue a file search begun with a call to CGopherFileFind::FindFile
FindClose (Hhandle);
Closes a file search handle opened by the Findfirstfile,findfirstfileex,or FINDFIRSTSTREAMW function
Sort the filenames,performs a quick sort
Qsort (void *) & (Vfilenames[0]), (size_t) nbfiles, sizeof (string), str_compare);
return vfilenames;
}
int main (int argc, char* argv[])
{
file_lists files = scandirectory ("E:", ". jpg");
if (Files.empty ())
{
cout<< "No image file Find in current directory ..." <<endl;
System ("pause");
Exit (-1);
}
int size = Files.size ();
cout<< "There is" <<size<< "image files totally ...." <<endl;
for (int i=0; i<size; i++)
{
Cout<<files[i].c_str () <<endl;
}
System ("pause");
return 0;
}
The above is compiled in the VS2010 environment, run will look for all the files with the suffix. jpg in the e:/directory and print them out
#include "stdafx.h" is a precompiled software for Visual Studio Tools. If you are not using the VS tool, or if you do not have the precompilation option set in VS, you do not need this line.
second, in the project properties of VS, do not use Unicode characters and change to no set.
The following is a function of traversing the read file directory in MATLAB
function fileList = Readfilelistfromfolder (FolderName, Filesuffix)
% Read all the files with special suffix to a file
% list.
% A Simple use case:
% FileList = Readfilelistfromfolder (' test\ ', ' txt ');
%check the input FolderName
if ~isempty (findstr (FolderName, "))
Disp (' There is some space in the folder name: ');
Disp ([' folderName]);
Disp (' version2 instead of this one! ');
End
if strcmp (folderName (end), ' \ ')
[S, W] = DOS ([' dir ' folderName ' *. ') Filesuffix '/s/b >filelist.txt ']);
Else
[S, W] = DOS ([' dir ' folderName ' \*. ') Filesuffix '/s/b >filelist.txt ']);
End
FileList = ImportData (' fileList.txt ');
DOS (' del fileList.txt ');
End
Windows traversal reads files under the specified file directory