Replace string in the Plain text file.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace string in the Plain text file.

Raghu12
This post has NOT been accepted by the mailing list yet.
I have a requirement in which while installing some text needs to be replace.
If it is XML, then I can use xmlFile/xmlConfig to manipulate, but this is plain text file with 3 lines of information.

Ex:
File: TextFile.txt
You must first_second subscribe

Need to replace "first_second" with "first"
Reply | Threaded
Open this post in threaded view
|

Re: Replace string in the Plain text file.

Raghu12
This post has NOT been accepted by the mailing list yet.
Updating the question with more information.

<Property Id="POWERSHELLEXE">
      <RegistrySearch Id="POWERSHELLEXE" Type="raw" Root="HKLM" Name="Path"
                    Key="SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" />
    </Property>
    <Condition Message="This application requires Windows PowerShell.">
<![CDATA[Installed OR POWERSHELLEXE]]>
</Condition>
   
    <CustomAction Id="CACmdLine" Property="CACmd" Value="[POWERSHELLEXE] (Get-Content 'c:\Sample.txt').replace('first_second', 'first') | Set-Content 'c:\Sample.txt'" />

    <CustomAction Id="CACmd" BinaryKey="WixCA" DllEntry="CAQuietExec64" Return="check" Execute="deferred" />
    <InstallExecuteSequence>
      <Custom Action="CACmdLine" Before="ProcessComponents">NOT Installed</Custom> 
      <Custom Action="CACmd" After="CACmdLine">NOT Installed</Custom>
    </InstallExecuteSequence>

When I install, It is not throwing any error but it is saying Installation failed. Can someone help me where I'm doing wrong.
Reply | Threaded
Open this post in threaded view
|

Re: Replace string in the Plain text file.

Namrata
This post has NOT been accepted by the mailing list yet.
In reply to this post by Raghu12
To replace a string in plain text file, we need to execute CustomAction dll written in Dot Net as follows:
[CustomAction]
public static ActionResult SearchAndReplaceInit(Session session)
{
  session.Log("Begin SearchAndReplaceInit");
  File.Delete(SearchAndReplaceDataFile);
  if (session.Database.Tables.Contains("SearchAndReplace"))
  {
     var lstSearchAndReplace = new List<SearchAndReplaceData>();
     using (var propertyView =
      session.Database.OpenView("SELECT * FROM `SearchAndReplace`"))
     {
       propertyView.Execute();
       foreach (var record in propertyView)
       {
         var token = new SearchAndReplaceData
         {
           File = session.Format(record["File"].ToString()),
           Search = session.Format(record["Search"].ToString()),
           Replace = session.Format(record["Replace"].ToString())
         };
         lstSearchAndReplace.Add(token);
       }
     }
     var serializer = new TypedXmlSerializer<List<SearchAndReplaceData>>();
     serializer.Serialize(SearchAndReplaceDataFile, lstSearchAndReplace);
  }
  else
  {
    session.Log("No SearchAndReplace custom table found");
  }
  session.Log("End SearchAndReplaceInit");
  return ActionResult.Success;
}