I want to make an installer for an old program which comes on 2 CDs and I want to install the files directly from the discs.
At start up the setup should check if a certain file exists which means the first CD is inserted into the cd rom drive.
This is the code for that task:
[Files]
Source: {code: ??? }; Destination: {app}; flags:external;
[Code]
procedure InitializeWizard();
begin
if not FileExists('A:\Resource\CD1.GOB') xor
FileExists('B:\Resource\CD1.GOB') xor
// and so on, for every drive letter...
FileExists('Z:\Resource\CD1.GOB') then
Repeat
if MsgBox('Insert the first CD!', mbInformation, MB_OKCANCEL) = IDCANCEL then
ExitProcess(0);
Until FileExists('A:\Resource\CD1.GOB') or
FileExists('B:\Resource\CD1.GOB') or
// going through all letters again...
FileExists('Z:\Resource\CD1.GOB') = true;
So this works as intended. If the CD is not inserted and thus the file cannot be found a message will be shown which asks the user to insert the CD.
But I am wondering if there is a better way to increment the drive letter, because this is quite a mess.
And second, how can I save the full filepath und pass it on to the [Files] section?
I hope you can help me with this!
UPDATE:
I tried it again and came up with this:
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageId = wpWelcome then
begin
WizardForm.NextButton.Enabled := False;
repeat
for i:=0 to 31 do
dstr := (Chr(Ord('A') + i) + ':\Resource\CD1.gob');
until FileExists(dstr);
WizardForm.NextButton.Enabled := True;
end;
end;
But using this code Setup freezes at the beginning and doesn't respond even if the CD is already inserted.
Something like this should do what you need:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Files]
Source: {code:GetFileSource}; DestDir: {app}; flags:external;
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
type
TDriveType = (
dtUnknown,
dtNoRootDir,
dtRemovable,
dtFixed,
dtRemote,
dtCDROM,
dtRAMDisk
);
TDriveTypes = set of TDriveType;
function GetDriveType(lpRootPathName: string): UINT;
external 'GetDriveType{#AW}@kernel32.dll stdcall';
function GetLogicalDriveStrings(nBufferLength: DWORD; lpBuffer: string): DWORD;
external 'GetLogicalDriveStrings{#AW}@kernel32.dll stdcall';
var
FileSource: string;
#ifndef UNICODE
function IntToDriveType(Value: UINT): TDriveType;
begin
Result := dtUnknown;
case Value of
1: Result := dtNoRootDir;
2: Result := dtRemovable;
3: Result := dtFixed;
4: Result := dtRemote;
5: Result := dtCDROM;
6: Result := dtRAMDisk;
end;
end;
#endif
function GetLogicalDrives(var ADrives: array of string;
AFilter: TDriveTypes): Integer;
var
S: string;
I: Integer;
DriveRoot: string;
begin
Result := 0;
SetArrayLength(ADrives, 0);
I := GetLogicalDriveStrings(0, #0);
if I > 0 then
begin
SetLength(S, I);
if GetLogicalDriveStrings(Length(S), S) > 0 then
begin
S := TrimRight(S);
I := Pos(#0, S);
while I > 0 do
begin
DriveRoot := Copy(S, 1, I - 1);
#ifdef UNICODE
if (AFilter = []) or
(TDriveType(GetDriveType(DriveRoot)) in AFilter) then
#else
if (AFilter = []) or
(IntToDriveType(GetDriveType(DriveRoot)) in AFilter) then
#endif
begin
SetArrayLength(ADrives, GetArrayLength(ADrives) + 1);
#ifdef UNICODE
ADrives[High(ADrives)] := DriveRoot;
#else
ADrives[GetArrayLength(ADrives) - 1] := DriveRoot;
#endif
end;
Delete(S, 1, I);
I := Pos(#0, S);
end;
Result := GetArrayLength(ADrives);
end;
end;
end;
function GetFileSource(Value: string): string;
begin
// file source path passed to the [Files] section
Result := FileSource;
end;
procedure InitializeWizard;
var
I: Integer;
DriveCount: Integer;
DriveArray: array of string;
begin
// the function will fill the DriveArray only with CDROM
// drives and returns the count of found drives
DriveCount := GetLogicalDrives(DriveArray, [dtCDROM]);
// here you have an array of CD-ROM drives so iterate it
// search for a file you need and when you find it, pass
// the path to the FileSource variable, which will later
// be queried to get the source to the file in [Files]
for I := 0 to DriveCount - 1 do
begin
if FileExists(DriveArray[I] + 'Resource\CD1.GOB') then
begin
FileSource := DriveArray[I] + 'Resource\CD1.GOB';
Break;
end;
end;
MsgBox('File was found on path: ' + FileSource, mbInformation, MB_OK);
end;