locked
Extract all msp filename and parent directory (the one where the msp is) RRS feed

  • Question

  • Hi,

    I have some msps in different folders

    ...path\path\update\1710\filea.msp

    ...path\path\update\1720\fileb.msp

    By running that commandline, I would get Filea.msp, fileb.msp and so on.

    ...

    Get-ChildItem -path "\\xxx\Temp\Logiciels\Consolexx\Sources\Updates" -include *.msp -recurse | Where-object {!$_.psIsContainer -eq $true} | ForEach-Object -Process {$_.FullName}

    But I would like to get 1710\filea.msp, 1720\fileb.msp and so on.

    How may I achieve this goal?

    Thanks,

    Saturday, March 3, 2018 3:30 AM

Answers

  • If I understand your question correctly, you only want the last portion of the FullName of your files:

    Get-ChildItem -path  "\\xxx\Temp\Logiciels\Consolexx\Sources\Updates" -include *.msp -recurse -File |
    ForEach-Object {
      [PSCustomObject]@{
        'Input' = $_.FullName;
        'Output1' = Join-Path (split-path $_.DirectoryName -Leaf) $_.name;  # Add the parent directory name
        'Output2' = ($_ -split '\\' | select -Skip 6) -join '\'    # With subdirectories below 6 deep
      }
    }

    The example above gives you 2 output options:

    1. Get the parent directory and the filename
    2. Get the subdirectories below 6 levels deep and the filename

    As I was not sure what you needed, I included both options to choose from.




    • Edited by F. Van Roie Saturday, March 3, 2018 9:30 AM Code cleanup
    • Marked as answer by FRacine Saturday, March 3, 2018 10:42 AM
    • Unmarked as answer by FRacine Saturday, March 3, 2018 10:45 AM
    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    Saturday, March 3, 2018 9:25 AM
  • Hi,

    -f is just a formatting parameter for making the code more readable and maintainable. It will replace those numbers in curly braces (like {0 and {1} within that string) with the values of the parameters following it. Given:

    "{0}\{1}" -f (split-path $_.DirectoryName -Leaf), $_.name
    • {0} gets replaced by the result from (split-path $_.DirectoryName -Leaf)
    • {1} gets replaces by $_.name

    The result is a concatenation of the two values and a backslash in the middle of it. I updated the code to use Join-Path instead as it actually does the same thing and makes more sense:

    Join-Path (split-path $_.DirectoryName -Leaf) $_.name;
    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    Saturday, March 3, 2018 2:45 PM
  • Another way...


    $path = '\\xxx\Temp\Logiciels\Consolexx\Sources\Updates'
    Get-ChildItem -file -path $path -include *.msp -recurse |
    ForEach-Object {
    $parent = $_.Directory.Parent
    "$parent\$_"
    }





    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    • Edited by JS2010 Saturday, March 3, 2018 8:45 PM
    Saturday, March 3, 2018 3:34 PM

All replies

  • If I understand your question correctly, you only want the last portion of the FullName of your files:

    Get-ChildItem -path  "\\xxx\Temp\Logiciels\Consolexx\Sources\Updates" -include *.msp -recurse -File |
    ForEach-Object {
      [PSCustomObject]@{
        'Input' = $_.FullName;
        'Output1' = Join-Path (split-path $_.DirectoryName -Leaf) $_.name;  # Add the parent directory name
        'Output2' = ($_ -split '\\' | select -Skip 6) -join '\'    # With subdirectories below 6 deep
      }
    }

    The example above gives you 2 output options:

    1. Get the parent directory and the filename
    2. Get the subdirectories below 6 levels deep and the filename

    As I was not sure what you needed, I included both options to choose from.




    • Edited by F. Van Roie Saturday, March 3, 2018 9:30 AM Code cleanup
    • Marked as answer by FRacine Saturday, March 3, 2018 10:42 AM
    • Unmarked as answer by FRacine Saturday, March 3, 2018 10:45 AM
    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    Saturday, March 3, 2018 9:25 AM
  • Hi,

    Its great. But output1 and output2 are giving the same results probably because the path was different on my second computer.

    What is 

    "{0}\{1}" -f 
    doing exactly?


    • Edited by FRacine Saturday, March 3, 2018 10:46 AM
    Saturday, March 3, 2018 10:42 AM
  • Hi,

    -f is just a formatting parameter for making the code more readable and maintainable. It will replace those numbers in curly braces (like {0 and {1} within that string) with the values of the parameters following it. Given:

    "{0}\{1}" -f (split-path $_.DirectoryName -Leaf), $_.name
    • {0} gets replaced by the result from (split-path $_.DirectoryName -Leaf)
    • {1} gets replaces by $_.name

    The result is a concatenation of the two values and a backslash in the middle of it. I updated the code to use Join-Path instead as it actually does the same thing and makes more sense:

    Join-Path (split-path $_.DirectoryName -Leaf) $_.name;
    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    Saturday, March 3, 2018 2:45 PM
  • Another way...


    $path = '\\xxx\Temp\Logiciels\Consolexx\Sources\Updates'
    Get-ChildItem -file -path $path -include *.msp -recurse |
    ForEach-Object {
    $parent = $_.Directory.Parent
    "$parent\$_"
    }





    • Marked as answer by FRacine Saturday, March 3, 2018 6:44 PM
    • Edited by JS2010 Saturday, March 3, 2018 8:45 PM
    Saturday, March 3, 2018 3:34 PM