Recent

Author Topic: Copying a directory under Linux and Mac  (Read 7869 times)

bastla

  • New Member
  • *
  • Posts: 23
Copying a directory under Linux and Mac
« on: October 30, 2012, 04:31:36 pm »
Hi,

I need a function copying a whole directory working under Linux and Mac.
Unhappily I didn't find such a function in the LCL or another library; even Google could not help me :(

Under Windows I can do:
Code: [Select]
// uses ShellApi, Windows
function CopyDir(const fromDir, toDir: string): Boolean;
var
  fos: TSHFileOpStruct;
begin
  ZeroMemory(@fos, SizeOf(fos));
  with fos do
  begin
    wFunc  := FO_COPY;
    fFlags := FOF_FILESONLY;
    pFrom  := PChar(fromDir + #0);
    pTo    := PChar(toDir)
  end;
  Result := (0 = ShFileOperation(fos));
end;
(Source)

Does somebody has an idea how to copy a whole directory under Linux and Mac?
I would be really perfect if the function returns the progress in percent for a ProgressBar :).

Greetings in advance,
bastla

Edit: Thanks to Leledumbo, I found a way to get this working.
I posted my solution here :)
« Last Edit: April 30, 2013, 05:39:51 pm by bastla »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Copying a directory under Linux and Mac
« Reply #1 on: October 30, 2012, 04:50:50 pm »
Use TFileSearcher (I prefer extending the class and assign the events in the constructor, fill the events with directory creation and file copy) in combination with CreateDir (or its UTF8 equivalent in FileUtil) and CopyFile.

bastla

  • New Member
  • *
  • Posts: 23
Re: Copying a directory under Linux and Mac
« Reply #2 on: October 30, 2012, 05:06:25 pm »
Hi Leledumbo,

thank you very much for you answer!

The problem is, I don't know which files are contained in the directory; so I cannot use TFileSearcher, because you have to give the names of the files, don't you?

If it's either possible, can you please give me a short code example (I'm very new in Larzarus-Coding :D)

Thanks for your answer again and Greetings,
bastla

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Copying a directory under Linux and Mac
« Reply #3 on: October 30, 2012, 11:26:13 pm »
Maybe a look at the sources form my Stupid Backup Program can help?

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Copying a directory under Linux and Mac
« Reply #4 on: October 31, 2012, 10:01:43 am »
Quote
The problem is, I don't know which files are contained in the directory; so I cannot use TFileSearcher, because you have to give the names of the files, don't you?
No, you just have to specify the starting directory and file mask upon calling Search(), then assign OnFileFound and OnDirectoryFound events. Both events have parameter containing the file / directory being processed.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Copying a directory under Linux and Mac
« Reply #5 on: October 31, 2012, 03:51:31 pm »
I need a function copying a whole directory working under Linux and Mac.

There is such function in LCL, made by Takeda. It uses TFileSearcher. I can check the details later today when I get home.

Juha
« Last Edit: October 31, 2012, 03:54:40 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Takeda

  • Full Member
  • ***
  • Posts: 157
Re: Copying a directory under Linux and Mac
« Reply #6 on: October 31, 2012, 05:46:27 pm »
Hi,

I need a function copying a whole directory working under Linux and Mac.
Unhappily I didn't find such a function in the LCL or another library; even Google could not help me :(
..............

Hi, Bastla.
Well, do you have try to use "FileUtil.CopyDirTree" ?? (Available on Trunk of Lazarus)

I thought, It was suitable for Linux & *nix family, since I'm not put any "Windows-API Dependencies" in CopyDirTree, in short it would working properly in any supported Platform by Lazarus.

If you use, Lazarus Stable 1.0.x , unfortunately it's not yet available.

-Takeda-
« Last Edit: October 31, 2012, 05:49:11 pm by Takeda »
Call me Takeda coz that's my true name.
Pascal coding using Lazarus => "Be native in any where.."

ƪ(˘⌣˘)┐ ƪ(˘⌣˘)ʃ ┌(˘⌣˘)ʃ

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Copying a directory under Linux and Mac
« Reply #7 on: November 01, 2012, 12:59:23 am »
Quote
If you use, Lazarus Stable 1.0.x , unfortunately it's not yet available.
Which is why I can't find it in the current doc...

bastla

  • New Member
  • *
  • Posts: 23
Re: Copying a directory under Linux and Mac
« Reply #8 on: November 01, 2012, 05:17:51 pm »
WOW, thank you very much for all the replies!

I would like to use the methode mentioned by Leledumbo with TFileSearcher, because this seems to be the easiest one :D

How do I have to define the OnFileFound-Event? This does not work:
Code: [Select]
Search.OnFileFound := ShowMessage('File found:' + sLineBreak + Search.FileName);
@Takeda:
Where can I download Trunk of Lazarus?
I could not find any download-page... :o

@Bart:
Your backup-tool is very complex, I am confused by the many functions and procedures :S

Thank you for the replies again and Greetings,
bastla

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Copying a directory under Linux and Mac
« Reply #9 on: November 01, 2012, 05:58:12 pm »
Quote
How do I have to define the OnFileFound-Event? This does not work
You have to use procedural variable, e.g. (this is how I would do it):
Code: [Select]
type
  TDirCopier = class(TFileSearcher)
  private
    FToDir: String; // destination directory
    procedure CopyFile(FileIterator: TFileIterator);
    procedure CopyDir(FileIterator: TFileIterator);
  public
    constructor Create; override;
    procedure Copy(const FromDir,ToDir: String);
  end;

...

procedure TDirCopier.CopyFile(FileIterator: TFileIterator);
begin
  // copy the file here
end;

procedure TDirCopier.CopyDir(FileIterator: TFileIterator);
begin
  // create directory here
end;

constructor TDirCopier.Create; override;
begin
  inherited;
  OnFileFound := @CopyFile;
  OnDirectoryFound := @CopyDir;
end;

procedure TDirCopier.Copy(const FromDir,ToDir: String);
begin
  FToDir := IncludeTrailingPathDelimiter(ToDir);
  Search(FromDir,AllFilesMask);
end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Copying a directory under Linux and Mac
« Reply #10 on: November 01, 2012, 06:07:58 pm »
@Takeda:
Where can I download Trunk of Lazarus?
I could not find any download-page... :o
Via subversion.

However, if you're just beginning it may be easier to download a snapshot, which is generated every night (if the compilation works).
See the link on the left of the page.

Note: snapshots/trunk builds are the current development versions and may not be as stable as... ehmmm stable versions. However, they do contain the newest bug fixes and features.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

bastla

  • New Member
  • *
  • Posts: 23
Re: Copying a directory under Linux and Mac
« Reply #11 on: April 30, 2013, 05:39:05 pm »
Thanks to Leledumbo, I found a way to get this working.
I posted my solution here :)

Greetings,
bastla

 

TinyPortal © 2005-2018