eblazer
Posts: 4
Joined: Sat Dec 24, 2005 2:33 am

Edit File After Install

Hi,

I'd like to update a properties file with the [SourceDir] sometime after install. What's the best way to do this?

e.g.
I want to update this log4j.properties file with the location of where the application's log file should now be.

log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A2.maxFileSize=1GB
log4j.appender.A2.maxBackupIndex=10
log4j.appender.A2.File=[SourceDir]\logfile.log

becomes...

log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A2.maxFileSize=1GB
log4j.appender.A2.maxBackupIndex=10
log4j.appender.A2.File=C:\Program Files\My App\logfile.log

Thanks!
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi,
I'd like to update a properties file with the [SourceDir] sometime after install. What's the best way to do this?
You can achieve this by using a VBS script that search and replace text in your file. Here how to do this:

Add the file where you want to searches and replaces text in "Files and Folders" page. Switch to "Custom Actions" page and add "New Attached Custom Action" under the "InstallFinalize" standard action (if the "InstallFinalize" standard action is not visible make it visible by right clicking on "InstallExecuteSequence" and choosing "Show Standard Action->BeforeFinalize->InstallFinalize").

Use following options for the custom action:
1. Function Name: main.
2. Execute Properties: Synchronous execution, check return code.
3. Execution Options: Immediate execution.
4. Scheduling Options: Always Execute.
5. Execution Condition: Not Installed.

Here is the VBS script:

Code: Select all

Function main()
	appPath = Session.property("APPDIR")
	filePath = Session.property("APPDIR") + "yourfile.txt"
	
        'replace "[SourceDir]" with appPath
	ReplaceInFile( filePath), "[SourceDir]", appPath 
End function

Function ReplaceInFile(strFileToUse, strOldText, strNewText)
  Const ForReading = 1
  Const ForWriting = 2
  
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.OpenTextFile(strFileToUse, ForReading)
  
  strText = ""
  strFile = ""
  
  strText = objFile.ReadAll
  objFile.Close
  strFile = Replace(strText, strOldText, strNewText)
  
  Set objFile = objFSO.OpenTextFile(strFileToUse, ForWriting)
  objFile.WriteLine strFile
  objFile.Close
End Function
Note that [APPDIR] property gives you the path where the application is installed and the [SourceDir] property gives you the path from where the application is installing. This script searches in the file given by the filePath variable for the "[SourceDir]" text and replaces it with the text given by the appPath variable. Change the VBS script to fit your needs.

In your file change log4j.appender.A2.File=[SourceDir]\logfile.log text with log4j.appender.A2.File=[SourceDir]logfile.log because the [APPDDIR] or [SourceDir] properties already finish with a '\'.

Regards,
Gigi
________________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com
eblazer
Posts: 4
Joined: Sat Dec 24, 2005 2:33 am

Hi Gigi,

Thanks for the script. There were a few errors in it, but it got me going in the right direction. Still have one issue though; When this runs at the end of the install, the MsgBox only displays "log4j.properties" without the proper APPDIR path. This tells me that the Session.property part isn't working right. Any ideas?

Also, I'm using 3.3.1. Let me know if this is the reason it isn't working.

Code: Select all

Function main()
	On Error Resume Next

	Dim appPath
	Dim filePath

	appPath = Session.property("APPDIR")
	filePath = appPath + "log4j.properties"
	MsgBox filePath

	Const ForReading = 1
	Const ForWriting = 2

	Dim objFSO

	Set objFSO = CreateObject("Scripting.FileSystemObject")

	If objFSO.FileExists(filePath) = True Then
		Dim objFile
		Set objFile = objFSO.OpenTextFile(filePath, ForReading)

		Dim strText
		Dim strFile

		strText = objFile.ReadAll
		objFile.Close
		strFile = Replace(strText, "[SourceDir]", appPath)

		Set objFile = objFSO.OpenTextFile(filePath, ForWriting)
		objFile.WriteLine strFile
		objFile.Close
	End If
End Function
Thanks!
gigi
Posts: 2103
Joined: Tue Apr 11, 2006 9:55 am
Contact: Website

Hi,
When this runs at the end of the install, the MsgBox only displays "log4j.properties" without the proper APPDIR path
This because in the 3.3.1 version of Advanced Installer the [APPDIR] property does not exist. We introduce this property with the 3.4 version of AI. In the 3.3.1 version the path where the application is installed is given by the [TARGETDIR] property.

Regards,
Gigi
_________________
Gheorghe Rada
Advanced Installer Team
http://www.advancedinstaller.com

Return to “Common Problems”