-1

I would like to search thru files with .token files that have a string with the following pattern __[characters]__ and perform the following via PowerShell:

  • remove "__" (two underscores) in front of any character with an "#{"
  • remove "__" (two underscores) at the end of the same character and replace with "}"

For example:

__STAGE__
to
#{STAGE}

I am migrating RM token files to Octopus Deploy and need a clean up step via scripting.

Chaka
  • 1,709
  • 11
  • 33
  • 58
  • What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Feb 28 '18 at 14:34

1 Answers1

1

In the most basic way and assuming there's no problem fitting a token file into memory:

$TokenFiles = Get-ChildItem *.token

foreach ($file in $TokenFiles) {
    $NewContent = Get-Content $file.FullName -Raw
    $NewContent = $NewContent -replace '__(.*?)__', '#{$1}'
    Set-Content $file.FullName -Value $NewContent
}
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • What if you add variable for path and folder? Example: $folder = "Y:\CMGitWorkspace\" $product = "GAM" – Chaka Feb 28 '18 at 14:42
  • and perform search on "$folder\$product" – Chaka Feb 28 '18 at 14:43
  • 1
    @Chaka Try it. I'm not writing your complete script for you, I'm giving you a suggestion to answer your specific issue. "How do I use `Get-ChildItem`?" is a very different (and extremely basic) question compared "How do I search and replace text in a file?" – Bacon Bits Feb 28 '18 at 14:48