0

I was using .replace until I discovered it is case sensitive. So I am rewritng a line of code to use -replace instead.

Here is what is working, but is case sensitive:

$SourcePath = 'c:\scripts\test
$folder = 'c:\testing\test'
$sourceFullPath = 'c:\scripts\test\FolderToTest'
$sourceFileRelativePath = $sourceFullPath.Replace($SourcePath, "")
$destFullFilePath = $folder + $sourceFileRelativePath

Write-output $destFullFilePath
c:\testing\test\FolderToTest

How would I convert this to use -replace or is there a way to use the .net .replace case-insensitive?

Note: This section of code will be in a function so they will not be static. I put in examples for this post but they could be any file path.

Thanks!!

GreetRufus
  • 421
  • 2
  • 9
  • 19
  • possible duplicate of [Is there an alternative to string.Replace that is case-insensitive?](http://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that-is-case-insensitive) – Andrew Savinykh Jun 06 '13 at 20:22
  • possible duplicate of http://stackoverflow.com/questions/5549426/is-there-a-case-insensitive-string-replace-in-net-without-using-regex – Andrew Savinykh Jun 06 '13 at 20:22

1 Answers1

1

Unlike the Replace method which takes strings, the replace operator takes a regular expression pattern. $SourcePath needs to be escaped as it contains backslashes which are special regex characters.

$sourceFileRelativePath  = $sourceFullPath -replace [regex]::escape($SourcePath)
$destFullFilePath = Join-Path $folder $sourceFileRelativePath
Shay Levy
  • 121,444
  • 32
  • 184
  • 206