use get-childitem -recurse in powershell but get each full path on a separate line (without extra directory lines)

流过昼夜 提交于 2020-08-23 09:22:29

问题


If I execute, e.g.

Get-ChildItem *.ext -recurse 

the output consists of a series of Directory: entries followed by one entry for each each matching file. That's useful for many purposes, but today I'd like a result (analogous to the Unix find command) in which each matching file appears on a line by itself and shows its full relative path (and no other lines appear).

I've searched a bit but haven't found a solution.


回答1:


Get-Childitem by default outputs a view for format-table defined in a format xml file somewhere.

get-childitem | format-table
get-childitem | format-list *

shows you the actual properties in the objects being output. See also How to list all properties of a PowerShell object . Then you can pick and choose the ones you want. This would give the full pathname:

get-childitem | select fullname

If you want an output to be just a string and not an object:

get-childitem | select -expand fullname
get-childitem | foreach fullname



回答2:


Resolve-Path with the -Relative switch can be used to display the relative paths of a set of paths. You can collect the full path names (FullName property) from the Get-ChildItem command and use the member access operator . to grab the path values only.

Resolve-Path -Path (Get-ChildItem -Filter *.ext -Recurse).FullName -Relative

Note: The relative paths here only accurately reflect files found within the current directory (Get-ChildItem -Path .), i.e. Get-ChildItem -Path NotCurrentDirectory could have undesirable results.




回答3:


Get-ChildItem's -Name switch does what you want:

  • It outputs the relative paths (possibly including subdir. components) of matching files as strings (type [string]).
# Lists file / dir. paths as *relative paths* (strings).
# (relative to the input dir, which is implicitly the current one here).
Get-ChildItem -Filter *.ext -Recurse -Name

Note that I've used -Filter, which significantly speeds up the traversal.

Caveat: As of PowerShell 7.0, -Name suffers from performance problems and behavioral quirks; see these GitHub issues:

  • https://github.com/PowerShell/PowerShell/issues/9014
  • https://github.com/PowerShell/PowerShell/issues/9119
  • https://github.com/PowerShell/PowerShell/issues/9126
  • https://github.com/PowerShell/PowerShell/issues/9122
  • https://github.com/PowerShell/PowerShell/issues/9120



回答4:


Thanks for the various suggestions. I'm curious that some of them lead to empty output in my Powershell (PSVersion: 5.1.18362.145).

I tried a number of these and, inspired by some of them, found the best answer for my case at the moment:

Get-ChildItem *.ext -recurse | Select-Object -property fullname

(When I made the window wide enough I got all the info I needed; in general I suppose I might need to do more to get the formatting I want.)



来源:https://stackoverflow.com/questions/60039046/use-get-childitem-recurse-in-powershell-but-get-each-full-path-on-a-separate-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!