1

We are trying to break the following Path from the text file, the path is dynamic in length, i.e, it can be ABC\DSD\AAR\ARE or ABC\DSD.

So we need a solution to break down the path as per the following example.

ABC\DSD\AAR

to

ABC$
ABC\\DSD$
ABC\\DSD\\AAR\\*

The Last element should end with \\*. However all parent elements will end with $ and it should increment until last element is reached and write to file via Out-File module.

May we ask your help to solve this problem through Powershell?

Thank you,

Rokr1

Thomas
  • 4,225
  • 5
  • 23
  • 28
rokr1
  • 13
  • 2

1 Answers1

0

Following would do

$x = ""; ("ABC\DSD\AAR" -split '\\' | % {$x = "$($x)$($_)\"; $x -replace '\\$', '$'}) -join " " -replace '\$$', '\*'

or a bit more longwinded

$x = ""
("ABC\DSD\AAR" -split '\\' | 
    Foreach-Object  {
        $x = "$($x)$($_)\"
        $x -replace '\\$', '$'
    }
) -join " " -replace '\$$', '\*'
  • Thank you Mr. Lieven, That works, however there are list of paths like $i = @('ABC\DSD\AAR','ABC\ZRS\ASK','KAS\DSR\BBX') also the output should be; ABC$ #Line 1 ABC\\DSD$ #Line 2 ABC\\DSD\\AAR\\* #Line 3 All in new line – rokr1 Jun 06 '18 at 04:57
  • @rokr1 - `$x="";$x=("ABC\DSD\AAR" -split '\\' | % {$x = "$($x)$($_)";"$($x)$";$x = "$($x)\\"});$x[-1]=$x[-1] -replace '\$$', '\\*';$x` – Lieven Keersmaekers Jun 06 '18 at 05:34
  • 1
    Okay Adding Function works [array]$i = @('abc\abd\ssd','add\frd\sdr') function getdata($s){ $x = "" (($s -split '\\' | % {$x = "`n$($x)$($_)\"; $x -replace '\\$', '$'}) -join " " -replace '\$$', '\*') -replace '\\', '\\'; } foreach($s in $i ){ getdata($s) } – rokr1 Jun 06 '18 at 05:49