3

I'm writing a simple script to list all the files recursively within a folder "fullscreen" containing the word 'plugin'. Because the path is too long, and unnecessary, I decided to get the jut the FileName. The problem is that all the files are called "index.xml" so it would be very helpful get: "containing folder + file name". So the output would look something like this:

on\index.xml
off\index.xml

Instead of:

C:\this\is\a\very\long\path\fullscreen\on\index.xml
C:\this\is\a\very\long\path\fullscreen\off\index.xml

This is what I have:

dir .\fullscreen | sls plugin | foreach { write-host $($_).path }

I am getting this error:

Cannot bind argument to parameter 'Path' because it is null.

2 Answers 2

8

You're close: :-)

dir .\fullscreen | sls plugin | foreach { write-host $_.path }

This would have also worked:

dir .\fullscreen | sls plugin | foreach { write-host "$($_.path)" }

BTW I would generally avoid Write-Host unless you're really just displaying info only for someone to see who is sitting at the console. If you later wanted to capture this output to a variable, it wouldn't work as-is:

$files = dir .\fullscreen | sls plugin | foreach { write-host $_.path } # doesn't work

Most of the time you can achieve the same output and enable capture to a variable by just using the standard output stream e.g.:

dir .\fullscreen | sls plugin | foreach { $_.path }

And if you are on PowerShell v3 you can simplify to:

dir .\fullscreen | sls plugin | % Path

Update: to get just the containing folder name, do this:

dir .\fullscreen | sls plugin | % {"$(split-path (split-path $_ -parent) -leaf)\$($_.Filename)"}
1
  • Thank you for your help but I think you haven't understood what I wanted to achieve. I have edited my question giving more details about what's the kind of output I'm looking for. Cheers
    – RafaelGP
    Jul 15, 2013 at 16:02
1

The Directory property of the FileInfo class tells you the parent directory, you just have to grab the base of that and join with the name of your file. Note the extra foreach that turns the item back into a FileInfo object:

dir .\fullscreen | sls plugin | foreach{ get-item $_.Path } | foreach { write-output (join-path $_.Directory.BaseName $_.Name)}

If you want to avoid the extra pipeline:

dir .\fullscreen | sls plugin | foreach{ $file = get-item $_.Path; write-output (join-path $file.Directory.BaseName $file.Name)}
3
  • Select-String outputs a MatchInfo object, not a FileInfo object so there is no Directory property available on it.
    – Keith Hill
    Jul 15, 2013 at 19:28
  • @Keith thanks I forgot that. I edited my answer to offer a slightly alternative approach.
    – zdan
    Jul 15, 2013 at 20:32
  • Let's try that again ... In PowerShell V4 you will be able to use FileInfo via the -PipelineVariable parameter (alias pv) e.g. dir .\fullscreen -pv fi | sls plugin | foreach { write-output (join-path $fi.Directory.BaseName $_.Filename)}
    – Keith Hill
    Jul 16, 2013 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.