简体   繁体   English

Inno Setup - FileCopy 在路径名中使用通配符

[英]Inno Setup - FileCopy use wildcard character in pathname

I'm trying to copy all database files over from a previous installation to a new installation, which has a new pathname.我正在尝试将所有数据库文件从以前的安装复制到具有新路径名的新安装。 The problem is that the installer will not know the names of the database files, so I'm trying to use a wildcard character.问题是安装程序不知道数据库文件的名称,所以我尝试使用通配符。

I tried using TFileStream.Create(), but this was searching for a single file, such as "*.mdb", and I kept getting an error saying it couldn't find that file.我尝试使用 TFileStream.Create(),但这是在搜索单个文件,例如“*.mdb”,并且我不断收到错误消息,指出找不到该文件。 I also tried using FileCopy(), but it seems to simply fail and move on.我也尝试过使用 FileCopy(),但它似乎只是失败并继续前进。 I even tried using Exec() to run it through command line, but it would just freeze the installation.我什至尝试使用Exec()通过命令行运行它,但它只会冻结安装。

I've searched online a long time for an answer and read through a lot of the documentation.我在网上搜索了很长时间以获得答案并阅读了大量文档。 I just need to know how I can use a wildcard character to copy files with unknown names.我只需要知道如何使用通配符复制名称未知的文件。 Below are examples of what I've tried.以下是我尝试过的示例。

TFileStream.Create() TFileStream.Create()

    OldDBs := 'C:\Users\seang\Desktop\Old\*.mdb';
    NewDBs := 'C:\Users\seang\Desktop\New\*.mdb';
    SourceDB:= TFileStream.Create(OldDBs, fmOpenRead);
    DestDB:= TFileStream.Create(NewDBs, fmCreate);
    DestDB.CopyFrom(SourceDB, SourceDB.Size);
    SourceDB.Free;
    DestDB.Free;

FileCopy()文件复制()

    FileCopy('C:\Users\seang\Desktop\Old\*.mdb', 'C:\Users\seang\Desktop\New\*.mdb', True);

Command Line命令行

    Exec('cmd.exe', 'COPY "C:\Users\seang\Desktop\Old\*.mdb" "C:\Users\seang\Desktop\New\*.mdb"', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);

You need to use FindFirst , FindNext , and FindClose to iterate through the folder.您需要使用FindFirstFindNextFindClose来遍历文件夹。 You get each database name, and then copy it individually.您获取每个数据库名称,然后单独复制它。 An example of doing that in Pascal (Delphi) can be found here .可以在此处找到在 Pascal (Delphi) 中执行此操作的示例。 There's also an example of using them in the InnoSetup help file, in the Support Functions Reference section on File System Functions :在 InnoSetup 帮助文件中还有一个使用它们的示例,在File System FunctionsSupport Functions Reference部分:

// This example counts all of the files (not folders) in the System directory.
var
  FilesFound: Integer;
  FindRec: TFindRec;
begin
  FilesFound := 0;
  if FindFirst(ExpandConstant('{sys}\*'), FindRec) then begin
    try
      repeat
        // Don't count directories
        if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
          FilesFound := FilesFound + 1;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
  MsgBox(IntToStr(FilesFound) + ' files found in the System directory.',
    mbInformation, MB_OK);
end;

You can change the loop above to look in the proper old folder for each *.mdb (in the FindFirst call) and change the line that counts to a block that copies each file found into the new folder (using either FileCopy or a TFileStream , whichever you prefer).您可以更改上面的循环以查看每个*.mdb的正确旧文件夹(在FindFirst调用中)并将计数的行更改为将找到的每个文件复制到新文件夹中的块(使用FileCopyTFileStream ,你喜欢哪个)。

如果您稍微修改一下,您的命令行尝试可以工作:

Exec('cmd.exe', '/c COPY "C:\Users\seang\Desktop\Old\*.mdb" "C:\Users\seang\Desktop\New"', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM