delphi 如何获取同一目录下指定扩展名 的文件名
解决方案 »
- 如何动态获取窗体TForm.caption的方法?
- 如何批量把硬盘里的JPG 图片导入到MSSQL 中的image 字段中.
- 关于TFcLabel控件的安装
- 在线等待:如何让ListView具有像TreeView那样的展开收缩功能。
- MDAC的问题,装了高版本的,还总是报还是2.1版本以下的
- 我手头上有TVideoCap和TVideoDisp两个控件,请求哪里有它们接口说明的文档?
- 在connectstring配置时,测试成功, 在选择tablename时出现错误提示''数据提供程序或其它返回E_faildy状态“
- 有谁用过《XPMenu 控件》啊?
- 初学,关于RAS问题
- 拥有个人主页的同志,希望能帮一下我!
- delphi 性能 不如JAVA ?
- 高手帮忙
tls: TStringList;
FindFileData: TWIN32FindData;
FindHanle: THandle;
FilePath, FileExt: String;
begin
tls := TStringList.Create;
try
FillChar(FindFileData, SizeOf(TWIN32FindData), 0);
FilePath := 'x:\dir\'; //目录
FileExt := '.ext'; //扩展名
FindHanle := FindFirstFile(PAnsiChar(FilePath + '*' + FileExt), FindFileData);
if FindHanle <> INVALID_HANDLE_VALUE then begin
repeat
if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY then begin
//过滤掉属性为“目录”,即“文件”
tls.Add(FilePath + FindFileData.cFileName);
end;
until Not FindNextFile(FindHanle, FindFileData) ;
windows.FindClose(FindHanle);
end;
Memo1.Lines.Assign(tls);
finally
tls.Free;
end;
end;
procedure TForm1.Getfile(Directory,extname: string;var str:TStrings);
var
SearchRec: TSearchRec;
s:string;
begin
if Directory[Length(Directory)] <> '\' then
Directory := Directory + '\';
if FindFirst(Directory + '*.*' , faAnyFile, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and fadirectory=fadirectory) and (SearchRec.Name <> '..') and (SearchRec.Name <> '.') then
begin
end
else
begin
s:=ExtractFileExt(SearchRec.Name);
if s=extname then
str.Add(Directory+SearchRec.Name);
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
end;
var
F: File;
begin
ListBox1.Items := MakeFileList(ExtractFilePath(Application.Exename),'.tmp');
Edit1.Text := ListBox1.Items.Text;
AssignFile(F, (ExtractFilePath(Application.Exename)+'!Llst.ini'));ReName(F, (ExtractFilePath(Application.Exename)+(Edit1.Text)));
end;那里错了 求解本人菜鸟
vStringList := TStringList.Create;
sName:= 'D:\Dir\';
if FindFirst(sName+'*.db', faAnyFile and not faDirectory, vSearchRec)=0 then
begin
repeat
vStringList.Add(vSearchRec.Name);
until FindNext(vSearchRec) <> 0;
FindClose(vSearchRec);
end;
ComboBox1.Clear;
ComboBox1.Items.AddStrings(vStringList);
ComboBox1.Refresh;
vStringList.Free;
var
sch:TSearchrec;
isSearchfolder:Boolean;
begin
if FileExt='\\' then isSearchfolder:=true else isSearchfolder:=false;
Result:=TStringlist.Create;
if rightStr(trim(Path), 1) <> '\' then
Path := trim(Path) + '\'
else
Path := trim(Path);
if not DirectoryExists(Path) then
begin
Result.Clear;
exit;
end;
if FindFirst(Path +'*', faAnyfile,sch) = 0 then
begin
repeat
Application.ProcessMessages;
if ((sch.Name = '.') or (sch.Name = '..')) then Continue;
if isSearchfolder and DirectoryExists(Path+sch.Name) then
Result.Add(sch.Name);
if (not isSearchfolder) and ((UpperCase(extractfileext(Path+sch.Name))=UpperCase(FileExt)) or (FileExt='.*')) then
Result.Add(sch.Name);
until FindNext(sch) <> 0;
SysUtils.FindClose(sch);
end;
end;调用
procedure TForm1.Button1Click(Sender: TObject);
var
TempPngList:TStringList;
i:Integer;
begin
TempPngList:=TStringList.Create;
TempPngList:=GetFolderList('E:\','.png'); //路径自己改 第二个参数是后缀名
for i:=0 to TempPngList.Count-1 do
ShowMessage(TempPngList[i]);
end;
var
sch:TSearchrec;
isSearchfolder:Boolean;
begin
if FileExt='\\' then isSearchfolder:=true else isSearchfolder:=false;
Result:=TStringlist.Create;
if rightStr(trim(Path), 1) <> '\' then
Path := trim(Path) + '\'
else
Path := trim(Path);
if not DirectoryExists(Path) then
begin
Result.Clear;
exit;
end;
if FindFirst(Path +'*', faAnyfile,sch) = 0 then
begin
repeat
Application.ProcessMessages;
if ((sch.Name = '.') or (sch.Name = '..')) then Continue;
if isSearchfolder and DirectoryExists(Path+sch.Name) then
Result.Add(sch.Name);
if (not isSearchfolder) and ((UpperCase(extractfileext(Path+sch.Name))=UpperCase(FileExt)) or (FileExt='.*')) then
Result.Add(sch.Name);
until FindNext(sch) <> 0;
SysUtils.FindClose(sch);
end;
end;//调用
procedure TForm1.Button1Click(Sender: TObject);
var
TempPngList:TStringList;
i:Integer;
begin
TempPngList:=TStringList.Create;
TempPngList:=GetFolderList('E:\','.png'); //路径自己改 第二个参数是后缀名
for i:=0 to TempPngList.Count-1 do
ShowMessage(TempPngList[i]);
end;