cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
mladair
Level 2

Checking whether a file exists

Hoping somebody out there might be able to lend some assistance...

I have a Basic MSI install that I am developing that needs to check whether or not a particular file exists.

I'm using a custom dialog that prompts the user for a folder location (totally separate from the dialog which sets INSTALLDIR). Once they select the folder, I need to verify that it is the correct one by making sure a particular file exits. If the file doesn't exist, then it needs to prevent them from continuing with the install.

I don't believe (at least as I understand it) that System Search would work because this takes place before the user interface.

Any thoughts would be greatly appreciated.

Matt Adair
Labels (1)
0 Kudos
(2) Replies
RobertDickau
Flexera Alumni

Correct, the system search might not be the best approach; if you're not averse to InstallScript custom actions, the Is(FILE_EXISTS, ...) function might be of use.
0 Kudos
ITI_Randy
Level 6

You probably already have a property defined for the folder browse location in your dialog. Make sure you define a directory of the same name in the directory table.

It is a good idea to preset this directory for the browse with a system search. This will happen before the install begins and it will initialize the value for the folder, preventing errors in your browse. Also define another property like "MYFOLDERFOUND" that you can use for a flag.

The installscript to check the value will look something like this:

export prototype CheckForFile(HWND);

function CheckForFile(hMSI)
STRING sFolder, sFilename;
NUMBER nSize;
begin
try
MsiGetProperty(hMSI, "MYFOLDER", sFolder, nSize);
sFilename = sFolder ^ "SomeFile";

if (Is(FILE_EXISTS, sFilename) = 1) then
MsiSetProperty(hMSI, "MYFOLDER", sFolder );
MsiSetProperty(hMSI, "MYFILEFOUND", "True");
else
SprintfBox(SEVERE, "Invalid Folder", "File could not be located at %s", sFolder);
MsiSetProperty(hMSI, "MYFILEFOUND", "False");
endif;
catch
endcatch;
end;

On the dialog Next events, set the first action to be a call to the custom action above which will check the value. Then condition the New Dialog event which follows with MYFILEFOUND = "True".

If the user selects the correct path for the file, the value is true and they can move ahead in the dialogs. If they pick a wrong folder, the value is false and they get a message telling them it is wrong and they can't continue. They must either pick a correct folder or cancel.

You may also want to consider checking the value in a separate customer action very early in the install to see if system search was able to locate the file at the default location. If it did, you can avoid even showing the browse dialog at all.

Hope this helps.
0 Kudos