Board index » delphi » Copying multiple files in a filelistbox

Copying multiple files in a filelistbox

I want to copy multiple selected files in a filelistbox to a different
directory. These files have a unknown format. How can I copy them in
Delphi ?

Peter de Mooy
de_m...@dds.nl

 

Re:Copying multiple files in a filelistbox


Peter,
  If your using Delphi 2.0 under win 95 then the following will work in one
simple pass.  This code shows how to use the SHFileOpStruct to delete files
to the Bin (or completely) but you can also use it to copy, move or rename
files.  Whole directories at a time.  It also allows you to determine if
you want the files to be put in the Recycle Bin or wiped out completely.

procedure DeleteFiles;
var
  T:TSHFileOpStruct;
  X: Integer;
begin
    with T do
    begin
      Wnd:=0;
      wFunc:=FO_DELETE;
      pFrom:='E:\Temp\TestDel\*.*';
      fFlags:=FOF_ALLOWUNDO or FOF_FILESONLY or FOF_SILENT or
FOF_NOCONFIRMATION;
    end;
    SHFileOperation(T);
end;

This will, because of the *.*, delete all files in E:\Temp\TestDel\ and
does not require a loop or any other code.  Neat, huh!  Take a look at
SHFileOpStruct in the on-line help for more info on the flags.  E.g. The
code above will delete the file to the recycle bin (FOF_ALLOWUNDO) and will
not ask for confirmation (FOF_NOCONFIRMATION) or display a progress box
(FOF_SILENT).
The FOF_FILESONLY is required if you use the *.* or an error will be
raised.  See help for more info and details of other functions you can do
with SHFileOpStruct and SHFileOperation.  (Move, Copy, Rename)

******
The following is some info I down loaded but haven't been able to check out
so your on your own.
******

There are some other quirks you should be aware of, too:

* Give the complete file path for every file specified.  Do not rely on the
current directory, even if you change to it right before the call.  The
SHFileOperation API is not "smart" enough to use the current directory if
one is not given for undo information.  So, even if you give the
FOF_ALLOWUNDO flag, it will not move deleted files to the recycle bin
because it doesn't know what path they came from, and thus couldn't restore
them to their original location from the recycle bin.  It will simply
delete the file from the current directory.

* MS has a documentation correction about the pFrom member.  It says that
for multiple files, each filename is seperated by a NULL (#0) character,
and the whole thing is terminated by double NULLs.  You need the double
NULL whether or not you are passing multiple filenames.  Sometimes it will
work correctly without them, but often not.  That's because sometimes you
get lucky and the memory following the end of your string has a NULL there.

An example of how to do this would be:

var
  FileList: string;
  FOS: TShFileOpStruct;
begin
  FileList := 'c:\delete.me'#0'c:\windows\temp.$$$'#0#0;
  { if you were using filenames in string variables: }
  FileList := Filename1 + #0 + Filename2 + #0#0;

  FOS.pFrom := PChar(FileList);

  // blah blah blah
end;

Hope this helps!
--
Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Centraal Bodemkundig Bureau <c...@tref.nl> wrote in article
<337837a7.8973...@news.tref.nl>...

Quote
> I want to copy multiple selected files in a filelistbox to a different
> directory. These files have a unknown format. How can I copy them in
> Delphi ?

> Peter de Mooy
> de_m...@dds.nl

Re:Copying multiple files in a filelistbox


Quote
Centraal Bodemkundig Bureau wrote:

> I want to copy multiple selected files in a filelistbox to a different
> directory. These files have a unknown format. How can I copy them in
> Delphi ?

Use the CopyFile WinAPI call on each file in the listbox.

Other Threads