Reputation: 183
I'm confused as to how to use the Split-Path properly for Powershell version 5 when I have a very long directory structure as I am using a Dropbox drive on my PC within Win 10.
My problem domain is to recursively travel several directories of software changes I had made, there are several versions and sometimes each directory have two or three sub directories each deep at several places.
The directory structure I have and it begins at C:\Users\XXXX\Dropbox\Doing\SoftwareName\Releases
The "Release" field is to show the release number I wanted to be able to summarise my report as to the version number which is under the ..\Release directory. Location is the last (sub)-directory hence using -Leaf and the filename as File.
The code I used (EDIT: Shown with changes I learnt. I'm happy with Curly braces in the output due to referencing a collection.
cd C:\Users\XXXX\Dropbox\Doing\SoftwareName\Releases
Get-ChildItem -Path (Get-Location) -Filter *.cs -Recurse |
ForEach-Object {
$Release = $_.Directory.Split('\') -like "*Version*"
$Location = Split-Path ($_.DirectoryName) -Leaf
$File = $_.Name
ForEach-Object {
New-Object PSObject -Property @{
Release = $Release.ToString()
Location= $Location.ToString()
File = $File.ToString()
}
} | Format-Table -AutoSize Release, Location, File
}
Output (the first three lines to simplify)
Release
Location File
C:\Users\XXXX\Dropbox\Doing\SoftwareName\Releases\Version1\StoryWriter StoryWriter Chapter1.cs
C:\Users\XXXX\Dropbox\Doing\SoftwareName\Releases\Version2\StoryWriter\Outline\BeachScene BeachScene Chapter2.cs
C:\Users\XXXX\Dropbox\Doing\SoftwareName\Releases\Version3\StoryWriter\Outline\Conversations Conversations Chapter3.cs
Outcome I'm after is
Release Location File
------- -------- ----
Version1 StoryWriter Chapter1.cs
Version2 BeachScene Chapter2.cs
Version3 Conversations Chapter3.cs
Many thanks.
Upvotes: 0
Views: 821
Reputation: 17472
try this :
Get-ChildItem -Path "c:\temp" -Filter *.cs -Recurse -file | where FullName -like '*\Release\version*\*\*' | %{
$Array=$_.FullName -split '\\'
$RankRealease=$Array.IndexOf("Release")
[pscustomobject]@{Release = $Array[$RankRealease+1];Location= $Array[$RankRealease+2];File = $_.Name}
}
Upvotes: 0
Reputation: 2904
you can acheive this using calculated properties of select-object but to continue what you are doing:
i got rid of the 2nd foreach-object
that you had inside the first because it was serving no purpose. also if you are going to use foreach-object
you need to pipe a collection as input.
Since you want to get the version folder name showing up under the release
column i used regular expressions to parse the path. maybe there are better way to do that but anyways serves the purpose.This will work as long as the version folder is preceeded by the releases
folder.
Get-ChildItem -Path (Get-Location) -Filter *.cs -Recurse |
ForEach-Object {
New-Object PSObject -Property @{
Release = ([regex]::Matches($_.DirectoryName,'(?<=Releases\\)\w+\\' ) | select -ExpandProperty value) -replace '\\'
Location= Split-Path ($_.DirectoryName) -Leaf
File = $_.Name
}
} | Format-Table -AutoSize Release, Location, File
Upvotes: 1