Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Check for files in a folder.

Hi.

I'd like a procedure to check the files inside recycle bin.
(Not yet deleted...completely)
And then create a list with the file names...

Is that possible?

Thanks in advance :)
SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK

ASKER

Hi mikelittlewood :) Thanks
Yep Delphi 7.

Of course i prefer the easy way ;)

BUT

I need to export this to a txt file ! (All the info that this ShellList has.)
Does ShellList support export to a txt file (pass it to a StringList maybe)?

Avatar of anorganix
anorganix

Hi! The code below retrieved information about the number ob files in the Recycle Bin and their total size.
I hope it helps...

///////////////////////// begin code /////////////////////////

type
  PSHQueryRBInfo = ^TSHQueryRBInfo;
  TSHQueryRBInfo = packed record
    cbSize: DWORD;
    i64Size: Int64;
    i64NumItems: Int64;
  end;

function SHQueryRecycleBin(szRootPath: PChar; SHQueryRBInfo: PSHQueryRBInfo): HResult;
                                         stdcall; external 'shell32.dll' Name 'SHQueryRecycleBinA';

function GetBinInfo: string;
var
  SHQueryRBInfo: TSHQueryRBInfo;
  R: HResult;
begin
  FillChar(SHQueryRBInfo, SizeOf(TSHQueryRBInfo), #0);
  SHQueryRBInfo.cbSize := SizeOf(TSHQueryRBInfo);
  R:=SHQueryRecycleBin(nil, @SHQueryRBInfo);

  if R=s_OK then
  begin
    Result:=Format('Size: %d bytes in %d item(s)',
            [SHQueryRBInfo.i64Size, SHQueryRBInfo.i64NumItems]);
  end else
    Result:=Format('Error: %x',[R]);
end;

// now, all you have to do is call the function

procedure TMainForm.ButtonClick(Sender: TObject);
begin
  BinLabel.Caption:=GetBinInfo;
end;

///////////////////////////// end code /////////////////////////////////////
Oh, and if you want to open the Recycle Bin, use this:

////////////////////////// begin code /////////////////////////////

uses ShlObj, ShellAPI, ActiveX;

procedure FreePidl(pidl: PItemIDList);
var
  allocator: IMalloc;
begin
  if Succeeded(shlobj.SHGetMalloc(allocator)) then
  begin
    allocator.Free(pidl);
    {$IFDEF VER90}
    allocator.Release;
    {$ENDIF}
  end;
end;

// and call the function like:

procedure TMainForm.ShowBinButtonClick(Sender: TObject);
var
  exInfo: TShellExecuteInfo;
begin
  FillChar(exInfo, SizeOf(exInfo), 0);

  with exInfo do
  begin
    cbSize := SizeOf(exInfo);
    fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_IDLIST;
    Wnd := Handle;
    nShow := SW_SHOWNORMAL;
    lpVerb := 'open';
    ShGetSpecialFolderLocation(Handle, CSIDL_BITBUCKET, PItemIDLIst(lpIDList));
  end;

  ShellExecuteEx(@exInfo);
  FreePIDL(exinfo.lpIDList);
end;

//////////////////////////// end code /////////////////////////////

That's it...
Maximizing the Master/ Wave volume is one but this... :)
Avatar of CodedK

ASKER

anorganix thanks :)
I'll check it tomorrow...
Avatar of CodedK

ASKER

Good morning (here...)
Anorganix i need the file names. Currently your code just gives the quantity of deleted items and the size.
Is it possible to extract file name too?
Thanks :)
I searched the Internet a little bit, but I didn't find useful stuff... I'll keep trying.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK

ASKER

:/

How can i use this?
Even if i have Sid i cant list the names of the files inside a txt...
I have the same problem :(
The Recycle Bin Path is *correct* but it's a system/hidden folder (special folder) so I think that's the reason...

Maybe search for files in hidden/system folder (TSearchRec attributes - faSystem, faHidden)?
I'll keep trying...
Avatar of CodedK

ASKER

I am working with your GetSid function, a ListView and a proc  "Enumerate Folder" from WorkShopAlex.

-------------
procedure EnumerateFolder( const Folder: string; Files: TStringList );
var
  I: Integer;
  SearchRec: TSearchRec;
  Folders: TStringList;
begin
  Folders := TStringList.Create;
  Folders.Add( IncludeTrailingBackslash( Folder ) );
  I := 0;
  while ( I < Folders.Count ) do begin
    if ( FindFirst( Folders[ I ] + '*.*', faAnyFile, SearchRec ) = 0 ) then begin
      repeat
        if ( ( SearchRec.Attr and faDirectory ) = faDirectory ) then begin
          if not ( ( SearchRec.Name = '.' ) or ( SearchRec.Name = '..' ) ) then begin
            Folders.Add( IncludeTrailingBackslash( Folders[ I ] + SearchRec.Name ) );
          end;
        end
        else begin
          Files.Add( Folders[ I ] + SearchRec.Name );
        end;
      until ( FindNext( SearchRec ) <> 0 );
      FindClose( SearchRec );
    end;
    Inc( I );
  end;
end;

---------------------------------------
The first parameter is the folder that i want to have enumerated
and the second parameter is the stringlist that will contain the result.

So passing the Bin SID with your function, then i create a stringlist, but the names that i get with
Enumerate Folder proc are strange. It only keeps the extensions right and the names appear like this:

DC34.exe
DC35.txt
Dc36.bat
--------------------------------------
With ShellListView i get the results i like...
Name, Original location.
But i cant export it to a txt....

Any ideas?
Hmm, strange indeed...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeap, Slick812's code works fine...
I'll also mention that the system recycle bin is a "Virtual Folder" it does NOT exist on a hard disk, although in some systems there is a "Real" disk folder called - RECYCLER - or some other system folder name, it is NOT the Recycle bin as shown in the Explorer view of "Recycle Bin", There is most likely a - RECYCLER - folder on every drive partition you have in that windows system, and there is information stored by the system about the listed files in the virtual recycle bin which contains the deleted files from all partitions. . . It is best to access a shell virtual folders by using Shell Interfaces and functions. . . Although you can get a list of files in the - RECYCLER - folder , there is no information about what these files were named before they got in that folder, except in shell info storage areas
Avatar of CodedK

ASKER

Sorry....

What should i put at the uses?
uses ComObj, ShlObj;
oh, and add ActiveX too.

uses ActiveX, ComObj, ShlObj;
Avatar of CodedK

ASKER

I'd like to thank everyone :)

Eventually the results are:

1.) Use ShellListView for gathering every info Explorer has... BUT.. cannot export to txt.
2.) Several function that either result to noname files or named files with no more info.

Anyway thanks to everyone..

Increasing and splitting due to high participation. :P