2

I'm not 100% certain how to formulate this powershell.

If my PWD is "C:\Work\Projects\Release\20140610" and that in itself is a directory tree comprised of several sub folders that themselves may contain files and sub folders ad nauseum, how can I format the output so that it prints like so:

20140610\HTML\File1.html
20140610\HTML\File2.html
20140610\HTML\File3.html
20140610\HTML\Forms\Form1.html
20140610\HTML\Forms\Form2.html
20140610\HTML\Forms\Form3.html
20140610\HTML\Views\View1.html
20140610\Scripts\File1.js

Edit:

Sorry, posted too early.

What I have so far is:

$TableFormat = @{Expression={ $_.Path};Label="Name"},@{Expression = {$_.HashString};Label="Signature"}

Get-ChildItem -Recurse | ?{ !$_.PSIsContainer } | Get-Hash -Algorithm MD5 | Format-Table -AutoSize $TableFomat | Tee-Object -FilePath "C:\Signatures.txt"

Now, I believe I need to add my path split and likely join too midstream after the folder check. However, as so, I do not get the full name so I'm not sure how to continue.

SteveMustafa
  • 621
  • 2
  • 8
  • 19

2 Answers2

4

You mean you want just the relative path including the current directory instead of the full path? Something like this should do that:

$cwd = (Get-Item .).Parent.FullName
$re  = '^' + [regex]::Escape("$cwd\")
Get-ChildItem -Recurse |
  Where-Object { -not $_.PSIsContainer } |
  ForEach-Object { $_.FullName -replace $re }
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
3

It's not clear how the format you're using in the code you posted relates to the question, since it seems to contradict the format of the list you have above, unless you mean you want that to be one column in the output.

In any case, if what you want is a list of all files in the subtree of the directory C:\Work\Projects\Release\20140610, with the path starting from the root of that subtree with that root's parent path excluded, you can do it by using -replace to remove the parent path from each file's FullName property (the file's full path):

$Path = 'C:\Work\Projects\Release\20140610'
Get-ChildItem $Path -Recurse | ?{! $_.PSIsContainer} `
| %{$_.FullName -replace [regex]::Escape("$(Split-Path $Path)\"), ''}

To use it as the value of the Name column, just use the scriptblock from ForEach-Object (%) as the expression:

@{l='Name'; e={$_.FullName -replace [regex]::Escape("$(Split-Path $Path)\"), ''}}
  • "$(Split-Path $Path)\" gives you the parent of $Path plus a trailing backslash, which is the part of the path you want to exclude (by replacing it with an empty string). You might want to assign this to a variable for efficiency (to avoid having to recalculate it for each iteration of the pipeline), but there doesn't seem to be much overhead.
  • [regex]::Escape escapes any regex special characters, which is particularly important for Windows paths, which contain backslashes.
  • In PowerShell 3.0 or later, you can use -File instead of | ?{! $_.PSIsContainer}
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • Thank you. Marked as solved; but, if we were to alter that a bit so that it does two things. 1) the above [done] which provides me with a list of file names in a relative path to the project folder. 2) generate an MD5 hash (I'm using PSCX 3.1 production, so the Get-Hash cmdlet) **for the actual file** – SteveMustafa Jun 08 '14 at 21:51
  • I'm not familiar with **Get-Hash**, which is a community extension, not native PowerShell, but maybe this helps? http://stackoverflow.com/questions/10521061/how-to-get-a-md5-checksum-in-powershell (The accepted answer uses a .NET class, but the second answer uses **Get-Hash**.) – Adi Inbar Jun 08 '14 at 22:51