Recent

Author Topic: Get Free space of Disk or USB  (Read 776 times)

eldonfsr

  • Sr. Member
  • ****
  • Posts: 295
Get Free space of Disk or USB
« on: May 22, 2022, 01:49:53 am »
How i can get free space  of usb or any disk on windows
DiskFree(0) is current disk how i can ask for disk F or H

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Get Free space of Disk or USB
« Reply #1 on: May 22, 2022, 02:21:53 am »
Please see the FPC documentation for diskfree for how to do this.
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

eldonfsr

  • Sr. Member
  • ****
  • Posts: 295
Re: Get Free space of Disk or USB
« Reply #2 on: May 23, 2022, 11:24:32 pm »
thanks trev  i read and tested but don't accept send string parameters only numbers how i can know what is number for hdd H, on linux sad use adddisk() but on windows is not available...
.

winni

  • Hero Member
  • *****
  • Posts: 3092
Re: Get Free space of Disk or USB
« Reply #3 on: May 24, 2022, 12:23:23 am »
Hi!

0 = current drive.
1 = A:
2 = B:
3 = C:
4 = D:
5 = E:
....


Winni



mas steindorff

  • Hero Member
  • *****
  • Posts: 500
Re: Get Free space of Disk or USB
« Reply #4 on: May 24, 2022, 01:25:31 am »
that works if you know that you are after  D: or E:

GetDiskFreeSpace() takes the drive as a string (cast to pchar) and returns lots of info you may find useful.

you can use getDriveType() to find out if it is ...
  TDrvType = (dtNotDetermined, dtNonExistent, dtRemoveable,
              dtFixed, dtRemote, dtCDROM, dtRamDrive);
and you can use
GetVolumeInformation() to get the volume name use full if you named your USB drive and it is one of several pulled into the system.
It's been a while since I worked on my "diskutil" unit but I believe they are all built ins.
my uses ...
  windows   ,fileutil   ,process   ,jwadbt   ; 
incase you can not find something.



« Last Edit: May 24, 2022, 09:18:55 pm by mas steindorff »
windows 7/10 - laz 2.0 / 1.2.6 general releases

eldonfsr

  • Sr. Member
  • ****
  • Posts: 295
Re: Get Free space of Disk or USB
« Reply #5 on: May 24, 2022, 04:00:20 am »
Thanks well some confuse  i send DiskFree(0) witch is current drive and return this value
(0)   FSpaceD = -229756928

i chanege Diskfree(3) suppost is drive c:\ and return this value
(3)   FSpaceD = -194945024

not must be same...
could be partitions of HDD
you can see on image values from windows

Code: Pascal  [Select][+][-]
  1. procedure TFormMain.BtnStartClick(Sender: TObject);
  2. var
  3.   FilesBkp: TStringList;
  4.   i:integer;
  5.   f: File of byte;
  6.   tbk,udisk:integer;
  7.   Info : TSearchRec;
  8.   Attributes: Integer;
  9.   CFolder,cFile,TDisk:String;
  10.   FSpace,FSpaceD:longint;
  11.   Disklt:Array[0..24]of String;
  12. begin
  13.   Attributes := Attributes or faHidden;
  14.   LogFile:= TMemo.Create(nil);
  15.   FilesBkp := TStringList.Create;
  16.   tbk:=0;
  17.   Disklt[1]:='A:\';
  18.   Disklt[2]:='B:\';
  19.   Disklt[3]:='C:\';
  20.   Disklt[4]:='E:\';
  21.   Disklt[5]:='F:\';
  22.   Disklt[6]:='G:\';
  23.   Disklt[7]:='H:\';
  24.   Disklt[8]:='I:\';
  25.   Disklt[9]:='J:\';
  26.   Disklt[10]:='K:\';
  27.   Disklt[11]:='L:\';
  28.   Disklt[12]:='M:\';
  29.   Disklt[13]:='N:\';
  30.   Disklt[14]:='O:\';
  31.   Disklt[15]:='P:\';
  32.   Disklt[16]:='Q:\';
  33.   Disklt[17]:='R:\';
  34.   Disklt[18]:='S:\';
  35.   Disklt[19]:='T:\';
  36.   Disklt[20]:='U:\';
  37.   Disklt[21]:='V:\';
  38.   Disklt[22]:='X:\';
  39.   Disklt[23]:='Y:\';
  40.   Disklt[24]:='Z:\';
  41.   try
  42.     if( ETFolder.Text<>'') then begin
  43.     for i:=0 to LbFolders.Items.Count-1 do  begin
  44.       FindAllFiles(FilesBkp, LbFolders.Items[i] , '*.*', true); //find e.g. all pascal sourcefiles
  45.     end;
  46.  //   ShowMessage(Format('Files for backup %d ', [Filesbkp.Count]));
  47.     For i:=0 to FilesBkp.Count -1 do begin
  48.       AssignFile(f, FilesBkp[i]);
  49. //       LBFiles.Items.Add(FilesBkp[i]);
  50.       Reset(f);
  51.       tbk:=tbk+FileSize(f);
  52.       CloseFile(f)
  53.     end;
  54.     if( LbFiles.Items.Count > 0 ) then begin
  55.       for i:=0 to LbFiles.Items.Count-1 do  begin
  56.         AssignFile(f,LbFiles.Items[i]);
  57.         Reset(f);
  58.         tbk:=tbk+FileSize(f);
  59.         CloseFile(f)
  60.       end;
  61.     end;
  62.     StatusBar1.Panels[0].Text:='Total space required for Backup: '+ FloatToStr(tbk div (1024*1024))+' MB';
  63.     TDisk:=UpperCase( Copy(ETFolder.Text, 0, Pos('\',ETFolder.Text)));
  64.     for i:=0 to length(DiskLt) do begin
  65.        if( DiskLt[i] = TDisk ) then begin
  66.          udisk:=i;
  67.          break;
  68.        end;
  69.     end;
  70.     FSpaceD:=DiskFree(udisk);
  71.     FSpace:= FSpaceD div (1024*1024) ; // Mega Bit
  72.     If Not DirectoryExists(ETFolder.Text) then begin
  73.        If Not CreateDir (ETFolder.Text) Then
  74.           LogFile.Lines.Add('Error Cerate folder ' +ETFolder.Text );
  75.     end;
  76.     for i:=0 to LBFolders.Count-1 do begin;
  77.       SDD.FileName:= LbFolders.Items[i];
  78.       cFolder:=  Copy(SDD.FileName, Rpos('\',SDD.FileName)+1, length(SDD.FileName)-Rpos('\',SDD.FileName));
  79.       If Not DirectoryExists( ETFolder.Text+'\'+cfolder) then
  80.          if Not CreateDir (ETFolder.Text+'\'+cfolder) Then
  81.                      LogFile.Lines.Add('Error cerate folder '+ ETFolder.Text+'\'+Info.name);
  82.        if FindFirst(SDD.FileName  + '\*.*', faAnyFile and faDirectory, Info)=0 then begin
  83.             repeat
  84.             if( (Info.Attr and faDirectory=0) ) then begin
  85.               if not FileUtil.CopyFile(SDD.FileName+'\'+info.Name , ETFolder.Text+'\'+cfolder+'\'+info.Name, true) then
  86.                 begin
  87.                   LogFile.Lines.Add('Failed to copy file : ' + SDD.FileName+'\'+info.Name)
  88.                 end
  89.  
  90.             end else begin
  91.               if (Info.Name <> '.') and (Info.Name <> '..') then begin
  92.                  If Not DirectoryExists( ETFolder.Text+'\'+cfolder+'\'+info.Name) then
  93.                     If Not CreateDir (ETFolder.Text+'\'+cfolder+'\'+info.Name) Then
  94.                      LogFile.Lines.Add('Error cerate folder '+ ETFolder.Text+'\'+cfolder+'\'+Info.name);
  95.  //                    SDD.FileName:=SDD.FileName+'\'+info.Name ;
  96.                      GetSubFolder(Info.Name ,  SDD.FileName+'\'+info.Name , ETFolder.Text+'\'+cfolder+'\'+Info.name  );
  97.               end;
  98.             end;
  99.  
  100.             until FindNext(Info) <> 0;
  101.             FindClose(Info);
  102.  
  103.        end;
  104.      end;
  105.      for i:=0 to LBFiles.Count-1 do begin
  106.        cFile:=  Copy(LbFiles.Items[i] , Rpos('\',LbFiles.Items[i])+1, length(LbFiles.Items[i])-Rpos('\',LbFiles.Items[i]));
  107.        if not FileUtil.CopyFile(LbFiles.Items[i] , ETFolder.Text+'\'+cFile, true) then begin
  108.             LogFile.Lines.Add('Failed to copy file : ' + LbFiles.Items[i])
  109.        end
  110.      end;
  111.      LogFile.Lines.SaveToFile(GetUserDir+'\Documents'+'\sbackuplog.log');
  112.     end else begin
  113.       ShowMessage('Backup can''t create don''t has targer folder information');
  114.       ETFolder.SetFocus;
  115.     end;
  116.   finally
  117.     Filesbkp.Free;
  118.   end;
  119.  
  120. end;    
  121.  

 

Thaddy

  • Hero Member
  • *****
  • Posts: 11620
Re: Get Free space of Disk or USB
« Reply #6 on: May 24, 2022, 07:55:30 am »
In this case it looks that it shows the recovery partition at index 3 , winni's table is not correct:
Also note your code contains an error, so the return value is negative. See your output.
  FSpace,FSpaceD:longint;
should be:
  FSpace,FSpaceD:Int64;
That is because DiskFree returns Int64
« Last Edit: May 24, 2022, 08:19:37 am by Thaddy »
Black themes should be banned.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1983
  • Former Delphi 1-7, 10.2 user
Re: Get Free space of Disk or USB
« Reply #7 on: May 24, 2022, 09:25:29 am »
could be partitions of HDD

Please read the documentation for diskfree.

Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

eldonfsr

  • Sr. Member
  • ****
  • Posts: 295
Re: Get Free space of Disk or USB
« Reply #8 on: May 24, 2022, 02:47:30 pm »
Ok Thanks great help...

winni

  • Hero Member
  • *****
  • Posts: 3092
Re: Get Free space of Disk or USB
« Reply #9 on: May 24, 2022, 04:32:33 pm »
In this case it looks that it shows the recovery partition at index 3 , winni's table is not correct:

RTFM

BobDog

  • Sr. Member
  • ****
  • Posts: 265
Re: Get Free space of Disk or USB
« Reply #10 on: May 24, 2022, 05:07:13 pm »
The Winapi will get this task done.
Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. uses
  4. windows,sysutils;
  5.  
  6. function Disks(whichone:ansistring):double;
  7. const
  8. d=1024*1024*1024;
  9. e='                ';
  10. var
  11. a,b,c:qword;
  12. i:int32;
  13. s:ansistring;
  14. ret:double;
  15. begin
  16. ret:=0.0;
  17. writeln('Drive',e,'Total Gb',e,'Free Gb');
  18. for i:=ord('A') to ord('Z') do
  19. begin
  20. s:=chr(i)+':';
  21. if GetDiskFreeSpaceexA(pchar(s),@a,@b,@c)then
  22. begin
  23. If (Pos (uppercase(whichone),s)<>0) then ret:=c/d;
  24.  writeln(chr(i)+':',e,b/d:9:5,e,c/d:9:5);
  25.  end;
  26. end;
  27. writeln;
  28. exit(ret)
  29. end;
  30.  
  31. var free:double;
  32.  
  33.  
  34. begin
  35. free:=disks('c');
  36. writeln('Free space on required disk = ',free:9:9,' Gb');
  37. writeln('Press return to end');
  38. readln;
  39. end.
  40.  
  41.  
  42.  
« Last Edit: May 24, 2022, 05:31:59 pm by BobDog »

BobDog

  • Sr. Member
  • ****
  • Posts: 265
Re: Get Free space of Disk or USB
« Reply #11 on: May 25, 2022, 08:29:54 am »
eldonfsr
How can I run your procedure
procedure TFormMain.BtnStartClick(Sender: TObject);
What units must I use.
I am using geany ide.
I have no access to TFormMain.BtnStartClick

eldonfsr

  • Sr. Member
  • ****
  • Posts: 295
Re: Get Free space of Disk or USB
« Reply #12 on: May 27, 2022, 01:31:25 am »
Thanks Bob, Give mw new idea to manage that, This my interface components
interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, ComCtrls,
  ExtCtrls, Buttons, Grids, StdCtrls, DBGrids, StrUtils, process, BufDataset,
  DB, memds, Types;   

and this is on Uses...
implementation
   uses FileUtil,lazfileutils,crt;

when start with this i thinking windows is going to show a list of disk drives, as you going testing i learning more.....

Thanks.. all take your time and send some text...
 

 

TinyPortal © 2005-2018