locked
FolderPath Traversing (Powershell) RRS feed

  • Question

  • Hi All,

    For one of my idea's rattling in my head, I have a need to traverse a folder structure in my script. More specifically I want to go backwards. 

    I am not sure if there is a cmdlet for this already, but from my googling I was unable to find anything like that yet. So then I though maybe I could break up a folder path and loop it back doing checks.

    So what I had in mind trying this new convertfrom-string command, however it does not like the Delimiter of "\" so I cannot break it up. 

    Ok so maybe I should be clearer on what I am trying to achieve, I want to for example start off at \\root\level1\level2\level3 I want it to do checks going backwards, \\root\level1\level2; \\root\level1; \\root

    Any Idea's?

    Regards,

    Cupis


    • Edited by Cupis Tuesday, February 2, 2016 3:40 PM
    Tuesday, February 2, 2016 3:40 PM

Answers

  • Hi,

    Split-Path can help you here:

    http://ss64.com/ps/split-path.html


    • Marked as answer by Cupis Friday, February 5, 2016 12:11 PM
    Tuesday, February 2, 2016 3:55 PM
  • PS C:\scripts> $f=Get-Item  'C:\Windows\System32\WindowsPowerShell\v1.0\en-US'
    PS C:\scripts> $f.Parent.FullName
    C:\Windows\System32\WindowsPowerShell\v1.0
    PS C:\scripts> $f.Parent.Parent.FullName
    C:\Windows\System32\WindowsPowerShell
    PS C:\scripts> $f.Parent.Parent.Parent.FullName
    C:\Windows\System32
    PS C:\scripts>
    A clear question is required before you can see an answer.

    \_(ツ)_/


    • Edited by jrv Tuesday, February 2, 2016 3:58 PM
    • Marked as answer by Cupis Friday, February 5, 2016 12:11 PM
    Tuesday, February 2, 2016 3:58 PM

All replies

  • Your question is vague. You need specific technical definitions of "backwards" and "does not like the delimiter so I cannot break it up".

    I think you need a clear idea of what you actually want to do, rather than trying to describe the way you think it needs to be done.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 2, 2016 3:47 PM
  • Hi,

    Split-Path can help you here:

    http://ss64.com/ps/split-path.html


    • Marked as answer by Cupis Friday, February 5, 2016 12:11 PM
    Tuesday, February 2, 2016 3:55 PM
  • PS C:\scripts> $f=Get-Item  'C:\Windows\System32\WindowsPowerShell\v1.0\en-US'
    PS C:\scripts> $f.Parent.FullName
    C:\Windows\System32\WindowsPowerShell\v1.0
    PS C:\scripts> $f.Parent.Parent.FullName
    C:\Windows\System32\WindowsPowerShell
    PS C:\scripts> $f.Parent.Parent.Parent.FullName
    C:\Windows\System32
    PS C:\scripts>
    A clear question is required before you can see an answer.

    \_(ツ)_/


    • Edited by jrv Tuesday, February 2, 2016 3:58 PM
    • Marked as answer by Cupis Friday, February 5, 2016 12:11 PM
    Tuesday, February 2, 2016 3:58 PM
  • Sorry Everyone, its one of those times where it seems clear in my own head, but it wont be clear to others.

    "does not like the delimiter so I cannot break it up" - Here I mean I tried using a ConvertFrom-String command to turn "\\Root\Level1\Level2\Level3" into objects, like "root" "level1" "level2" "level3" which I can then work with separately. However the ConvertFrom-String does not like "\" as a delimiter and errors out.

    When I mean Backwards, I mean if someone gives me "\\Root\Level1\Level2\Level3" I can then do checks going back to root, ie checking the size of each level, check the number of files within each level, checking the access controls etc etc. so For example, I do a check on Level3, then I do a check on Level2, then Level1, then Root.

    Does that makes sense?

    So far Jrv's script could do what I want it to do and SplitPath, Ill give them a try. 

    With the new explanation if you have different recommendations, let me know


    Wednesday, February 3, 2016 5:31 AM
  • Hi Cupis,

    either JRVs method of using the parent property as well as Mike's Split-Path-Solution should cover all your needs in this scenario. That said, to split a string by "\", you can work like this:

    $c = "\\Root\Level1\Level2\Level3"
    
    # Method 1
    $c.Split("\")
    
    # Method 2
    $c -Split "\\"

    Note: This is not a good solution to handle paths, both above mentioned methods are superior. However, this is how you can split a string by any given delimiter.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Wednesday, February 3, 2016 8:11 AM
  • Searching can be helpful. For example:

    http://www.google.com/search?&q=powershell+split+path+string

    Right away you will get a result that says the cmdlet to use would be Split-Path.


    -- Bill Stewart [Bill_Stewart]

    • Edited by Bill_Stewart Wednesday, February 3, 2016 3:31 PM
    Wednesday, February 3, 2016 1:41 PM
  • Thanks; the machine I replied on only had PowerShell v3. But still, Split-Path would be the correct cmdlet.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 3, 2016 3:32 PM
  • Try this

    $Dir = "\\root\level1\level2\level3"

    While ($Dir.split ("\").count -gt 1)

    {

    Write-host "Working on: $Dir"

    $Dir = $Dir | Split-path

    }

    Add your code to replace the write-host and reference $dir

    Wednesday, February 3, 2016 8:56 PM
  • Thanks Everyone, you have helped tremendously. 

    I have been missing around with both the split path and the Parent aspect of the items and I think I can use them to do what I want.

    Google is brilliant when you know what to ask, but sometimes you just not typing the right thing to get the answer you want. As you guys pointed out on my original Post 

    Friday, February 5, 2016 12:15 PM