split output fullname

This topic contains 4 replies, has 3 voices, and was last updated by  russell karr 3 months, 3 weeks ago.

  • Author
    Posts
  • #73948

    russell karr
    Participant

    Hi all,

    I am trying to remove certain folders from the output fullname path.
    In short, instead of H:/main/demo/first/ I would like something like this, /main/demo/first

    Here is the code I am working with

     Function Get-Depth {
        Param(
            [String]$Path = 'H:\main\',
            [String]$Filter = "*",
            [Int]$ToDepth = 1,
            [Int]$CurrentDepth = 0
        )
        #incrimintation
        $CurrentDepth++
    
        Get-ChildItem $Path | %{
            $_ | ?{ $_.Name -Like $Filter }
            If ($_.PsIsContainer) {
                If ($CurrentDepth -le $ToDepth) {
     
                    # Call to function
                    #adds the filter values and depth to the path..
                    Get-Depth -Path $_.FullName -Filter $Filter `
                      -ToDepth $ToDepth -CurrentDepth $CurrentDepth
                }
            }
        }
       
    }
    Get-Depth|? {$_.PsIsContainer}| select @{Name='Date Modified'; Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}}, @{Name='Owner'; E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, @{Name='Structure'; E={(($_.FullName.Split('\'))[-1])}}
    

    Thank you!

  • #73949

    Richard Siddaway
    Moderator

    A couple of passes through split-path should do it – the drive and the filename will be stripped off just leaving the folder path

    PS> Get-ChildItem  .\HyperV\ -Recurse  | foreach {Split-Path -Path $_.Fullname  -Parent | Split-Path  -NoQualifier  }
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    \Scripts\HyperV\Setup
    

    BTW It is extremely bad practice to use aliases in your scripts ESPECIALLY ? and %

    • #73952

      russell karr
      Participant

      Hi Richard,

      Thank you for the feedback. Can you suggest any alternatives for the aliases ?

      Thanks

    • #73957

      Olaf Soyk
      Participant

      The recommendation for aliasses is: It's fine for the console for "quick 'n dirty" things, but do not use them in scripts. Never. 😉
      You can get a list of all alieasses with this:

      get-alias
    • #73964

      russell karr
      Participant

      @Olaf Soyak Thank you! I will make those changes immediately, btw, i was using them in scripts... sorry.

You must be logged in to reply to this topic.