Looking for a better way to do multiple replace operations

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 11 months, 2 weeks ago.

  • Author
  • #32093
    Profile photo of Keith R.
    Keith R.

    Hi, I'm trying to find a better way to do a bunch of replace operations. This is part of a larger script that I've been tasked with creating. During this part of the script, the script reads a text file and populates variables that will be used to replace other items it reads later on from the same text file. An example of this would be a dynamic log file directory based on the other items it reads from the text file. The log path is created following a logic where the script reads the following information Change Number = "1234", the Department = "IT", the Division="EndUserSupport", and the Branch="Packaging", putting it all together so the log file location will be "C:\IT\EndUserSupport\Packaging\Logs\1234\". Later on as the script reads through the text file it may need to run an MSI installer where the log file location is set to *LDIR* (this is what will need to be replaced by the log file path) and the log file name is set to "*CHG*.MSI.install.log" where the *CHG* is to be replaced by the change number 1234.

    The items in the text file that need to be replaced are shown below, followed by the items they represent:
    Item to use in text file What it will be replaced by
    *LDIR* C:\[Department]\[Division]\[Branch]\Logs\[Change]
    *CDIR* C:\[Department]\[Division]\[Branch]\Cache
    *CHG* [Change #]
    *WINDIR* [Windows directory]
    *PKGDIR* [Directory script is running from]
    NOTE: The asterisks surround the names to ensure that an item like INSTALLDIR which contains LDIR doesn't have the replacement done to it.

    The code I'm using currently can be seen below. Any help cleaning this up would be greatly appreciated.

    $Executable = $Executable -replace "\*LDIR\*", "$strLog"
    $Executable = $Executable -replace "\*CDIR\*", "$strPkgCache"
    $Executable = $Executable -replace "\*CHG\*", "$intChange"
    $Executable = $Executable -replace "\*WINDIR\*", "$strWinDir"
    $Executable = $Executable -replace "\*PKGDIR\*", "$strMyDir"
    $Arguments = $Arguments -replace "\*LDIR\*", "$strLog"
    $Arguments = $Arguments -replace "\*CDIR\*", "$strPkgCache"
    $Arguments = $Arguments -replace "\*CHG\*", "$intChange"
    $Arguments = $Arguments -replace "\*WINDIR\*", "$strWinDir"
    $Arguments = $Arguments -replace "\*PKGDIR\*", "$strMyDir"
    $ValidateValue = $ValidateValue -replace "\*LDIR\*", "$strLog"
    $ValidateValue = $ValidateValue -replace "\*CDIR\*", "$strPkgCache"
    $ValidateValue = $ValidateValue -replace "\*CHG\*", "$intChange"
    $ValidateValue = $ValidateValue -replace "\*WINDIR\*", "$strWinDir"
    $ValidateValue = $ValidateValue -replace "\*PKGDIR\*", "$strMyDir"
    $WaitValue = $WaitValue -replace "\*LDIR\*", "$strLog"
    $WaitValue = $WaitValue -replace "\*CDIR\*", "$strPkgCache"
    $WaitValue = $WaitValue -replace "\*CHG\*", "$intChange"
    $WaitValue = $WaitValue -replace "\*WINDIR\*", "$strWinDir"
    $WaitValue = $WaitValue -replace "\*PKGDIR\*", "$strMyDir"
  • #32104
    Profile photo of Curtis Smith
    Curtis Smith

You must be logged in to reply to this topic.