Forum > Third party

Copying an entire directory (incl. subdirectories)

(1/9) > >>

bastla:
Hello together,

I searched for a possiblity to copy a whole directory on every platform supported by Lazarus. Thanks to Leledumbo, I found a way to get this working. Maybe there're some guys outside who still search for this, so I publish my work here :D.

I created an simple and easy class, called "TCopyDir", which uses TFileSearcher to enumerate an entire directory and copy its content file by file.

Example Code:

--- Code: ---procedure TfrmMain.btCopyClick(Sender: TObject);
var
  CopyDir: TCopyDir;
  Log: TStringList;
begin
  CopyDir := TCopyDir.Create('C:\', 'D:\');
  CopyDir.Start;

  Log := CopyDir.GetLog;

  CopyDir.Free;
end;
--- End code ---

As you can see, the class is very simple to use and contains a log (TStringList). In this example, the whole content of "C:\" would be copied to "D:\".

[EDIT 2013-06-23]As of 23.06.2013, there's a improved version of TCopyDir (called TCopyDir v2). For more details see here[/EDIT]
[EDIT 2013-08-10]onivan found a bug in TCopyDir and has already created a fix. Make sure to read his post.[/EDIT]
[EDIT 2014-01-06]Caladan found another bug in TCopyDir affecting Unix users only. See this post for a fix![/EDIT]
[EDIT2014-06-03]I uploaded version 2.1 of TCopyDir which fixes two above mentioned bugs, plus I changed the disclaimer![/EDIT]

Download and example application in the attachments.
My work is Free Domain but remain the terms of LCL!

With the use of this work you agree to the following:
THIS SOFTWARE IS PROVIDED BY BASTLA "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BASTLA BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


I hope this is useful for some of you!

Greetings,
bastla

jmpessoa:
Thank you bastla!

exdatis:
Thank you very much!

LA.Center:
Nice - THX a bunch

CM630:
I have two questions:
1. How does it handle hidden and system files?
2. Is it tested with files with nonlatin filenames? Edit- I tested, works fine for me.

Edit: I found a bug- I copied a folder, which contained a hidden file. It copied the file, but it the file in the target directory lost its "Hidden" flag.

Edit: yet another bug- in the folder I had two subfolder. One of them had a name, ending with .txt and it was not copied. The other was was copied fine.

Edit: I made a third folder. It does not copy it, too. It does not copy more than 1 subfolder.

I did the tests using the example app. There is a bug in the example app, too- ledSource.ReadOnly is set to TRUE, this way users cannot paste the folder path there, but they have to browse the whole way to the folders.

Navigation

[0] Message Index

[#] Next page

Go to full version