如何遍历文件夹中的文件并得到类似文件名。

wbyniao 2008-12-14 06:55:46
举例:我有一个目录里 D:\目录\ 下面的文件 49088-1,49088-2,49088-3,49088-4,49088-5,49088-6,49089-1,49089-2,49089-3,49089-4,49089-5,49089-6 这样的文件 我数据库里存的是 49088 ,49089 的数 我怎么样才根据数据库的数据 查询出来对应的所有文件并得到文件名。
...全文
199 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JPEXE 2008-12-14
  • 打赏
  • 举报
回复
给你提供一下文件搜索函数,再对搜出来的文件名做个简单的字符串处理就能得到你要的数了.
可以这样处理 LeftStr('49088-1', Pos('-', '49088-1') - 1);

{------------------------------------------------------------------------------------
名称: SearchFiles
功能: 搜索指定目录下的文件
参数: strSearchFiles: TStringList - 输出搜索到的文件路径结果列表(需要调用者负责初始化和释放)
strDir: string - 指定要搜索的目录
strExt: string - 指定要搜索的文件类型(默认搜索所有类型 *.*)
strExceptFiles: TStringList - 指定排除的文件名列表(如 Desktop.ini, Thumbs.db 等)
返回: Cardinal - 返回搜索到的文件个数
说明: 只搜索当前一级目录, 不搜索子目录, 需自行修改为递归调用.
-------------------------------------------------------------------------------------}
function SearchFiles(out strSearchFiles: TStringList; const strDir: string; const strExt: string = '*.*'; const strExceptFiles: TStringList = nil): Cardinal;
var
search: TSearchRec;
ret: Integer;
count: Integer;
i: Integer;
bIsFoundExcept: Boolean;
begin
// 先检要搜索的目录是否存在
if not DirectoryExists(strDir) then
begin
Result := 0;
Exit;
end;

// 开始搜索
ret := FindFirst(strDir + '\' + strExt, faAnyFile, search);
if ret <> 0 then
begin
Result := 0;
Exit;
end;
while ret = 0 do
begin
// 排除文件夹
if (search.Name = '.') or
(search.Name = '..') or
(search.Attr = faDirectory) then
begin
ret := FindNext(search);
Continue; // 跳过
end;

// 排除指定文件
if strExceptFiles <> nil then
begin
count := strExceptFiles.Count;
if count > 0 then
begin
bIsFoundExcept := False;
for i := 0 to count - 1 do
begin
if LowerCase(search.Name) = LowerCase(strExceptFiles[i]) then
begin
bIsFoundExcept := True;
Break;
end;
end;
if bIsFoundExcept then
begin
ret := FindNext(search);
Continue; // 跳过
end;
end;
end;

// 添加到结果
if RightStr(strDir, 1) <> '\' then
begin
strSearchFiles.Add(strDir + '\' + search.Name);
end
else
begin
strSearchFiles.Add(strDir + search.Name);
end;

// 继续搜索
ret := FindNext(search);
end;
FindClose(search);

Result := strSearchFiles.Count;
end;
wbyniao 2008-12-14
  • 打赏
  • 举报
回复
没人能帮我吗?
wbyniao 2008-12-14
  • 打赏
  • 举报
回复
自己顶下 大家帮忙啊

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧