Thursday, February 28, 2013

Traverse all the files under one folders using matlab and C++ on windows

I usually need to process batches of files in my work. sometimes use matlab, some times use C++:

1. For MATLAB, it is very easy:

clear;
clc;
close all;
MyDir='D:\Matlab_Code\RA_6\recent-valid\'; 
% the folder where the targeted files are stored, do not forget the '\' in the end;
AllFileName=dir([MyDir,'*.txt']);
% AllFileName it is a array whose each element is a structure with many attributes
% all the infomation about txt files would be included in AllFilename
% You can even get the numbers of files just by size the AllFileName without traversing all the files
for i=1:length(AllFileName)
     Fid=fopen([MyDir,AllFileName(i).name]); % there are many optional parameters  you can choose
     Content=fgetl(Fid);
     while ischar(Content)
          Str= Content;
          % add your code here
          Content=fgetl(Fid);% if you miss this, it could run into a dead loop
     end
     fclose(Fid);% never forget to close the file
 end



2. For C\C++ on windows, it is almost same:


#include 
#include 
#include 
using namespace std;
int main (void)
{

    _finddata_t fileDir; // it is included in io.h, much similar to the AllFileName in the matlab code

    char* dir="C:\\CZG\\Matlab_Code\\HSS\\*.m";

    // notice the end of the above line: 
    // if you write as  *.m, then only m file would be found
    // if you write as  *.m*, hen all the extention format start with m could be found, such as .mat, .m file could be found at the same time

     long lfDir;

    if((lfDir = _findfirst(dir,&fileDir))==-1l) // in case there is no m file found

       printf("No file is found\n"); 

    else{

           printf("file list:\n");

           do{

               printf("%s\n",fileDir.name);

             }

           while( _findnext( lfDir, &fileDir ) == 0 );// the common way to tranverse the files, and you can not get the number of files without tranversing files, which is different from matlab

        }

    _findclose(lfDir);  // still do not forget to close

    unsigned int i=fileDir.size;

    printf("%d\n",i);

    return 0;

}

 another method is that you can put all the file names to one txt files using system function, then you can access all the files one by one through reading this txt file.

#include <iostream>
#include  <string>
#include <fstream>
using namesapce std;
int main()
{
system("dir /a-d /b D:\\zhuanfa_singapore\\step_1\\*.* >D:\\zhuanfa_singapore\\step_2\\list.txt");
// all the file names under D:\\zhuanfa_singapore\\step_1 will be stored in
//D:\\zhuanfa_singapore\\step_2\\list.txt
my_in.open("D:\\zhuanfa_singapore\\step_2\\list.txt",ios::in);
string str2="D:\\zhuanfa_singapore\\step_1\\";
 while(!my_in.eof())
 {
     string str1;
     getline(my_in,str1);
     string str3=str2+str8;
     fstream _file;
     _file.open(str3.c_str(),ios::in);
       if(!_file)
      {
         cout<<str3<<"not exist!"<<endl;
      }
      else
      {
         cout<<str3<<"already exsit!"<<endl;

      }
 }
 my_in.close();
return 0;
}


C\C++ on linux seems a little different, i will record that in another post.

No comments:

Post a Comment