使用delphi中的shell循环目录中的文件

发布于 2024-11-28 05:41:13 字数 90 浏览 1 评论 0原文

我想循环遍历给定目录中的所有文件并返回它们的版本号和exe名称。我尝试深入研究 shell,看看是否可以解决这个问题,但是我一直无法找到解决方案。任何提示将不胜感激。

I want to loop through all the files in a given directory and return their version number and exe name. I have tried digging into the shell to see if I can pull this off, however I have not been able to find a solution. Any tips would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2024-12-05 05:41:14

这样做:

在表单上放置一个 TMemo 和一个 TButton 并执行

type
  TVerInfo = packed record
    vMajor, vMinor, vRelease, vBuild: word;
  end;

function GetFileVerNumbers(const FileName: string): TVerInfo;
var
  len, dummy: cardinal;
  verdata: pointer;
  verstruct: pointer;
const
  InvalidVersion: TVerInfo = (vMajor: 0; vMinor: 0; vRelease: 0; vBuild: 0);
begin
  len := GetFileVersionInfoSize(PWideChar(FileName), dummy);
  if len = 0 then
    Exit(InvalidVersion);
  GetMem(verdata, len);
  try
    GetFileVersionInfo(PWideChar(FileName), 0, len, verdata);
    VerQueryValue(verdata, '\', verstruct, dummy);
    result.vMajor := HiWord(TVSFixedFileInfo(verstruct^).dwFileVersionMS);
    result.vMinor := LoWord(TVSFixedFileInfo(verstruct^).dwFileVersionMS);
    result.vRelease := HiWord(TVSFixedFileInfo(verstruct^).dwFileVersionLS);
    result.vBuild := LoWord(TVSFixedFileInfo(verstruct^).dwFileVersionLS);
  finally
    FreeMem(verdata);
  end;
end;

function GetFileVer(const FileName: string): string;
begin
  with GetFileVerNumbers(FileName) do
    result := IntToStr(vMajor) + '.' +
      IntToStr(vMinor) + '.' + IntToStr(vRelease) + '.' +
      IntToStr(vBuild);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  path = 'C:\WINDOWS';
var
  SR: TSearchRec;
begin
  Memo1.Clear;
  if FindFirst(IncludeTrailingBackslash(path) + '*.exe', faAnyFile, SR) = 0 then
    try
      repeat
        Memo1.Lines.Add(SR.Name + #9 +
          GetFileVer(IncludeTrailingBackslash(path) + SR.Name));
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
end;

“截图”

This does it:

Drop a TMemo and a TButton on your form and do

type
  TVerInfo = packed record
    vMajor, vMinor, vRelease, vBuild: word;
  end;

function GetFileVerNumbers(const FileName: string): TVerInfo;
var
  len, dummy: cardinal;
  verdata: pointer;
  verstruct: pointer;
const
  InvalidVersion: TVerInfo = (vMajor: 0; vMinor: 0; vRelease: 0; vBuild: 0);
begin
  len := GetFileVersionInfoSize(PWideChar(FileName), dummy);
  if len = 0 then
    Exit(InvalidVersion);
  GetMem(verdata, len);
  try
    GetFileVersionInfo(PWideChar(FileName), 0, len, verdata);
    VerQueryValue(verdata, '\', verstruct, dummy);
    result.vMajor := HiWord(TVSFixedFileInfo(verstruct^).dwFileVersionMS);
    result.vMinor := LoWord(TVSFixedFileInfo(verstruct^).dwFileVersionMS);
    result.vRelease := HiWord(TVSFixedFileInfo(verstruct^).dwFileVersionLS);
    result.vBuild := LoWord(TVSFixedFileInfo(verstruct^).dwFileVersionLS);
  finally
    FreeMem(verdata);
  end;
end;

function GetFileVer(const FileName: string): string;
begin
  with GetFileVerNumbers(FileName) do
    result := IntToStr(vMajor) + '.' +
      IntToStr(vMinor) + '.' + IntToStr(vRelease) + '.' +
      IntToStr(vBuild);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  path = 'C:\WINDOWS';
var
  SR: TSearchRec;
begin
  Memo1.Clear;
  if FindFirst(IncludeTrailingBackslash(path) + '*.exe', faAnyFile, SR) = 0 then
    try
      repeat
        Memo1.Lines.Add(SR.Name + #9 +
          GetFileVer(IncludeTrailingBackslash(path) + SR.Name));
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
end;

Screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文