Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Move Files older than x minutes to another directory

Posted on 2003-02-27
Medium Priority
Last Modified: 2012-06-27

I haven't touched C++ in a while and I'd like to get back into it.  I'm now using VC++ 6.0 (a new compiler to me).

I would like to know how I would move all files in directory A to directory B (windows 2000 computer - program run locally) that are older than 5 minutes.

Question by:aflat362
  • 2

Expert Comment

ID: 8035140
If you're using MFC, you can use the CFileFind class to find the files, then the CTime class to compare the time, and then MoveFileEx to move the file, like:
  CFileFind ff;
  CTime start_time = CTime::GetCurrentTime(); // Get the start time
  if(ff.FindFile("c:\\*.*")) // Find the files under C
    BOOL result;
      result = ff.FindNextFile();

      CTime creation_time;
      ff.GetCreationTime(creation_time); // Get the creation time
      if((start_time - creation_time).GetTotalMinutes() > 5) // If the difference is over 5 minutes
        CString new_file_name = "c:\\backup\\" + ff.GetFileName();
        MoveFileEx(ff.GetFilePath(), new_file_name, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING); // Move the file

    } while(result);

Accepted Solution

mnashadka earned 150 total points
ID: 8035157
If you're not using MFC, the FindFirstFile and FindNextFile will be the same.  Then you use the ftCreationTime data member to compare against the current time.  It's not quite as easy, but pretty close.  The struction would look the same, except that the FindNextFile stuff would happen after you compared the FindFirstFile stuff, whereas with the MFC version you need to call FindNextFile before using any of the data members.  Good luck.

Author Comment

ID: 8035855
I guess I don't really know what MFC is.  All I know is i'm using visual C++ 6 and I created a new console application and have one .cpp source code file.  Sorry - Like I said VC++ 6 is a new tool to me.  I understand what your code is doing for the most part and I might be able to figure the problem out from that

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question