Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Bytes einer Datei berechnen (https://www.delphipraxis.net/29339-bytes-einer-datei-berechnen.html)

HW764 7. Sep 2004 20:04


Bytes einer Datei berechnen
 
Wie bringe ich delphi dazu, dass er:

1.Den Pfad aus einer Datei aus Edit1 ausliest
2.Er sie Grösse der in edit1 stehenden Datei erkennt
3.Er sie in bytes umrechnet und halbiert. (und auf die Grössere bytezahl rundet)

Gruss HW764

Luckie 7. Sep 2004 20:08

Re: Bytes einer Datei berechnen
 
1. Grundlagen: :roll:
Delphi-Quellcode:
var
  s: String;
begin
  s := Edit1,text;
2. Forensuche sollte helfen.

3. Grundlagen: Grundrechenarten in Delphi.

Nothine 7. Sep 2004 20:28

Re: Bytes einer Datei berechnen
 
Zitat:

Zitat von HW764
1.Den Pfad aus einer Datei aus Edit1 ausliest

Delphi-Quellcode:
var F: File;

...

begin
  AssignFile(F,Edit1.Text);
  Reset(F);
end;
{ Hinterher mit
  CloseFile(F);
  wieder freigeben}
Zitat:

Zitat von HW764
2.Er sie Grösse der in edit1 stehenden Datei erkennt

Delphi-Quellcode:
  DeineVariable := FileSize(F); //in Byte
Zitat:

Zitat von HW764
3.Er sie in bytes umrechnet und halbiert. (und auf die Grössere bytezahl rundet)

das wird durch FileSize bereits erledigt...

Luckie 7. Sep 2004 20:33

Re: Bytes einer Datei berechnen
 
Man beachte:
Zitat:

Description

Call FileSize to determine the size of the file specified by the file variable F. The size is expressed as the number of records in a record file. Thus:

If the file is declared as a file of byte, then the record size defaults to one byte, and FileSize returns the number of bytes in the file.
The Reset procedure can set the record size (in bytes) when it opens the file. In this case, FileSize returns the number of records in the file.

Note: If the file is declared as an untyped file and you don’t specify a record size when you call Reset, then FileSize assumes a record size of 128. That is, FileSize gives the number of bytes divided by 128.

To use FileSize, the file must be open. If the file is empty, FileSize(F) returns 0.

Note: FileSize can't be used on a text file.
So würde ich es machen:
Delphi-Quellcode:
function GetFileSize(szFile: PChar): Int64;
var
  fFile: THandle;
  wfd: TWIN32FINDDATA;
begin
  result := 0;
  if not FileExists(szFile) then exit;
  fFile := FindFirstfile(pchar(szFile),wfd);
  if fFile = INVALID_HANDLE_VALUE then exit;
  result := (wfd.nFileSizeHigh*(Int64(MAXDWORD) + 1))+wfd.nFileSizeLow;
  windows.FindClose(fFile);
end;

mirage228 7. Sep 2004 20:35

Re: Bytes einer Datei berechnen
 
Ich stelle jetzt mal einfach so meine Funktion in den Raum :P
Delphi-Quellcode:
  function GetFileSize(const FileName: String; var Size: Int64): Boolean;
  var
    sr: TWin32FindData;
    h: HWND;
  begin
    h := FindFirstFile(PChar(FileName), sr);
    Result := (h <> 0) and (FileExists(FileName)) and (sr.dwFileAttributes and
       FILE_ATTRIBUTE_DIRECTORY = 0);
    if Result then
      Size := (sr.nFileSizeHigh * MAXDWORD) + sr.nFileSizeLow else Size := 0;
    Windows.FindClose(h);
  end;
mfG
mirage228


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:23 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz