C + + gets all file names (or paths) under the specified folder

Source: Internet
Author: User
Tags strcmp string format

  1. #include <io.h>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. Using namespace std;
  7. In fact, two strings together, such as a string unfortunately written: str1+str2+ "xxx" ...
  8. Get all the file names
  9. void Getallfiles (string path, vector<string>& files)
  10. {
  11. long hfile = 0;
  12. //File information
  13. struct _finddata_t fileinfo; The structure used to store file information
  14. string P;
  15. if (hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1) //First time lookup
  16. {
  17. Do
  18. {
  19. if ((Fileinfo.attrib & _a_subdir)) //If you find a folder
  20. {
  21. if (strcmp (Fileinfo.name,".")! = 0 && strcmp (fileinfo.name,"...")! = 0) //Enter folder to find
  22. {
  23. Files.push_back (p.assign (Path). Append ("\ \"). Append (Fileinfo.name));
  24. Getallfiles (p.assign (Path). Append ("\ \"). Append (Fileinfo.name), files);
  25. }
  26. }
  27. Else //If the find is not a folder
  28. {
  29. 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)
  30. }
  31. }while (_findnext (hfile, &fileinfo) = = 0);
  32. _findclose (hfile); //End lookup
  33. }
  34. }
  35. Get file names in a specific format
  36. void Getallformatfiles (string path, vector<string>& files,string format)
  37. {
  38. //File handle
  39. long hfile = 0;
  40. //File information
  41. struct _finddata_t fileinfo;
  42. string P;
  43. if (hfile = _findfirst (p.assign (Path). Append ("\\*" + format). C_STR (), &fileinfo))! =-1)
  44. {
  45. Do
  46. {
  47. if ((Fileinfo.attrib & _a_subdir))
  48. {
  49. if (strcmp (Fileinfo.name,".")! = 0 && strcmp (fileinfo.name,"...")! = 0)
  50. {
  51. //files.push_back (p.assign (Path). append ("\ \"). Append (Fileinfo.name));
  52. Getallformatfiles (p.assign (Path). Append ("\ \"). Append (Fileinfo.name), Files,format);
  53. }
  54. }
  55. Else
  56. {
  57. 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)
  58. }
  59. }while (_findnext (hfile, &fileinfo) = = 0);
  60. _findclose (hfile);
  61. }
  62. }
  63. The function has two parameters, the first is a path string (string type, preferably an absolute path);
  64. The second parameter is a folder with a file name that stores variables (vector type, reference pass).
  65. Call the format in the main function (and save the result in the file "AllFiles.txt", the total number of first actions):
  66. int main ()
  67. {
  68. String filePath = "d:\\ document \ \ \ course \ \ Data Structure \\MOOC practice \\testPCL\\x64\\Debug";
  69. vector<string> files;
  70. char * distall = "AllFiles.txt";
  71. //Read all files, including files of sub-files
  72. //getallfiles (FilePath, files);
  73. //Read all files in JPG format
  74. string format = ". dll";
  75. Getallformatfiles (FilePath, Files,format);
  76. Ofstream ofn (Distall);
  77. int size = Files.size ();
  78. ofn<<size<<endl;
  79. For (int i = 0;i<size;i++)
  80. {
  81. ofn<<files[i]<<endl; //write file
  82. cout<< Files[i] << Endl; //Output to screen
  83. }
  84. Ofn.close ();
  85. return 0;
  86. }

C + + gets all file names (or paths) under the specified folder

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.