Solved

Recycle Bin

Posted on 2002-05-20
19
933 Views
Last Modified: 2010-04-04
Couple of questions actually but they're all Recycle Bin related so tough:

1. How do I go about emptying the Recycle Bin in code under Win 9X AND NT?
2. How do I find out details of what's IN the Recycle Bin (i.e. no files, sizes etc.)?

Anybody got any ideas?

The Neil =:)
0
Comment
Question by:TheNeil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +5
19 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 7021100
hi,
from paq:

to empty recycle bin use :

{$EXTERNALSYM SHEmptyRecycleBin}
function SHEmptyRecycleBin(hwnd: HWND; pszRootPath: PChar; dwFlags: DWord): HResult; stdcall;

const
SHERB_NOCONFIRMATION = 1;
SHERB_NOPROGRESSUI = 2;
SHERB_NOSOUND = 4;

implementation

{$R *.DFM}

{$EXTERNALSYM SHEmptyRecycleBin}
function SHEmptyRecycleBin; external 'shell32.dll' name 'SHEmptyRecycleBinA';

procedure TForm1.Button1Click(Sender: TObject);
begin
SHEmptyRecycleBin(Application.Handle, nil, SHERB_NOCONFIRMATION);
end;



i havent got listing files to work properly before but i remember someone posting some code here once that was half way there ..

listening for fursther info myself :)
0
 
LVL 5

Author Comment

by:TheNeil
ID: 7021141
Weeeellllll, that looks suspciously like code that supposedly works under Win 9X but under NT I get 'Unable to find SHEmptyRecycleBinA in Shell32.dll' when I run it

Sorry =:(
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7021152
Hi TheNeil, Inthe

found some API calls utilized in vb (sorry)

http://www.mvps.org/vbnet/index.html?code/system/shrecycle.htm
http://www.mvps.org/vbnet/code/shell/shfileopadv.htm

Hope that you can use some of it, since the calls should be essentially the same

:O)Bruintje
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:TheNeil
ID: 7021335
Hi Bruintje,

Just as with InThe's answer I get the same 'Unable to find <X> in Shell32.dll' error when I run the code. Under Win 9X this might be fine but this has got to work under NT as well and as that's what I'm working under today, it's a none starter

Good suggestion though

The Neil =:(
0
 
LVL 44

Accepted Solution

by:
bruintje earned 100 total points
ID: 7021376
It may be a version problem. From MSDN:

Requirements
  Version 4.71 and later of Shell32.dll

  Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later)..

  Windows 95/98: Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)..

:O)Bruintje
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7021698
Couple times ago I played with recycle bin folder.

function SHQueryRecycleBin(pszrtootpath:pchar;QUERYRBINFO:pshqueryrbinfo):integer;stdcall;
external 'shell32' name 'SHQueryRecycleBinA';

procedure TForm1.Button1Click(Sender: TObject);
var rbinfo:SHQUERYRBINFO;
begin
  rbinfo.cbSize:=sizeof(rbinfo);
  rbinfo.i64Size:=0;
  rbinfo.i64NumItems:=0;
  SHQueryRecycleBin('C:\',@rbinfo);
  Label1.Caption:=intTostr(  rbinfo.cbSize);
  Label2.Caption:=inttostr ( rbinfo.i64Size  );
  Label3.Caption:=inttostr(   Rbinfo.i64NumItems);
end;

Label1 mean size of structure
Label2 mean size of recycle bin
Label3 mean number of items

Tested now under XP Pro .
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7021728
Then I was able to read the each file name and the original location but look like XP have changed the structure of Info2 file .
0
 
LVL 17

Expert Comment

by:inthe
ID: 7022272
i was suprised as its working here on nt no problems then i seen Bruintje  comments ,without a version of shell32.dll newer than 4.71 i dont know how your supposed to empty it,the ie4 active desktop update will update it to 4.71 if you want to install it (if you can still get it :)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7022642
interesting :)
0
 
LVL 34

Expert Comment

by:Slick812
ID: 7023149
I tried the SHEmptyRecycleBin( ) and it worked on one puter and not on another, but I forget which OS versions.
Here's some code that I have used to get the contents of the recycle and I added a contex menu delete to it.


implementation

{$R *.DFM}

uses ShlObj, ComObj, ActiveX;

procedure TForm1.sbut_EmptyReClick(Sender: TObject);
var
DeskFolder, RecycleBin: IShellFolder;
RecycleIDL, NextId: PItemIDList;
OleR: HResult;
ShellMalloc: IMalloc;
EnumList: IENUMIDLIST;
IsThere: ULONG;
PIidListA: Array of Pointer;
IdListNum: Cardinal;
i: Integer;
StrRet: TStrRet;
FileName1: String;
Pstr: PChar;
CmInfo: CMINVOKECOMMANDINFO;
ContextMenu: IContextMenu;
begin
IdListNum := 1;
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
  begin
  OleCheck(SHGetDesktopFolder(DeskFolder));
  OleCheck(SHGetSpecialFolderLocation(Application.Handle, CSIDL_BITBUCKET, RecycleIDL));
  DeskFolder.BindToObject(RecycleIDL, nil, IShellFolder, RecycleBin);
  if RecycleIDL <> nil then
    ShellMalloc.Free(RecycleIDL);
  OleR := RecycleBin.EnumObjects(Application.Handle, SHCONTF_FOLDERS or SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN, EnumList);
  if SUCCEEDED(OleR) then
    begin //OleR
    ListBox1.Clear;
    while (EnumList.Next(1, NextId, IsThere)=S_OK) do
      begin //while
      if IsThere > 0 then
        begin // IsThere
        RecycleBin.GetDisplayNameOf(NextId, SHGDN_NORMAL, StrRet);
        case StrRet.uType of
          STRRET_CSTR: SetString(FileName1, StrRet.cStr, lStrLen(StrRet.cStr));
          STRRET_OFFSET:
            begin
            Pstr := @NextId.mkid.abID[StrRet.uOffset - SizeOf(NextId.mkid.cb)];
            SetString(FileName1, Pstr, NextId.mkid.cb - StrRet.uOffset);
            end;
          STRRET_WSTR: FileName1 := StrRet.pOleStr;
          end;
        ListBox1.Items.Add(FileName1);
        {the FileName1 will have the file in the recycle}
        SetLength(PIidListA,IdListNum);
        PIidListA[IdListNum-1] := NextId;
        Inc(IdListNum);
        end; //IsThere
      end; // while
    if IdListNum > 1 then
    try
      OleR := RecycleBin.GetUIObjectOf(Application.Handle, IdListNum-1, PItemIDList(PIidListA[0]), IID_IContextMenu, nil, ContextMenu);
      if SUCCEEDED(OleR) then
        begin
        FillChar(CmInfo, SizeOf(CmInfo), 0);
        with CmInfo do
          begin
          cbSize:= sizeof (CMINVOKECOMMANDINFO);
          fMask:= 0;
          hwnd:= Application.Handle;
          lpVerb:= PChar('delete');
          lpParameters:= nil;
          lpDirectory:= nil;
          nShow:= SW_SHOWDEFAULT;
          dwHotKey:= 0;
          hIcon:= 0;
          end;

        ContextMenu.InvokeCommand(CmInfo);
        {this does the Recycle Context menu Delete}
        end;
   finally
   if IdListNum > 1 then
   for i := 0 to High(PIidListA) do
   ShellMalloc.Free(PIidListA[i]);
   end;
   end; // OleR
  end;

end;

- - - - - - - - - - - - - - - - -
it may could be optimized some, but it worked for me. If it's unclear, ask questions.
Wes
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7023345
Wow, Wes, I'm surprised to see such complicated code without any memory/resource leak, well done! That's not what I'm used to see here...   :-)

Regards, Madshi.

P.S: Nothing precious to add to all the comments here...
0
 
LVL 5

Author Comment

by:TheNeil
ID: 7023449
Bruintje

The out of date DLL looks to be the root of the problem BUT how do I update it? Under NT it won't let me and even though I'm using IE 5.5 (and re-sinstalled it to make sure) on NT4 (service pack 6), Shell32.dll stubbornly stays at version 4.00 (which explains why it can't find entry points for the Recycle Bin routines) - Any ideas?

The Neil =:)
0
 
LVL 5

Author Comment

by:TheNeil
ID: 7023462
Bruintje

The out of date DLL looks to be the root of the problem BUT how do I update it? Under NT it won't let me and even though I'm using IE 5.5 (and re-sinstalled it to make sure) on NT4 (service pack 6), Shell32.dll stubbornly stays at version 4.00 (which explains why it can't find entry points for the Recycle Bin routines) - Any ideas?

The Neil =:)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7023467
Only IE4 had the option to update the shell. IE5 and IE6 don't have this option anymore...   :-/

Read it here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/versions.asp

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 7023624
Am not sure about this but could'nt you just delete the files from the folder C:\Recycled or whichever the drive is if you don't have a version of shell32.dll newer than 4.71

I seem to remember having to do something like this a while back. It should work as long as the recycle bin is setup to recycle files.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 7023632
Also just found a link which may be of interest to you:

http://www.efg2.com/Lab/Library/Delphi/IO/Files.htm
0
 
LVL 5

Author Comment

by:TheNeil
ID: 7023739
Bruintje

This is definitely the answer and it seems that the IT people where I work made sure that Shell32.dll was always reset to v4.00 on reboot so even installing/replacing it wouldn't add the recycle bin functionality. Why they do this I don't know but what I do know is that this works now (after much shouting)

Thanks for everyone's help

The Neil =:)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7026295
Wow, thank you very much and all the others for contributing.....learned a lot again just for hopping in
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question