Determining if a file exists without erroring out - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

cecil@coral8.com
Posts: 18
Joined: Mon Oct 27, 2008 11:45 pm

Determining if a file exists without erroring out

Hello Caphyon,

I am trying to allow the user the option of selecting another
folder if the user selected folder already contains an installation.
I know a user cannot install multiple instances of the same
package, but if the new package has a different GUID, the user
may want to try an install in "\My Company\My Product" where
an existing install resides.

I need something that will detect if a single file already exists
using the user APPDIR selection from FolderDlg. If that file
exists, warn the user and allow him to go back to the FolderDlg
to select another installation folder.


The specific problem encountered is at install time. The msiexec log
reports:
... File: C:\My Company\My Product\AFileName.txt; Overwrite; Won't patch;
Existing file is unversioned and unmodified. File: ...
This is true. And the install silently fails. I want to prevent the
user from encountering this condition by checkng for a specific
filename I know will always exist in any installation. I want to
recover by allowing a different entry in FolderDlg.


The findFolder() function at
http://www.advancedinstaller.com/forums ... vbs#p18884
is close, but I would rather not error out. I would prefer to backup and
allow the user to select another folder.

Thank you for you GREAT support! :D
Cecil
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Determining if a file exists without erroring out

Hi Cecil,

You can try using a custom action to determine if the file exists or not. You can find some sample VBScript code here. Based on the presence of the file, you can set an installer property.

This property can then be used to prevent the user from installing in the selected location. The User Guide contains the Make sure an edit box is not empty how-to which may help you (instead of using the property of the editbox you can use the property set by the custom action).

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cecil@coral8.com
Posts: 18
Joined: Mon Oct 27, 2008 11:45 pm

Re: Determining if a file exists without erroring out

Still can't get the vbs return value in AI.

I have a CA defined as "File attached to installation" with a function name FindFile.
Other properties: Synchronous execution, check return code, immediate execution, always execute.

On the Next button of the FolderDlg, I want to have a conditional dialog displayed if a server.exe
is found under APPDIR. So in the Published Events:
...
DoAction findfile.vbs AI_INSTALL
...

findfile.vbs is:

' If the server file already exists, the user should uninstall or select another
' Ref: http://www.advancedinstaller.com/forums ... vbs#p18879
Function FindFile()
Dim appdir
appdir = Session.Property("APPDIR")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim server
server = appdir & "bin\server.exe"
If objFSO.FileExists(appdir & server) Then
Session.Property("SERVER_EXISTS") = "1"
FindFile = 1
Exit Function
End If
Session.Property("SERVER_EXISTS") = "0"
FindFile = 0
Exit Function
End Function
FindFile()

However, setting the Session.Property("SERVER_EXISTS") does not pass
the value back to AI.

How do I get AI to set a property based on the existence of a file?
Adding MsgBox traces to the vbs indicates it is functioning correctly.

Thanks for your assistance,
Cecil
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Determining if a file exists without erroring out

Hi Cecil,

Please note that the custom action should always return 0. Any other value (for example "1") may be interpreted as a failure. Also, you are verifying the string appdir & server which is resolved to appdir & appdir & "bin\server.exe". You can try modifying the code like this:

Code: Select all

Function FindFile()

  Dim appdir 
  appdir = Session.Property("APPDIR")
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Dim server 
  server = appdir & "bin\server.exe"

  If objFSO.FileExists(server) Then
    Session.Property("SERVER_EXISTS") = "1"
  Else
    Session.Property("SERVER_EXISTS") = "0"
  End If

End Function
After this custom action runs, the SERVER_EXISTS property should be set to 0 or 1. In order to conditionally show a custom dialog you can try the approach explained in these how-to's:
Conditionally show "StartMenuShortcutsDlg"
Conditionally show the "UpgradeDlg" dialog

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”