Link to home
Start Free TrialLog in
Avatar of aflat362
aflat362

asked on

Move Files older than x minutes to another directory

Hello,

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.

Thanks!
Avatar of mnashadka
mnashadka

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;
    do
    {
      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);
  }
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aflat362

ASKER

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