2012-12-03 58 views
2

我試圖將所有數據庫文件從以前的安裝複製到具有新路徑名的新安裝。問題是安裝程序不知道數據庫文件的名稱,所以我試圖使用通配符。Inno Setup - FileCopy在路徑名中使用通配符

我嘗試使用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; 

FileCopy()

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

命令行

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

參見[Inno Setup的:在拷貝代碼部分的文件夾,子文件夾和文件遞歸(http://stackoverflow.com/q/ 33391915/850848) –

回答

3

您需要使用FindFirstFindNext,並通過FindClose的文件夾進行迭代。您獲取每個數據庫名稱,然後單獨複製它。在Pascal(Delphi)中這樣做的一個例子可以在here找到。還有在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; 

你可以改變上面的循環看在適當的舊文件夾的每個*.mdb(在FindFirst調用)和將計數的行更改爲將每個找到的文件複製到新文件夾的塊(使用FileCopyTFileStream,無論您喜歡哪一個)。

+0

太棒了!非常感謝你的工作。在查看您的文章後,我搜索了FindFirst,並找到了Inno Setup文檔。猜猜我看起來不夠硬。謝謝您的幫助! –

+0

有關完整代碼(包括遞歸),請參閱[Inno Setup:在代碼段中遞歸複製文件夾,子文件夾和文件](http://stackoverflow.com/q/33391915/850848)。 –

2

,如果你修改了一點你的命令行的嘗試可以工作:

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