當前位置:首頁 » 文件管理 » 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-04-23 16:48:44 瀏覽:573
河源中考成績查詢密碼是什麼 發布:2024-04-23 16:48:42 瀏覽:607
ipad解壓視頻文件 發布:2024-04-23 16:47:44 瀏覽:137
順序表是線性表的什麼存儲結構 發布:2024-04-23 16:32:28 瀏覽:454
腳本刷皮膚 發布:2024-04-23 16:12:25 瀏覽:858
共享一個ip伺服器 發布:2024-04-23 15:46:26 瀏覽:142
如何打開好萊客密碼鎖 發布:2024-04-23 15:46:22 瀏覽:162
我的世界演戲伺服器房間號 發布:2024-04-23 15:44:11 瀏覽:84
雲伺服器的帶寬有用嗎 發布:2024-04-23 15:39:10 瀏覽:124
老款安卓平板玩吃雞反應慢怎麼辦 發布:2024-04-23 15:20:17 瀏覽:571