Search code examples
powershellpowershell-4.0

Powershell replace last two occurrences of a '/' in file path with '.'


I have a filepath, and I'm trying to remove the last two occurrences of the / character into . and also completely remove the '{}' via Powershell to then turn that into a variable.

So, turn this:

xxx-xxx-xx\xxxxxxx\x\{xxxx-xxxxx-xxxx}\xxxxx\xxxxx

Into this:

xxx-xxx-xx\xxxxxxx\x\xxxx-xxxxx-xxxx.xxxxx.xxxxx

I've tried to get this working with the replace cmdlet, but this seems to focus more on replacing all occurrences or the first/last occurrence, which isn't my issue. Any guidance would be appreciated!

Edit:

So, I have an excel file and i'm creating a powershell script that uses a for each loop over every row, which amounts to thousands of entries. For each of those entries, I want to create a secondary variable that will take the full path, and save that path minus the last two slashes. Here's the portion of the script that i'm working on:

Foreach($script in $roboSource)
   {
    $logFileName = "$($script.a).txt".Replace('(?<=^[^\]+-[^\]+)-','.')
   } 

$script.a will output thousands of entries in this format: xxx-xxx-xx\xxxxxxx\x{xxxx-xxxxx-xxxx}\xxxxx\xxxxx Which is expected.

I want $logFileName to output this: xxx-xxx-xx\xxxxxxx\x\xxxx-xxxxx-xxxx.xxxxx.xxxxx

I'm just starting to understand regex, and I believe the capture group between the parenthesis should be catching at least one of the '\', but testing attempts show no changes after adding the replace+regex.

Please let me know if I can provide more info.

Thanks!


Solution

  • A 2-step approach is simplest in this case:

    # Input string.
    $str = 'xxx-xxx-xx\xxxxxxx\x\{xxxx-xxxxx-xxxx}\xxxxx\xxxxx'
    
    # Get everything before the "{"
    $prefix = $str -replace '\{.+'
    
    # Get everything starting with the "{", remove "{ and "}", 
    # and replace "\" with "."
    $suffix = $str.Substring($prefix.Length) -replace '[{}]' -replace '\\', '.'
    
    # Output the combined result (or assign to $logFileName)
    $prefix + $suffix
    

    If you wanted to do it with a single -replace operation (with nesting), things get more complicated:

    Note: This solution requires PowerShell Core (v6.1+)

    $str -replace '(.+)\{(.+)\}(.+)',
      { $_.Groups[1].Value + $_.Groups[2].Value + ($_.Groups[3].Value -replace '\\', '.') }
    

    Also see the elegant PS-Core-only -split based solution with a negative index (to split only a fixed number of tokens off the end) in Mathias R. Jessen's helpful answer.