2

How to get the 4th folder name and store it in a variable while looping through the files stored in a parent folder. For example, if the path is

C:\ParentFolder\Subfolder1\subfolder2\subfolder3\file.extension 
C:\ParentFolder\Subfolder1\subfolder2\subfolder4\file.extension 
C:\ParentFolder\Subfolder1\subfolder2\subfolder5\file.extension 

then subfolder2 name should be stored in a variable. Can any one please help me on this?

get-childitem $DirectorNane -Recurse | Where-Object{!($_.PSIsContainer)} | % {

    $filePath = $_.FullName

    #Get file name
    $fileName = Split-Path -Path $filePath -Leaf
}   $FileI = Split-Path -Path $filePath -Leaf

Thanks in advance!

a krishna
  • 85
  • 2
  • 8

2 Answers2

6

You can use the -split operator on the $filePath variable to get what you want.

$split = $filePath -split '\\'
$myvar = $split[3]

We use the double backslash to split with, because the first slash escapes the slash character to split the path by. Then, we can reference the part of the path we want in the array that gets generated in the "split" variable.

Additionally, you can solve this with a one liner using the following code:

$myvar = $filepath.split('\')[3]

This would ensure that you're always getting the fourth element in the array, but is a little less flexible since you can't specify what exactly you want based on conditions with additional scripting.

Bryce McDonald
  • 1,760
  • 12
  • 22
  • Why not use the `.split()` method directly? `$myvar = $filepath.split('\')[3]` –  May 07 '18 at 18:58
  • 1
    Personally I like the operator because then if I wanted to go back and use the `split` variable for something else, I could (for instance, if I wanted to get the third subfolder unless it was named `x`, then get the fourth, or something to that effect). For the OP's use case, this is also a viable option so feel free to edit my answer and add it in if you'd like. – Bryce McDonald May 07 '18 at 19:01
  • 1
    @BryceMcDonald both come in handy, you just [have to know the difference](https://stackoverflow.com/questions/23796959/powershell-split-vs-split-whats-the-difference) –  May 07 '18 at 19:10
  • 1
    @LotPings: My suggestion is to stick with the `-split` _operator_: it can do everything the `.Split` method can and much more, it avoids method-syntax pitfalls, and it avoids problems with changing behavior due to new .NET method overloads getting introduced - see https://stackoverflow.com/a/41905031/45375 – mklement0 May 07 '18 at 21:24
  • 1
    As for a one-liner with the `-split` _operator_: `$myvar = ($filepath -split '\\')[3]` – mklement0 May 07 '18 at 21:26
  • 1
    @mklement0 Again a nice and thorough explanation +1 It's a pity you can't favorite an answer directly. –  May 07 '18 at 21:47
0

If you are asking how to get the parent directory of the directory containing a file, you can call Split-Path twice. Example:

$filePath = "C:\ParentFolder\Subfolder1\subfolder2\subfolder3\file.extension"
$parentOfParent = Split-Path (Split-Path $filePath)
# $parentOfParent now contains "C:\ParentFolder\Subfolder1\subfolder2"
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • Thanks for your reply! The subfolder2 will have a unique value so i have to store the subfolder2 value in a variable and use this as input while consuming a webservice – a krishna May 07 '18 at 19:09
  • I was trying to guess at what you were trying to do. Do you really need the 4th element in the path, or do you need the parent of the parent of a file? (Your question does not say.) – Bill_Stewart May 07 '18 at 19:13