当前位置:首页 » 文件管理 » delphi读取文件夹文件

delphi读取文件夹文件

发布时间: 2022-03-31 13:13:47

A. delphi如何获得一个文件夹下所有文件的信息

方法1:先调用FindFirst启动列表,再循环调用FindNext获取文件名存入aa数组,最后调用FindClose关闭列表
方法2:直接使用TFileListBox控件,设置好Drive、Directory、FileType属性,然后访问其Items数组就能得到所有文件/子目录列表了,很简单

B. delphi 连续读取多个txt文件

完全可以实现,我写了一个函数,给分吧

procere search(dir:string);
var
targetpath:string;{目标路径名}
sr:TsearchRec;
strlist:TStringList;
str:string;
begin
try
{第一阶段:找出初始dir目录下的所有文件,
其中dir为公共变量,值由你自己去确定}
strlist:=TStringList.Create();
targetpath:=extractfilepath(dir);{分解出目标路径名}
//先找文件
if findfirst(dir,faAnyFile,sr)=0 then
repeat
if((sr.name<>'.')and(sr.name<>'..')and(sr.Attr<>fadirectory)then//排除父目录和本目录两个假文件只取文件}}
begin//如果是文件,就打开并加入了文本框里
strlist.LoadFromFile(targetpath+sr.name);//读取文件
str:=stringreplace(strlist.Tostring,#13,'',[rfReplaceAll]);//把回车符换掉,保证在同一行
form1.memo1.Lines.Add(str);//加入到文本框中
end;
until findnext(sr)<>0;

//找子目录
if findfirst(dir,faanyfile,sr)=0 then
repeat
if(sr.name<>'.')and(sr.name<>'..')and(sr.Attr=fadirectory) then//排除父目录和本目录两个假文件}
//排除文件}
search(targetpath+sr.name+'\*.*');{递归调用}
until findnext(sr)<>0;
finally
findclose(sr);
strlist.free;
end;

end;

C. delphi读取文本文件

procere TForm1.Button1Click(Sender: TObject);
var
tmplist: tstringlist;
i, j: integer;
tel: string;
begin
tel := '1300000000'; //指定电话号码 如果用edit1控件则改成 edit1.text
tmplist := tstringlist.Create; //创建
tmplist.LoadFromFile('文件路径'); //载入文本,路径自行确定
for i:= 0 to tmplist.Count - 1 do //循环
begin
if pos(tel, tmplist.Strings[i]) > 0 then //当查到电话号码所在行时
begin
j := 1;
while j < 11 do //循环10次
begin
memo1.lines.add(tmplist.Strings[i + j]); //将后10行记录写入memo
inc(j); //j递增1
end;
break;
end;
end;
tmplist.Free;
end;

//由于文本文件的内容示例没有,不知道排列,只能大致写一个,楼主可以根据文本的排列规则进行修改,不明处可以追问

D. delphi 获取根目录下的文件名及子目录下的文件名

给一个通用过程,直接调用,运行看是不是你想要的效果。
procereGetChildFileList(AStrings:TStrings;ASourFile,
FileName:string);//查找子目录
//AStrings存放路径,ASourceFile要查找的目录,FileName搜索的文件类型若指定类型,则'*.jpg'or'*.png'
var
sour_path,sour_file:string;
TmpList:TStringList;
FileRec,subFileRec:TSearchrec;
i:Integer;
begin
if(ASourFile,Length(ASourFile),1)<>''then
sour_path:=IncludeTrailingPathDelimiter(Trim(ASourFile))//在路径后面加上反斜杠
else
sour_path:=trim(ASourFile);
sour_file:=FileName;

ifnotDirectoryExists(sour_path)then
begin
AStrings.Clear;
exit;
end;
TmpList:=TStringList.Create;
TmpList.Clear;

ifFindFirst(sour_path+'*.*',faAnyfile,FileRec)=0then
repeat
if((FileRec.AttrandfaDirectory)<>0)then
begin
if((FileRec.Name<>'.')and(FileRec.Name<>'..'))then
GetChildFileList(AStrings,sour_path+FileRec.Name+'',sour_file);
end;
untilFindNext(FileRec)<>0;
FindClose(FileRec);

ifFindFirst(sour_path+FileName,faAnyfile,subFileRec)=0then
repeat
if((subFileRec.AttrandfaDirectory)=0)then
TmpList.Add(sour_path+subFileRec.Name);
untilFindNext(subFileRec)<>0;
FindClose(subFileRec);

fori:=0toTmpList.Count-1do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
end;

调用:
procereTForm2.SpeedButton5Click(Sender:TObject);
begin
GetChildFileList(ListBox1.Items,'D:Wyp','*.jpg');//目录自己定
GetChildFileList(ListBox1.Items,'D:Wyp','*.png');
end;

这里是将查找的目录存放在ListBox里的。

在加载List时,由于Item太多,所以有一定的延时,而不是卡死。

希望能帮到你。

E. delphi中怎样读取txt文件的内容

text 文件最能想到的读取方式是按行以字符串的方式读取,然后对读取的串进行分解抽取你想要的东西。
如果是以空格分隔的数值数据,则可逐个数据项读取到你设定的变量或数组元素中。
.............

F. delphi如何获得指定路径文件的文件名

//delphi 获取文件所在路径
ExtractFileDrive :返回完整文件名中的驱动器,如"C:"
ExtractFilePath:返回完整文件名中的路径,最后带“/”,如"C:/test/"
ExtractFileDir:返回完整文件名中的路径,最后不带“/” ,如"C:/test"
ExtractFileName:返回完整文件名中的文件名称 (带扩展名),如"mytest.doc"
ExtractFileExt 返回完整文件名中的文件扩展名(带.),如".doc"

ExtractRelativePath : 返回相对路径,定义如下:
function ExtractRelativePath(const BaseName, DestName: string): string;
使用测试下如:
SysUtils.ExtractRelativePath('C:/test','C:/Test/TestRelativePath'):返回TestRelativePath
SysUtils.ExtractRelativePath('C:/Test/TestRelativePath','C:/test'):返回'../TestRelativePath'
SysUtils.ExtractRelativePath('C:/Test/TestRelativePath/','C:/test'):返回'../../TestRelativePath'
ExtractShortPathName :返回短文件名,即8+3,文件名长八位,扩展名为3号,为保持DOS系统兼容而存在

若想获取的文件名不带路径,可用:
ChangeFileExt(TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename,''); 函数将扩展名改掉即可。

G. delphi能不断循环读取文件夹内生成的文件,又能中途关闭程序,代码如何编

这样:
做一个线程,在线程里写个读取过程
type

TMyThread = class(TThread);
private
procere ReadDirectory();
protected
procere Execute(); override;
constructor Create(Suspended: boolean); override;
end;

procere TMyThread.Create(Suspended: boolean);
begin
FreeOnTerminated := true;
end;

procere TMyThread.Execute();
begin
// 如果线程不终止,就不停的读,
while not Terminated do begin
// 在这个函数里读出来的数据你直接处理也行,发给主窗口也行,
// 怎么处理就是你自己的事了。
ReadDirectory('c:\');
// 等待时间看你自己的情况设置,也可以换用其它方法(比如事件)
Sleep(5);
end;
end;

主窗口里加两个按钮,Button1是创建线程,Button2是停止线程:
var
mtt: TMyThread = nil;
procere TForm1.Button1Click(Sender: TObject);
begin
if mtt = nil then begin
mtt := TMyThread.Create;
mtt.Resume();
end;
end;

procere TForm1.Button2Click(Sender: TObject);
begin
if mtt = nil then exit;
mtt.Terminate;
mtt := nil;
end;

H. delphi从文件夹中获取文件名

用FindFirst,FindNext,FindClose

procere SearchFileEx(const Dir, Ext: string; Files: TStrings);
var
Found: TSearchRec;
i: integer;
Dirs: TStrings;
Finished: integer;
StopSearch: Boolean;
begin
StopSearch := False;
Dirs := TStringList.Create;
Finished := FindFirst(Dir + '*.*', 63, Found);
while (Finished = 0) and not (StopSearch) do
begin
if (Found.Name <> '.') then
begin
if (Found.Attr and faDirectory) = faDirectory then
Dirs.Add(Dir + Found.Name)
else
if Pos(UpperCase(Ext), UpperCase(Found.Name)) > 0 then
Files.Add(Dir + Found.Name);
end;
Finished := FindNext(Found);
end;
FindClose(Found);
if not StopSearch then
for i := 0 to Dirs.Count - 1 do
SearchFileEx(Dirs[i], Ext, Files);
Dirs.Free;
end;
3
procere FindSubDir(DirName: string; FileString: TStrings);
var
searchRec: TsearchRec;
begin
//找出所有下级子目录。
if (FindFirst(DirName + '*.*', faDirectory, SearchRec) = 0) then
begin
if IsValidDir(SearchRec) then
FileString.Add(DirName + SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
FileString.Add(DirName + SearchRec.Name);
end;
end;
FindClose(SearchRec);
end;

function IsValidDir(SearchRec: TSearchRec): Boolean;
begin
if (SearchRec.Attr = 16) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
Result := True
else
Result := False;
end

I. 如何用delphi 通过FTP获取文件夹

使用DELPHI自带的INDY控件中的TFTP控件

热点内容
梦幻西游有什么脚本 发布:2024-05-04 09:33:43 浏览:717
I编程视频 发布:2024-05-04 09:33:31 浏览:377
java客户端程序 发布:2024-05-04 08:08:11 浏览:939
腾讯视频账号和密码哪里看 发布:2024-05-04 08:08:11 浏览:451
专网数据存储安全问题分析 发布:2024-05-04 07:33:28 浏览:131
如何获得打印机无线密码 发布:2024-05-04 06:44:59 浏览:418
上古诸神录哪里改密码 发布:2024-05-04 06:43:55 浏览:263
灌篮高手手游自动盖帽脚本 发布:2024-05-04 06:42:31 浏览:425
javajs引擎 发布:2024-05-04 06:37:33 浏览:798
javalist重复 发布:2024-05-04 06:19:27 浏览:511