Recent

Author Topic: how to get recycle bin size  (Read 4417 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
how to get recycle bin size
« on: March 19, 2022, 01:11:43 pm »
hello friends good morning a query, how can I get the size of the recycle bin in bytes

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: how to get recycle bin size
« Reply #1 on: March 19, 2022, 02:11:11 pm »
Operating system?

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: how to get recycle bin size
« Reply #2 on: March 19, 2022, 04:13:39 pm »
Operating system?

hello friends, I am needing for windows

balazsszekely

  • Guest
Re: how to get recycle bin size
« Reply #3 on: March 20, 2022, 05:55:06 pm »
@Ericktux
Quote
hello friends good morning a query, how can I get the size of the recycle bin in bytes

AFAIK there is no api for this, but you can read the value from registry.

1. Get the GUID of a drive with TProcess and mount volume:
Code: Pascal  [Select][+][-]
  1. uses Process, Registry;
  2.  
  3. function GetDriveGUID(ADrive: Char): String;
  4. var
  5.   Process: TProcess;
  6.   SL: TStringList;
  7.   P1, P2: Integer;
  8. begin
  9.   Result := '';
  10.   Process := TProcess.Create(nil);
  11.   try
  12.     Process.Executable := 'mountvol';
  13.     Process.Parameters.Add(ADrive + ':\');
  14.     Process.Parameters.Add('/L');
  15.     Process.Options := Process.Options + [poUsePipes];
  16.     Process.Execute;
  17.     SL := TStringList.Create;
  18.     try
  19.       SL.LoadFromStream(Process.Output);
  20.       if Trim(SL.Text) <> '' then
  21.       begin
  22.         P1 := Pos('{', SL.Text);
  23.         P2 := Pos('}', SL.Text);
  24.         if (P1 > 0) and (P2 > 0) then
  25.           Result := Copy(SL.Text, P1, P2 - P1 + 1);
  26.       end;
  27.     finally
  28.       SL.Free;
  29.     end;
  30.   finally
  31.     Process.Free;
  32.   end;
  33. end;                        
  34.  

2. Read the value from registry
Code: Pascal  [Select][+][-]
  1. function ReadFromRegistry(AGUID: String): Integer;
  2. var
  3.   Registry: TRegistry;
  4.   RegKey: String;
  5.   RegValue: String;
  6. begin
  7.   Result := -1;
  8.   Registry := TRegistry.Create(KEY_READ);
  9.   try
  10.     Registry.RootKey := HKEY_CURRENT_USER;
  11.     RegKey := 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\' + AGUID;
  12.     RegValue := 'MaxCapacity';
  13.     if Registry.KeyExists(RegKey) then
  14.       if Registry.OpenKey(RegKey, False) then
  15.         Result := Registry.ReadInteger(RegValue); //the result is in MB
  16.   finally
  17.     Registry.Free;
  18.   end;
  19. end;

3. Convert the result from MB to Byte. I leave this step to you.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: how to get recycle bin size
« Reply #4 on: March 20, 2022, 05:59:36 pm »
There is an api, shellapi....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: how to get recycle bin size
« Reply #5 on: March 21, 2022, 12:54:30 am »
thank you very much friend getmem, as always very kind, sorry for not expressing myself well, what I want is to obtain the size used by the files inside the trash in windows
« Last Edit: March 21, 2022, 12:56:28 am by Ericktux »

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: how to get recycle bin size
« Reply #6 on: March 21, 2022, 02:23:12 am »

From shell
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.   uses
  3.   process;
  4.  
  5.   function dir(path:ansistring;flag:ansistring='s'):ansistring;
  6.   var ans:ansistring=' ';
  7.   begin
  8.   RunCommand('c:\windows\system32\cmd.exe', ['/c', 'dir /'+flag+' '+path], ans);
  9.   exit(ans);
  10.   end;
  11.  
  12.   var
  13.   s:ansistring;
  14.   begin
  15.  
  16.   s:=dir('c:\$Recycle.bin');
  17.   writeln(s);
  18.   writeln;
  19.   writeln('Press return to exit');
  20.   readln;
  21.   end.
  22.  

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: how to get recycle bin size
« Reply #7 on: March 21, 2022, 05:09:44 am »
thanks for the help friend, I see that an easy solution is to obtain the size of the folder "c:\$Recycle.bin" of course it would only be for the C drive, but it is a good start

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: how to get recycle bin size
« Reply #8 on: March 21, 2022, 06:13:57 pm »
The path to the Recycle Bin is, of course, configurable.  So you should ask the OS what the path actually is, using either SHGetFolderPath(CSIDL_BITBUCKET) or SHGetKnownFolderPath(FOLDERID_RecycleBinFolder).

That being said, have a look at this blog article by Raymond Chen: Invoking commands on items in the Recycle Bin, which (amongst other things) shows you how to enumerate the items in the Recycle Bin using IShellItem.  And then you can query each item for its size to calculate the total size the Recycle Bin is using.
« Last Edit: March 21, 2022, 06:16:42 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: how to get recycle bin size
« Reply #9 on: March 21, 2022, 07:23:37 pm »

For the recycle bin (size only) then try:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.   uses
  3.   process,sysutils;
  4.  
  5.   function dir(path:ansistring;flag:ansistring='s'):ansistring;
  6.   var ans:ansistring=' ';
  7.   begin
  8.   RunCommand('c:\windows\system32\cmd.exe', ['/c', 'dir /'+flag+' '+path], ans);
  9.   exit(ans);
  10.   end;
  11.  
  12.   var
  13.   s:ansistring;
  14.   x:TStringArray;
  15.   begin
  16.  
  17.   s:=dir('c:\$Recycle.bin | findstr File(s)');
  18.   x:=s.split(#10);
  19.   writeln(x[high(x)-1]);
  20.   writeln;
  21.   writeln('Press return to exit');
  22.   readln;
  23.   end.
  24.  

If your bin is not on c drive then just change it
 s:=dir('d:\$Recycle.bin | findstr File(s)');

Tested on Win 10.
My result:
            9331 File(s)    719,146,331 bytes

Press return to exit


So I should empty it anytime soon.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: how to get recycle bin size
« Reply #10 on: March 21, 2022, 07:27:37 pm »
Not everyone installs Windows to the c:\windows folder, so you should ask the OS where the System32 folder is actually located (via GetSystemDirectory(), SHGetFolderPath(CSIDL_SYSTEM[X86]), or SHGetKnownFolderPath(FOLDERID_System[X86])), and then append cmd.exe to the end of it.  Or better, simply query the %CMDSPEC% environment variable instead, which will give you the full path to cmd.exe itself.
« Last Edit: March 21, 2022, 07:30:13 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: how to get recycle bin size
« Reply #11 on: March 21, 2022, 10:17:25 pm »

Remy Lebeau.
I can use "where" to be more general to locate cmd.exe.
(and thanks for reminding me that cmd.exe is not necessarily on the c drive)
It has been so long since I have had a computer with hard drive partitions, but if I remember correctly there can be a recycle bin on each partition (drive), so I still leave that bit to the user.
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.   uses
  3.   process,sysutils;
  4.  
  5.   function dir(path:ansistring;flag:ansistring='s'):ansistring;
  6.   var ans:ansistring=' ';
  7.   var location :string='';
  8.   begin
  9.   RunCommand('where',['cmd.exe'],location);
  10.   RunCommand(trim(location), ['/c', 'dir /'+flag+' '+path], ans);
  11.   exit(ans);
  12.   end;
  13.  
  14.   var
  15.   s:ansistring;
  16.   x:TStringArray;
  17.   begin
  18.  
  19.  
  20.  
  21.   s:=dir('C:'+'\$Recycle.bin | findstr File(s)'); //<<--------- user chooses which drive recycle bin.
  22.   x:=s.split(#10);
  23.   writeln(x[high(x)-1]);
  24.   writeln;
  25.   writeln('Press return to exit');
  26.   readln;
  27.   end.
  28.  


440bx

  • Hero Member
  • *****
  • Posts: 4030
Re: how to get recycle bin size
« Reply #12 on: March 21, 2022, 10:48:39 pm »
<snip> if I remember correctly there can be a recycle bin on each partition (drive), so I still leave that bit to the user.
That is the case, one recycle bin per partition.  The reason for it is, when the file is "deleted" it is simply moved to the recycle bin folder instead of copied to it (which is much faster and makes the time spent "deleting" independent of the file size.) 

If there was only one recycle bin for all partitions then any "deletion" in the other partitions would require copying the file being deleted which could potentially be very time consuming and would also cause problems with disk space management.

HTH.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: how to get recycle bin size
« Reply #13 on: March 22, 2022, 12:59:25 am »
That is the case, one recycle bin per partition.  The reason for it is, when the file is "deleted" it is simply moved to the recycle bin folder instead of copied to it (which is much faster and makes the time spent "deleting" independent of the file size.) 
Yes, it physically one recycle bin per partition. But I think if you use the object Shell.Application THEN the recycle bin will contain (show) the deleted files from ALL the partitions (just like when you open the recycle bin on your desktop, see for yourself by opening the recycle bin on your desktop).

In powershell it's easy. It's a oneliner  :D
Code: PowerShell  [Select][+][-]
  1. $sum=0; (New-Object -com shell.application).Namespace(10).items() | Foreach {$sum+=$_.Size}; $sum

I tried in Lazarus but somehow I couldn't get NameSpace(10) to work correctly.
For the C:\Temp it works.

Code: Pascal  [Select][+][-]
  1. uses ComObj;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   ObjShell, ObjFolder: olevariant;
  6.   I: integer;
  7. begin
  8.   ObjShell := CreateOleObject('Shell.Application');
  9.   ObjFolder := ObjShell.NameSpace(10); // 10 = RECYCLE_BIN ???????
  10.   ObjFolder := ObjShell.NameSpace('C:\Temp'); // DESKTOP
  11.   if ObjFolder <> Null then
  12.   begin
  13.     Memo1.Lines.Add('Folder: ' + ObjFolder.Title);
  14.     for I := 0 to ObjFolder.Items.Count - 1 do
  15.       Memo1.Lines.Add(ObjFolder.Items.Item(I).Name + ' ' + IntToStr(ObjFolder.Items.Item(I).Size));
  16.   end;
  17. end;

Now why doesn't it work for NameSpace(10) (which should work for the recycle bin).

Edit: Ok, I got it working. You really need to pass HEX $A as Variant.

This works now:
Code: Pascal  [Select][+][-]
  1. uses ComObj;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   ObjShell, ObjFolder: olevariant;
  6.   I, Size: integer;
  7. begin
  8.   ObjShell := CreateOleObject('Shell.Application');
  9.   ObjFolder := ObjShell.NameSpace(OleVariant($A)); // 10 = RECYCLE_BIN
  10.   if ObjFolder <> Null then
  11.   begin
  12.     Memo1.Lines.Add('Folder: ' + ObjFolder.Title);
  13.     Size := 0;
  14.     for I := 0 to ObjFolder.Items.Count - 1 do
  15.       Size := Size + ObjFolder.Items.Item(I).Size;
  16.     Memo1.Lines.Add('Total size = ' + Size.ToString);
  17.   end;
  18. end;

Can you work with this?

(I found that it's not really lightning fast. I would expected it to iterate through the bin faster.)

« Last Edit: March 22, 2022, 01:22:07 am by rvk »

440bx

  • Hero Member
  • *****
  • Posts: 4030
Re: how to get recycle bin size
« Reply #14 on: March 22, 2022, 03:59:33 am »
But I think if you use the object Shell.Application THEN the recycle bin will contain (show) the deleted files from ALL the partitions (just like when you open the recycle bin on your desktop, see for yourself by opening the recycle bin on your desktop).
Yes, you're right.  The Windows/File explorer "unifies" all the recycle bins and presents them as one. 

Can you work with this?
I appreciate the code and I'm sure the OP will appreciate it even more :)  I'm just following the thread with some interest.  (the recycle bin is very rarely a concern of mine.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018