Solved

To mirror a Directory

Posted on 2000-03-28
9
184 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

809 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