Solved

To mirror a Directory

Posted on 2000-03-28
9
182 Views
Last Modified: 2010-04-02
I need a program in visual C++ to mirror a directory both full and incremental to another directory.
Mirror means to copy the files. It works fine with just
copying the files but I want to delete the files that does not
match with the parent dir in the target dir.
also it should be able to do incremental mirror ie files that are
different from the target dir.
0
Comment
Question by:Barry01
  • 5
  • 3
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2665261
What does "mirror" mean?  
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2665391
I don't understand, too, about mirror,
but may be API ShFileOperation, that
can copy/delete/move/rename all directory, that is, what you want.
Alex
0
 

Author Comment

by:Barry01
ID: 2665459
Edited text of question.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2665558
Please don't edit the question once a "dailog" has started  It makes it hard to follow the question.   Just post new information in comments.

You can use FindFirstFile() and FindNextFile() to enumare each of the files in the source directory.  for each file found you can copy it to the destination directory using CopyFile()  If you specify TRUE fopr the last parameter, the file won't be copied if it exists.

example follows.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2665580
  BOOL  Done;                     // Done searching for files?
   HANDLE  FndHnd = NULL;   // Handle to find data.
   WIN32_FIND_DATA FindDat;  // Info on file found.
   string SrcDir = "C:\\SOURCE\\";
   string DstDir = "C:\\DEST\\";
   
   FndHnd = FindFirstFile(SrcDir.c_str(), &FindDat);
   Done = (FndHnd == INVALID_HANDLE_VALUE);       // Indicate we are done if nothing was found.

   while (!Done)
   {
      string SrcNam = SrcDir + FindDat.cFileName;
      string DstNam = DstDir + FindDat.cFileName;
      CopyFile(SrcNam.c_str(),DstNam.c_str(),TRUE);
      Done = !FindNextFile(FndHnd, &FindDat);
   }
   if (FndHnd)            // If there was anything found, then
      FindClose(FndHnd); // Close the find handle

Let me know if you have any questions.
0
 

Author Comment

by:Barry01
ID: 2665714
will it also work if the file is same but modified since the last mirror is executed. and can we delete the files that are not in the source directory but are in the target directory
0
 
LVL 22

Expert Comment

by:nietod
ID: 2669213
If you want to copy modified fiels, use GetFileTime() to get the times for the source and destination files.  If the source files has a more recent last write time than the destination file, then you want to copy it.  You will have to specify FALSE in the last parameter to CopyFile() in order to make the copy work in this case.


You can use the same sort of logic to delete files in the destiantion directory.  enumerate all the files n the destination directory and for each file, check to see if it is in the source directory.  (A quick way to make the check is to use GetfileAttributes().)  If the file is not in the source directory, delete it from the destination directory using DeleteFile.()
0
 

Author Comment

by:Barry01
ID: 2670105
Can we have list of all the files that are not in the source directory but are in the target directory and then have the option to delete some of the files from the list leaving others as it is.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2670551
yes.  You would have to create the list using FindFirstFile()/FindNextFile().  Then display it to the user in some way using a list control or some sort of custom control.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now