Solved

To mirror a Directory

Posted on 2000-03-28
9
180 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
Comment Utility
What does "mirror" mean?  
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
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
Comment Utility
Edited text of question.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 22

Expert Comment

by:nietod
Comment Utility
  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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
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.

728 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

10 Experts available now in Live!

Get 1:1 Help Now