sfaust
Posts: 48
Joined: Tue Oct 13, 2015 11:57 pm

Text File Ensure Line Occurs Once

Fri Dec 06, 2019 12:48 am

I am trying to update a text file through the installer but with a slight twist. I need to ensure that the file exists and that a line exists, but exists only once. End user machines may or may not have the file, and if they do the line may or may not exist. If I do an append it will add it to the file regardless correct? so for users that don't have the file or line that would work but for those that do I would end up with the line multiple times correct? i.e.:

**other stuff**
**my line**
**my line**

How can I tell it to skip if the text already exists in the file? One way that I thought about is to put a text file update with a 'replace' that replaces my next with an empty string, effectively deleting it if it's there, then an append to add it. My questions on this are:
  • Do text file updates happen dependably in the order listed? If not this won't work because the replace could come after the append.
  • Ideally it would also REMOVE the line when it uninstalls. This isn't completely required but would be nice, is it possible?
  • This seems a bit hackish... is there a better way I'm not seeing?
Thanks for any advice you can give!

Catalin
Posts: 6542
Joined: Wed Jun 13, 2018 7:49 am

Re: Text File Ensure Line Occurs Once

Fri Dec 06, 2019 1:42 pm

Hello Steve,

Unfortunately, I am afraid we do not have predefined support for your scenario (i.e. to check if a piece of text exists in a file).

You can still achieve what you want. However, a custom approach will be needed.

First of all, we will need a custom action that will achieve what you want (open a file and look for the piece of text in it). Based on the results, this custom action will need to set a property (e.g. if the text exists, set MY_PROP to "1", else set it to "0"):

How to get/set installer properties using custom actions

Now, if you go to "Files and Folders" page and double click on your "Text File Update" operation, you can notice that this will be run on a component install. Basically, if the component is installed, the operation will be run. Therefore, conditioning the component will further condition the run of the text file update.

What we will need here is a dummy component (the dummy component can be created by adding a dummy file to your project - e.g. an empty text file) whose installation can be conditioned by the result of our custom action.

After creating the dummy component, you can go to "Organization" page --> under "Installation Behavior" --> select "Not installed" by default and "Installed if:" to your condition. In the sample I have give above, we will need the MY_PROP = "0" condition (because we need to execute the text file update only if the text is not present within the file)

Now that we have conditioned the installation of the component (therefore the file update operation), you can go to "Files and Folders" page --> double click on the text file update --> under "Install" section, expand the "Component" drop-down and select the dummy component.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

sfaust
Posts: 48
Joined: Tue Oct 13, 2015 11:57 pm

Re: Text File Ensure Line Occurs Once

Tue Dec 10, 2019 9:04 pm

Ok thank you. If I'm doing a custom action, though, should I just make the text update inside the custom action?

sfaust
Posts: 48
Joined: Tue Oct 13, 2015 11:57 pm

Re: Text File Ensure Line Occurs Once

Wed Dec 11, 2019 1:46 am

For anyone else that comes across this I was able to get this working by doing the modifications inside the action itself, I just needed to run the action as deferred and with full permissions because it was modifying files under Program Files.

Catalin
Posts: 6542
Joined: Wed Jun 13, 2018 7:49 am

Re: Text File Ensure Line Occurs Once

Thu Dec 12, 2019 9:49 am

Hello Steve,
Ok thank you. If I'm doing a custom action, though, should I just make the text update inside the custom action?
You are always welcome. And yes, you can do the text update inside the custom action as well, but it is not a must.
For anyone else that comes across this I was able to get this working by doing the modifications inside the action itself, I just needed to run the action as deferred and with full permissions because it was modifying files under Program Files.
Thank you for the followup on this and for sharing your solution with us. I am sure this will come in handy for further users facing a similar scenario.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”