File copy and file timestamp C++ Linux

Hi,

i am trying to write a function take takes in a time and retrieve files which matches the time stamp from a directory and copy them to another directory.

I am using C++ with Qt libraries in linux.
Can it be done using System() with shell commands? How can get System() to return me the list of files names that matches the timestamp?

Or how can i do it with Qt libraries?

thanks
jaxrpcAsked:
Who is Participating?
 
jkrCommented:
>>...what if i want to find files with a date range?

If you read the timestamps via 'stat()' as above, that's pretty straightfoward:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string>
#include <iostream>
using namespace std;

bool process_if_time_matches ( const string& sSrcDir, const string& sDstDir, const time_t tMatchMin,  const time_t tMatchMin) {

   DIR* pDir = opendir ( sSrcDir.c_str ());

   if ( !pDir) return false;

   dirent* pEntry;

   while ( pEntry = readdir ( pDir)) {

       // cout << "found " << pEntry->d_name << endl; // uncomment this line for diagnostic output

       if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

           continue; // skip subdirectories

       }

     struct stat buf;

     stat(pEntry->d_name,&buf);

     // Check if time stamp is within range <--------------------
     if (tMatchMin  <= buf.st_mtime && tMatchMax  >= buf.st_mtime) {

        // time last modified matches the time stamp, process file
     }

   }

   closedir( pDir);

   return true;
}

Open in new window

0
 
stefan73Commented:
You may want to use the GNU find command. Depending on the timestamp you need, check -anewer / -cnewer to get all files which are newer than the directory
0
 
stefan73Commented:
Or do you need a perfect match?

The "old-fashioned" way to do it would be opendir, readdir, closedir to read the directory, and stat() for each entry in the directory - and for the directory itself. That way, you can get all the timestamps and perform the needed operation.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
stefan73Commented:
Another alternative is using bash:
dir=some_directory
for file in $dir/*; do
  if [[ $file -nt $dir ]]; then
     cp $file $target_dir
  fi
done

-> For a perfect timestamp match, you can use a combination of -nt (newer than) and -ot (older than) if conditions.
0
 
itsmeandnobodyelseCommented:
>>> How can get System() to return me the list of files names that matches the timestamp?

  system("ls -a > xx.lst");
  ifstream ifs("xx.lst");
  string line;
  vector<string> files;
  while (getline(ifs, line))
       files.push_back(line);
  ifs.close();
  remove(xx.lst);
0
 
jkrCommented:
A little bit more the programming way using 'readdir()' (http://linux.die.net/man/3/readdir) and 'stat()' (http://linux.die.net/man/2/stat):
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string>
#include <iostream>
using namespace std;

bool process_if_time_matches ( const string& sSrcDir, const string& sDstDir, const time_t tMatch) {

   DIR* pDir = opendir ( sSrcDir.c_str ());

   if ( !pDir) return false;

   dirent* pEntry;

   while ( pEntry = readdir ( pDir)) {

       // cout << "found " << pEntry->d_name << endl; // uncomment this line for diagnostic output

       if ( DT_DIR & pEntry->d_type && strcmp ( pEntry->d_name, ".") && strcmp ( pEntry->d_name, "..")) {

           continue; // skip subdirectories

       }

     struct stat buf;

     stat(pEntry->d_name,&buf);

     if (tMatch == buf.st_mtime) {

        // time last modified matches the time stamp, process file
     }

   }

   closedir( pDir);

   return true;
}

Open in new window

0
 
jaxrpcAuthor Commented:
system("ls -a > xx.lst");
  ifstream ifs("xx.lst");
  string line;
  vector<string> files;
  while (getline(ifs, line))
       files.push_back(line);
  ifs.close();
  remove(xx.lst);

may i know what is lst? is it stored physically or in the memory?

0
 
itsmeandnobodyelseCommented:
the

   > xx.lst

would redirect the ouput of the ls command to a file named "xx.lst" in the current directory.

Then the xx.lst was opened using ifstream and the files were read from file and put into a vector.

finally the (temporary) xx.lst was deleted

   remove("xx.lst");   // I forgot to quote the string in the above sample

You also could make a grep after the ls like

   system("ls -la | grep \"Jul 22 2009\" > xx.lst");

or you do the grep when reading the files from file.

   system("ls -la > xx.lst");
   ifstream ifs("xx.lst");
   string line;
   vector<string> files;
   while (getline(ifs, line))
   {
       int pos = 0;
       if ((pos = line.find("Jul 22  2009")) != string::npos)
       {
            // extract filename
            line.erase(line.begin(), line.begin()+pos+13);  
            files.push_back(line);
        }
   }
0
 
irfan_omairCommented:
If you are using Qt, then using platform native api or code will not make it platform independent.
the best way to do is to use Qt and it is pretty easy . here is the folloing code which can guide you how to do it.
Let me know if this does not help.


#include <QtGui>

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QDir dir;
     dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
     dir.setSorting(QDir::Size | QDir::Reversed);

     QFileInfoList list = dir.entryInfoList();
   
     for (int i = 0; i < list.size(); ++i) {
         QFileInfo fileInfo = list.at(i);
             qDebug()<<fileInfo.size()<<fileInfo.fileName()<<fileInfo.lastModified();
             //if(fileInfo.lastModified()=="the given time stamp input")
            // .... code to copy and paste the file to desired destination
         
     }
       return app.exec();
 }
0
 
jaxrpcAuthor Commented:
hi guys,
sorry i forgot to add...what if i want to find files with a date range? lets say 5 july 2009 2pm to 06 july 2009 1am
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.