Sea*_*ers 2 delphi pascal inno-setup wildcard file-copying
我正在尝试将所有数据库文件从以前的安装复制到具有新路径名的新安装。问题是安装程序不知道数据库文件的名称,所以我尝试使用通配符。
我尝试使用 TFileStream.Create(),但这是在搜索单个文件,例如“*.mdb”,并且我不断收到错误消息,指出找不到该文件。我也尝试过使用 FileCopy(),但它似乎只是失败并继续前进。我什至尝试使用Exec()通过命令行运行它,但它只会冻结安装。
我在网上搜索了很长时间以获得答案并阅读了大量文档。我只需要知道如何使用通配符复制名称未知的文件。以下是我尝试过的示例。
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;
Run Code Online (Sandbox Code Playgroud)
文件复制()
FileCopy('C:\Users\seang\Desktop\Old\*.mdb', 'C:\Users\seang\Desktop\New\*.mdb', True);
Run Code Online (Sandbox Code Playgroud)
命令行
Exec('cmd.exe', 'COPY "C:\Users\seang\Desktop\Old\*.mdb" "C:\Users\seang\Desktop\New\*.mdb"', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
Run Code Online (Sandbox Code Playgroud)
您需要使用FindFirst、FindNext和FindClose来遍历文件夹。您获取每个数据库名称,然后单独复制它。可以在此处找到在 Pascal (Delphi) 中执行此操作的示例。在 InnoSetup 帮助文件的以下Support Functions Reference部分中还有一个使用它们的示例File System Functions:
// 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;
Run Code Online (Sandbox Code Playgroud)
您可以更改上面的循环以查看每个*.mdb (在FindFirst调用中)的正确旧文件夹,并将计数的行更改为将找到的每个文件复制到新文件夹中的块(使用FileCopy或TFileStream,无论您喜欢哪个)。