cane someone Help me a bit.
I want to read a Text file, Search for $$I and replace it with the current Dir Name, Then write the file again.
Well something does not work 🙁
$content = [IO.File]::ReadAllText("CC.comp")
$content = $content -replace '$$I','Split-Path -Path (Get-Location) -Leaf'
Print $content
pause
>Solution :
-replace uses regex and $ is a special regex character which needs to be escaped with \ if you want to match it literally:
'$$I' -replace '$$I', 'test' # Nothing is replaced
'$$I' -replace '\$\$I', 'test' # Outputs 'test'
However, much easier is to use the .Replace method for literal replacement, no escaping is needed:
(Get-Content CC.comp -Raw).Replace('$$I', (Split-Path $pwd -Leaf)) |
Set-Content CC.comp