Solved

To mirror a Directory

Posted on 2000-03-28
9
186 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

679 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