delphi获取文件夹大小
❶ 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 如何获取文件的大小和类型
示例代码如下:
首先,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;