Solved

To mirror a Directory

Posted on 2000-03-28
9
193 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
[X]
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
  • 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

Independent Software Vendors: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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