- #include <io.h>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <iostream>
- Using namespace std;
- In fact, two strings together, such as a string unfortunately written: str1+str2+ "xxx" ...
- Get all the file names
- void Getallfiles (string path, vector<string>& files)
- {
- long hfile = 0;
- //File information
- struct _finddata_t fileinfo; The structure used to store file information
- string P;
- if (hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1) //First time lookup
- {
- Do
- {
- if ((Fileinfo.attrib & _a_subdir)) //If you find a folder
- {
- if (strcmp (Fileinfo.name,".")! = 0 && strcmp (fileinfo.name,"...")! = 0) //Enter folder to find
- {
- Files.push_back (p.assign (Path). Append ("\ \"). Append (Fileinfo.name));
- Getallfiles (p.assign (Path). Append ("\ \"). Append (Fileinfo.name), files);
- }
- }
- Else //If the find is not a folder
- {
- Files.push_back (P.assign (fileinfo.name)); //Save the file path, or you can save only the filename: p.assign (path). append ("\ \"). Append (fileinfo.name)
- }
- }while (_findnext (hfile, &fileinfo) = = 0);
- _findclose (hfile); //End lookup
- }
- }
- Get file names in a specific format
- void Getallformatfiles (string path, vector<string>& files,string format)
- {
- //File handle
- long hfile = 0;
- //File information
- struct _finddata_t fileinfo;
- string P;
- if (hfile = _findfirst (p.assign (Path). Append ("\\*" + format). C_STR (), &fileinfo))! =-1)
- {
- Do
- {
- if ((Fileinfo.attrib & _a_subdir))
- {
- if (strcmp (Fileinfo.name,".")! = 0 && strcmp (fileinfo.name,"...")! = 0)
- {
- //files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
- Getallformatfiles (p.assign (Path). Append ("\ \"). Append (Fileinfo.name), Files,format);
- }
- }
- Else
- {
- Files.push_back (P.assign (fileinfo.name)); //Save the file path, or you can save only the filename: p.assign (path). append ("\ \"). Append (fileinfo.name)
- }
- }while (_findnext (hfile, &fileinfo) = = 0);
- _findclose (hfile);
- }
- }
- The function has two parameters, the first is a path string (string type, preferably an absolute path);
- The second parameter is a folder with a file name that stores variables (vector type, reference pass).
- Call the format in the main function (and save the result in the file "AllFiles.txt", the total number of first actions):
- int main ()
- {
- String filePath = "d:\\ document \ \ \ course \ \ Data Structure \\MOOC practice \\testPCL\\x64\\Debug";
- vector<string> files;
- char * distall = "AllFiles.txt";
- //Read all files, including files of sub-files
- //getallfiles (FilePath, files);
- //Read all files in JPG format
- string format = ". dll";
- Getallformatfiles (FilePath, Files,format);
- Ofstream ofn (Distall);
- int size = Files.size ();
- ofn<<size<<endl;
- For (int i = 0;i<size;i++)
- {
- ofn<<files[i]<<endl; //write file
- cout<< Files[i] << Endl; //Output to screen
- }
- Ofn.close ();
- return 0;
- }
C + + gets all file names (or paths) under the specified folder