Recent

Author Topic: Converting "Copy Folder Test" in DelphiForFun.org  (Read 9482 times)

tjpren

  • Jr. Member
  • **
  • Posts: 67
Converting "Copy Folder Test" in DelphiForFun.org
« on: December 28, 2011, 01:30:42 pm »
Hello,

I"ve been googling for code snippets to help me get started on a new program - to copy files and directories from my HD to a backup USB.

I noticed that the forum had mentioned DelphiForFun.org, and I found a brilliant example of what I wanted - Copy Folder Test (located in the Utilities section).

Unfortunately, I've not been successful converting it, and was wondering I anyone else had done the "hard yards".

I've been looking through the posts and noticed a number of methods for conversion - I've tried the basic: Tools>>Convert Delphi Project.

It gets as far as an error saying:
"At this point there should be no missing units"
Continuing, results in the .dpr in the Source Editor only.

Thought?

Regards

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #1 on: December 28, 2011, 02:54:31 pm »
I"ve been googling for code snippets to help me get started on a new program - to copy files and directories from my HD to a backup USB.
I noticed that the forum had mentioned DelphiForFun.org, and I found a brilliant example of what I wanted - Copy Folder Test (located in the Utilities section).
Unfortunately, I've not been successful converting it, and was wondering I anyone else had done the "hard yards".
I've been looking through the posts and noticed a number of methods for conversion - I've tried the basic: Tools>>Convert Delphi Project.

It gets as far as an error saying:
"At this point there should be no missing units"
Continuing, results in the .dpr in the Source Editor only.

I didn't get such error. For me the converter in Lazarus trunk worked well. The older version may have a bug that caused your error.

The Copy Folder code is Windows only. It doesn't compile on Linux.
The same functionality should be trivial to implement with FileUtil functions. It would also reduce the amount of code in the app.
If you only want to work on Windows then the converted app may work directly.

[Edit: See FindAllFiles function for an example how to iterate files, and use CopyFile function.]

Juha
« Last Edit: December 28, 2011, 03:11:56 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #2 on: December 28, 2011, 08:51:44 pm »
Hello,
Thanks for the reply.  May I enquire what version of Lazarus youare running?  Mine is 0.9.30.  (Date 2011-03-08).

Although my home PC is a Linux/Win combo, my work is Win only, so the compile is not a problem.

When you converted the code, did it run?

Regards

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #3 on: December 28, 2011, 11:17:22 pm »
Thanks for the reply.  May I enquire what version of Lazarus youare running?  Mine is 0.9.30.  (Date 2011-03-08).

The latest Lazarus trunk, 0.9.31.

Quote
Although my home PC is a Linux/Win combo, my work is Win only, so the compile is not a problem.
When you converted the code, did it run?

No, I used Linux and it didn't compile or run. I could later try on Windows if it is important. I am personally interested only in multi-platform code. Lazarus is designed for that, you know.
Anyway, the application seems to use only standard GUI components and has only few source files. The conversion is easy except for the multi-platform issue.
You should consider reimplementing the actual recursive "copy directory" code. This example has too much code for such simple task IMO.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #4 on: December 28, 2011, 11:31:55 pm »
You can use my stupid backup program.

Bart

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #5 on: December 29, 2011, 08:26:05 am »
Thanks for the reply JuhaManninen.  I'd also rather multi-platform, but when I saw it, I thought I might be able to learn from it.  Given that it doesn't convert well, recoding as per your suggestion is a good call.

Hello again Bart, I've given your program a try first, although I get an error when I first load- "unable to read file sbp.lpr".  Could this be a version issue?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #6 on: December 29, 2011, 11:44:55 am »
Bart, I've given your program a try first, although I get an error when I first load- "unable to read file sbp.lpr".  Could this be a version issue?

No, the archive is incomplete, the file sbp.lpr is missing from it.
I can upload a newer version when I have the time for it, if you want.

Bart

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #7 on: December 29, 2011, 12:00:59 pm »
Bart,

That would be appreciated when you get time.

Meanwhile I followed JuhaManninen's suggestion and have successfully used the FindAllFiles and CopyFiles methods to do a basic backup.

Code: [Select]
Procedure TForm1.Find;
var
  sl: TStringList;
  i: Integer;
begin
  sl := FindAllFiles(Edit1.Text, Edit2.Text, True);
  try
          for i:=0 to sl.Count-1 do
          copyfile(sl.Strings[i], Edit3.Text + ExtractFileName(sl.Strings[i])+'.'+ExtractFileExt(sl.Strings[i]));
          ListBox1.Items.Add('File'+IntToStr(i)+': ' + sl.Strings[i]);
  finally
    sl.Free;
    Shape1.Brush.Color :=clGreen;
    Application.ProcessMessages;
  end;
end; 

Whilst I can successfully copy the files from the path in Edit1.Text to the path in Edit3.Text, the two problems with the code are:
1. the ListBox only lists the last file that was copied
2. subdirectories are not copied - only files in the subdirectories.

Any thoughts on improving the code?

Regards

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4474
  • I like bugs.
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #8 on: December 31, 2011, 01:50:33 am »
Any thoughts on improving the code?

At least there is one clear error: the for loop has no begin / end. That is why ListBox doesn't get data. Proper indentation would make such errors more visible.

You can use sl\[i\] instead of sl.Strings\[i\].

My original idea was to use a class derived from TFileSearcher and only look FindAllFiles as an example. Maybe there is no real difference even with many files and your solution is good.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: Converting "Copy Folder Test" in DelphiForFun.org
« Reply #9 on: December 31, 2011, 10:11:39 am »
Hi Guys,

Well I've finished this little project - code attached if anyone is interested.

I've taken JuhaManninen's advice, and am pretty happy.

I was thrown a bit by trying to copy directories, but found ForceDirectories was useful. 

Also, as I read through the search directories, I use the result of the search as the directory to create in ForceDirectories.  One Issue was trying to get rid of the C:\ from each directory.  I was up all night trying to find something that would strip the leading 3 characters.  The AnsiReplaceText works, but I'm sure there must be a better way.

I've included a copy, if anyone sees this post in the future, and wants a copy of the end result. To get the file below the 250KB I've compressed with 7zip and Ultra compression.

Regards, and thanks for the help.


 

TinyPortal © 2005-2018