Board index » delphi » How to move files to the recycle bin?

How to move files to the recycle bin?

Hello,
I am currently using delete(..) to remove files, but it seems to
be deleted forever. How can I delete it to the recycle bin?

Thanks,

CK Lam
--
         _/_/_/    _/  _/
       _/         _/_/
      _/         _/ _/  
      _/_/_/    _/   _/

 

Re:How to move files to the recycle bin?


imagen,....

if you select files from listview...

it'll be workd... like this...

  sTemp := '';
  for nLoop := 0 to (ListView1.Items.Count -1)
  do
    begin
      if Listview1.Items[nLoop].StateIndex = 0
// if seleted...
      then
        begin
          sTemp := sTemp + #0 + Listview1.Items[nLoop].Caption;
// you must add #0.. for mutiple file operation...
        end
      ;
    end
  ;
  sTemp := Copy(sTemp,2,Length(sTemp)) + #0#0;
// you must add #0#0 the end of fils name..

& you use

ShFileOperation Api..

thanks.

 C.K. Lam (?) <5k9j28$...@mimas.tc.hsa.nl> a?????- ??o????...

Quote
>Hello,
>I am currently using delete(..) to remove files, but it seems to
>be deleted forever. How can I delete it to the recycle bin?

>Thanks,

>CK Lam
>--
>         _/_/_/    _/  _/
>       _/         _/_/
>      _/         _/ _/
>      _/_/_/    _/   _/

Re:How to move files to the recycle bin?


CK,
  If your using Delphi 2.0 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

C.K. Lam <e9...@tc.hsa.nl> wrote in article <5k9j28$...@mimas.tc.hsa.nl>...

Quote
> Hello,
> I am currently using delete(..) to remove files, but it seems to
> be deleted forever. How can I delete it to the recycle bin?

> Thanks,

> CK Lam
> --
>          _/_/_/    _/  _/
>        _/         _/_/
>       _/         _/ _/  
>       _/_/_/    _/   _/

Re:How to move files to the recycle bin?


Quote
>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);

>end;

Great - but how do you define TShFileOpStruct in delphi - an example
would be greatly appreciated.

Regards,
        Me.

Other Threads