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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

771 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