當前位置:首頁 » 文件管理 » delphi獲取文件夾大小

delphi獲取文件夾大小

發布時間: 2022-05-12 10:40:34

❶ delphi 判斷文件大小並剪切到D盤

uses	
sysutils;
var
a:longint;
f,fd:fileofbyte;
x:byte;
begin
assign(f,'c:123.txt');reset(f);
a:=filesize(f);reset(f);
ifa>2000000thenbegin
assign(fd,'d:123.txt');rewrite(fd);
whilenoteof(f)dobeginread(f,x);write(fd,x);end;
close(fd);
end;
close(f);
ifa>2000000thenerase(f);
end.
{位元組文件,復制速度較慢。要提高速度,可採用流式文件復制!}

❷ 在DELPHI中如何用文件流讀寫幾百M大的文件呢

unit U_Client;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, Math,
StdCtrls, ComCtrls;

type
Tfrm_Client = class(TForm)
SaveDialog1: TSaveDialog;
IdTCPClient1: TIdTCPClient;
Button1: TButton;
StatusBar1: TStatusBar;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
procere Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frm_Client: Tfrm_Client;

implementation

{$R *.dfm}

procere Tfrm_Client.Button1Click(Sender: TObject);
var
cmd: string;
ASize, TotalSize: Int64;
AFileStream: TFileStream;
begin
IdTCPClient1.Host := Edit1.Text; //連接主機
IdTCPClient1.Port := StrToIntDef(Edit2.Text, 9925); //埠
IdTCPClient1.Connect; //連接
try
IdTCPClient1.WriteLn('BEGIN'); //提示伺服器開始接收
cmd := IdTCPClient1.ReadLn;
//以「|」符號分離文件名
SaveDialog1.FileName := Copy(cmd, Pos('|', cmd) + 1, Length(cmd));
if not SaveDialog1.Execute then
begin
IdTCPClient1.WriteLn('CANCEL'); //告訴伺服器取消
IdTCPClient1.Disconnect; //斷開連接
exit;
end;
TotalSize := StrToInt(Copy(cmd, 0, Pos('|', cmd) - 1)); //分離文件大小
//建立文件流准備接收
AFileStream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
try //循環開始接受
repeat
IdTCPClient1.WriteLn(IntToStr(AFileStream.Size));//發送當前傳輸的位置
ASize := Min(TotalSize - AFileStream.Size, IdTCPClient1.RecvBufferSize);
//選擇剩餘大小和緩沖區大小小的一個作為傳輸的大小
IdTCPClient1.ReadStream(AFileStream, ASize); //接收流
StatusBar1.SimpleText := Format('當前傳輸位置%d/大小%d', [AFileStream.Size, TotalSize]);
Application.ProcessMessages;
until AFileStream.Size = TotalSize; //大小一致了表示結束
finally
AFileStream.Free; //釋放文件流
end;
IdTCPClient1.WriteLn('END'); //提示伺服器傳輸完成
StatusBar1.SimpleText := '傳輸完成...';
except
StatusBar1.SimpleText := '連接伺服器失敗或者對方已經中斷傳輸!';
end;
IdTCPClient1.Disconnect;
end;

end.

unit U_Server;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, IdBaseComponent, IdComponent, IdTCPServer, Math;

type
Tfrm_Server = class(TForm)
IdTCPServer1: TIdTCPServer;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ProgressBar1: TProgressBar;
StatusBar1: TStatusBar;
Edit1: TEdit;
Button4: TButton;
OpenDialog1: TOpenDialog;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procere Button1Click(Sender: TObject);
procere Button4Click(Sender: TObject);
procere Button2Click(Sender: TObject);
procere Button3Click(Sender: TObject);
procere IdTCPServer1Execute(AThread: TIdPeerThread);
procere FormClose(Sender: TObject; var Action: TCloseAction);
private
AFileStream: TFileStream; //傳輸的文件流
procere ButtonBegin;
procere ButtonEnd;
{ Private declarations }
public
{ Public declarations }
end;

var
frm_Server: Tfrm_Server;

implementation

{$R *.dfm}

procere Tfrm_Server.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName;
end;

procere Tfrm_Server.Button4Click(Sender: TObject);
begin
Close;
end;

procere Tfrm_Server.Button2Click(Sender: TObject);
begin
if not FileExists(Edit1.Text) then //檢測文件是否存在
begin
Showmessage('文件不存在,請選擇文件!');
exit;
end;
//建立文件流
AFileStream := TFileStream.Create(Edit1.Text, fmOpenRead);
ProgressBar1.Max := AFileStream.Size;
ProgressBar1.Position := 0;
ButtonBegin; //VCL開始狀態設置
//伺服器准備好連接
IdTCPServer1.DefaultPort := StrToIntDef(Edit2.Text, 9925);
if not IdTCPServer1.Active then IdTCPServer1.Active := True;
end;

procere Tfrm_Server.ButtonBegin;
begin //VCL開始狀態設置
Button1.Enabled := False;
Button2.Enabled := False;
Button3.Enabled := True;
Button4.Enabled := False;
end;

procere Tfrm_Server.ButtonEnd;
begin //VCL結束狀態設置
Button1.Enabled := True;
Button2.Enabled := True;
Button3.Enabled := False;
Button4.Enabled := True;
end;

procere Tfrm_Server.Button3Click(Sender: TObject);
begin
StatusBar1.SimpleText := '傳輸取消...';
AFileStream.Free; //釋放文件流
ButtonEnd; //VCL結束狀態設置
end;

procere Tfrm_Server.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd: string; //接收到客戶端的字元串信息
ASize: Integer; //需要傳輸的流大小
begin
with AThread.Connection do //已經連街上的一個進程
begin
cmd := UpperCase(ReadLn); //客戶端發送的命令字元串
if cmd = 'BEGIN' then //開始傳輸
begin
//告訴遠程傳輸文件的大小和文件名
WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName(Edit1.Text)]));
StatusBar1.SimpleText := '准備傳輸...';
Exit;
end;
if cmd = 'END' then
begin //傳輸完成
Button3.Click;
StatusBar1.SimpleText := '傳輸完成...';
Exit;
end;
if cmd = 'CANCEL' then
begin //傳輸取消
StatusBar1.SimpleText := '傳輸取消...';
//保持傳輸狀態
Exit;
end;
//按照指定位置傳輸文件
AFileStream.Seek(StrToInT(cmd), soFromBeginning); //轉到文件流傳輸的位置
ASize := Min(AFileStream.Size - AFileStream.Position, RecvBufferSize);
//計算需要發送的大小,Min()函數在Math單元
OpenWriteBuffer; //准備發送緩沖
WriteStream(AFileStream, false, false, ASize);
//注意這個函數的參數。
CloseWriteBuffer; //結束發送緩沖
StatusBar1.SimpleText := Format('當前傳輸位置%s/大小%d', [cmd, AFileStream.Size]);
ProgressBar1.Position := ProgressBar1.Position + ASize;
end;
end;

procere Tfrm_Server.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPServer1.Active := False;
end;

end.

希望能幫到你!

❸ delphi中我想把文件大小用字元串的形式輸出

var
F:
file
of
Byte;
begin
AssignFile(F,
'D:\123.exe');
Reset(F);
Edit1.Text
:=
IntToStr(FileSize(F));//File的參數是file類型,返回值是Integer類型,如果要得到KB的話,需要用結果/1024,同理如果要得到MB和GB的話,可以繼續/1024就可以了
CloseFile(F);
end;

❹ delphi如何獲得文件大小

添加一個openDialog組件
var rmFilestream:Tfilestream;
xuanzewenjian:string;
MemSize:integer;
begin
if (openDialog1.Execute) and (openDialog1.FileName<>'') then
begin
xuanzewenjian:=OpenDialog1.FileName;//獲取文件名稱及完整路徑
rmFilestream:=TFileStream.Create(xuanzewenjian,fmopenRead);
rmFilestream.Position:=0;
MemSize:= rmFilestream.Size;//獲取文件大小
showmessage(inttostr(memsize));
end;
end;

❺ delphi 如何獲取文件的大小和類型

示例代碼如下:


  1. 首先,uses 添加IdGlobalProtocols。


2.

functionTForm1.GetFileSizeStr(fName:string):string;
varnSize:Integer;
begin
nSize:=FileSizeByName(fName);
ifnSize>1073741824then
Result:=FormatFloat('###,##0.00G',nSize/1073741824)
elseifnSize>1048576then
Result:=FormatFloat('###,##0.00M',nSize/1048576)
elseifnSize>1024then
Result:=FormatFloat('###,##00K',nSize/1024)
else
Result:=FormatFloat('###,#0B',nSize);
ifLength(Result)>2then
ifResult[1]='0'then
Delete(Result,1,1);
end;

procereTForm1.Button1Click(Sender:TObject);
begin
ShowMessage('文件大小是:'+GetFileSizeStr('d: able.mdb'));
end;


示例截圖:


❻ Delphi中,如何獲得文件大小

大概有這些方法可以獲得文件大小
FileSizeByName(需要引用IdGlobal單元)
GetFileSize
FileSize(不能獲得正在使用的文件大小)
FileSeek
TFileStream.Size

下面是使用例子
1。FileSizeByName(需要引用IdGlobal單元)
begin
if OpenDialog1.Execute then
begin
ShowMessage(IntToStr(FileSizeByName(OpenDialog1.Filename)));
end;

2。GetFileSize
var
FileHandle:integer;
begin
if OpenDialog1.Execute then
begin
FileHandle := FileOpen(OpenDialog1.FileName, 0);
ShowMessage(IntToStr(GetFileSize(FileHandle, nil)));
FileClose(FileHandle);
end;

3。FileSize(不能獲得正在使用的文件大小)
var f: file;
begin
if OpenDialog1.Execute then
begin
AssignFile(f, OpenDialog1.FileName);
Reset(f, 1);
ShowMessage(IntToStr(FileSize(f)));
CloseFile(f);
end;

4。FileSeek
var
FileHandle:integer;
begin
if OpenDialog1.Execute then
begin
FileHandle := FileOpen(OpenDialog1.FileName, 0);
ShowMessage(IntToStr(FileSeek(FileHandle,0,2)));
FileClose(FileHandle);
end;

5。TFileStream.Size
var
FS: TFileStream;
begin
if OpenDialog1.Execute then
begin
FS := TFileStream.Create(OpenDialog1.FileName, fmShareDenyNone);
ShowMessage(IntToStr(FS.Size));
FS.Free;
end;

另在IdGlobalProtocols中有個FileSizeByName()的函數。

❼ 怎麼在delphi中搜索TXT格式文件並把文件名,文件大小,路徑存入資料庫

你的問題的范圍太大;
1.TXT文件的搜索范圍沒有確定;
2.存入什麼資料庫等等等都還沒有說明;
盡管別人可以幫你,可是都只能說明一些大概的方法,不可能把細節都幫你想好的。
提供一些參考資料吧
1.搜索文件的函數和代碼
procere
GetDirsAndFiles(Path:
String;
Strings:
TStrings;
IncludePath:
Boolean
=
True);
//參數Path表示起始目錄,如「c:\MyDir」;Strings用來存放搜索的結果;IncludePath指定返回結果是否包括路徑,默認為True
var
F:
TSearchRec;
FileName,
RFileName:
String;
begin
//開始搜索
if
FindFirst(Path+'\*.*',
faAnyFile,
F)
=
0
then
repeat
FileName
:=
F.Name;
//「.」和「..」兩個內建文件夾對我們來說是毫無用處的,但是這個函數也找出來了
if
(FileName
<>
'.')
and
(FileName
<>
'..')
then
begin
if
IncludePath
then
RFileName
:=
Path
+
'\'
+
FileName
else
RFileName
:=
FileName;
//向字元串列表中添加搜索結果
Strings.Add(RFileName);
//如果是文件夾,則遞歸
if
F.Attr
and
faDirectory
<>
0
then
//指定新的起始目錄
GetDirsAndFiles(Path
+
'\'
+
F.Name,
Strings);
end;
//直到搜索完成
until
FindNext(F)
<>
0;
//最後釋放資源
FindClose(F);
end;
2.獲得文件大小
var
sr:
TSearchRec;
begin
if
FindFirst('c:\temp\test.dat',
faAnyFile,
sr)
=
0
then
sr.size就是指文件的大小。

❽ 怎麼樣得到文件的大小(DELPHI)

大概有這些方法可以獲得文件大小
FileSizeByName(需要引用IdGlobal單元)
GetFileSize
FileSize(不能獲得正在使用的文件大小)
FileSeek
TFileStream.Size
下面是使用例子
1。FileSizeByName(需要引用IdGlobal單元)
begin
if
OpenDialog1.Execute
then
begin
ShowMessage(IntToStr(FileSizeByName(OpenDialog1.Filename)));
end;
2。GetFileSize
var
FileHandle:integer;
begin
if
OpenDialog1.Execute
then
begin
FileHandle
:=
FileOpen(OpenDialog1.FileName,
0);
ShowMessage(IntToStr(GetFileSize(FileHandle,
nil)));
FileClose(FileHandle);
end;
3。FileSize(不能獲得正在使用的文件大小)
var
f:
file;
begin
if
OpenDialog1.Execute
then
begin
AssignFile(f,
OpenDialog1.FileName);
Reset(f,
1);
ShowMessage(IntToStr(FileSize(f)));
CloseFile(f);
end;
4。FileSeek
var
FileHandle:integer;
begin
if
OpenDialog1.Execute
then
begin
FileHandle
:=
FileOpen(OpenDialog1.FileName,
0);
ShowMessage(IntToStr(FileSeek(FileHandle,0,2)));
FileClose(FileHandle);
end;
5。TFileStream.Size
var
FS:
TFileStream;
begin
if
OpenDialog1.Execute
then
begin
FS
:=
TFileStream.Create(OpenDialog1.FileName,
fmShareDenyNone);
ShowMessage(IntToStr(FS.Size));
FS.Free;
end;
另在IdGlobalProtocols中有個FileSizeByName()的函數。

❾ delphi 如果我想獲得textfile文件的長度或者行數,那麼應該怎麼辦了

var filea:file of byte; fsize:integer;begin assignfile('txt.txt',filea); reset(filea); fsize:=filesize(filea);//獲得文件大小 end; end; 查看更多答案>>

❿ delphi讀取大容量txt並對其進行一個處理

我嘗試寫了一個過程,參數分別是要處理的文件InFile,結果文件OutFile,寬字元數量WideWordCount

procere DoText(const InFile, OutFile: string; WideWordCount: Integer);
var
FileStream: TFileStream;
OutList: TStringList;
TmpStr: string;
Cnt, ReadCnt: Integer;
begin
OutList := TStringList.Create;
FileStream := TFileStream.Create(InFile, fmOpenRead or fmShareDenyNone);
try
Cnt := WideWordCount * 2;
SetLength(TmpStr, Cnt);

while True do
begin
ReadCnt := FileStream.Read(TmpStr[1], Cnt);
if ReadCnt < Cnt then
begin
SetLength(TmpStr, ReadCnt);
OutList.Add(TmpStr);
Break;
end;
OutList.Add(TmpStr);
end;

OutList.SaveToFile(OutFile);
finally
OutList.Free;
FileStream.Free;
end;
end;

熱點內容
個人個稅賬號密碼是什麼 發布:2024-11-19 22:37:57 瀏覽:359
c語言二級試題及答案 發布:2024-11-19 22:35:07 瀏覽:937
pythonforin報錯 發布:2024-11-19 22:33:27 瀏覽:836
android系統資源 發布:2024-11-19 22:21:50 瀏覽:388
androidapi中文chm 發布:2024-11-19 22:15:25 瀏覽:804
安卓鏈接文件夾 發布:2024-11-19 21:49:35 瀏覽:269
GP演算法 發布:2024-11-19 21:48:05 瀏覽:181
如何打開安卓手機的隱藏空間 發布:2024-11-19 21:09:46 瀏覽:885
c語言strncmp 發布:2024-11-19 21:06:26 瀏覽:767
計算機二級c語言考點 發布:2024-11-19 21:05:08 瀏覽:767