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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.