4

If I execute:

Get-ChildItem *.ext -recurse 

the output consists of a series of Directory sections followed by one or more columns of info for each matching file separated by said directory sections. Is there something like the Unix find command? In which each matching file name appears on a single line with its full relative path?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Tim Hanson
  • 59
  • 1
  • 5

5 Answers5

5

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
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    Nice overview; the question is somewhat ambiguous ("full relative path"), so to clarify: these solutions give you the _full_ paths, not _relative_ ones. – mklement0 Feb 03 '20 at 17:04
  • Sorry about the ambiguity. I prefer the relative path (though full would be fine) and used "full relative" to make it clear I want the whole path on the line, not split up (directories on lines by themselves, filenames only on file entry lines) as per Powershell default. – Tim Hanson Feb 10 '20 at 08:40
4

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.

AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • Even though `Get-ChildItem -Name` should suffice here, this is a handy technique to know about in general. Note that you currently cannot specify a _reference_ dir. with `Resolve-Path -Relative`: the paths are invariably resolved relative to the _current_ dir. See [this GitHub feature request](https://github.com/PowerShell/PowerShell/issues/8435) – mklement0 Feb 03 '20 at 16:59
2

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:

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks! I tried that before posting (and now reading your suggestion) but I get no output whatsoever. I wonder what could be going wrong. – Tim Hanson Feb 03 '20 at 16:37
  • @TimHanson: That is mysterious. The `-Name` switch has been supported since at least v2, and it does exactly what you're looking for. Getting no output suggests that no files matched. Are you targeting the right directory? Does using `-Path *.ext` (same as just `*.ext`, as in your question) instead of `-Filter *.ext` make a difference (it shouldn't)? – mklement0 Feb 03 '20 at 16:49
0

I am having some problem passing the path plus filename to a parser. There are about 90 files of 1 GB each involved in my task. Each of the file is contained in a folder of its own. All of the folders are contained under a parent folder.

Goal: Ideally, I would like to parse 20 files simultaneously for multitasking and continue to the next 20 until all 90 files are done.

This would mean that I would like to spawn some concurrent parsing of 20 files in a batch at any one given time. In carrying out the parsing, I would like to use measure-command to time the work from beginning to finish.

Script I have used:

Get-ChildItem –Path "E:\\OoonaFTP\\input\\Videos3\\" -Filter *.mp4 -recurse | select -expand fullname

Foreach-Object {
Measure-Command { "E:\OoonaFTP\Ooona_x64_ver_2.5.13\OoonaParser.exe -encode -dat -drm $_.FullName" } | Select-Object -Property TotalSeconds
}

===============================

I have this working batch script with a for statement but doing each iteration one after another. This is not what is the ideal case though. I would really like to accomplish this in PowerShell and with simultaneous tasks.

Could someone please suggest some ways by which I could accomplish this?

Thank you very much!

Kris Stern
  • 1,192
  • 1
  • 15
  • 23
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/29992596) – Flair Oct 05 '21 at 04:39
-1

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.)

Tim Hanson
  • 59
  • 1
  • 5
  • If you just want the path strings, use `-ExpandProperty` instead of `-Property`, as shown in js2010's answer. However, as stated, the `-Name` switch should do directly what you want - though note the list of issues I've added to my answer. – mklement0 Feb 03 '20 at 16:50
  • Also, please note that this solution outputs _full_ (absolute) paths, whereas your question at least in part makes it sound like you're looking for _relative_ ones "full relative path". – mklement0 Feb 03 '20 at 17:09
  • If this is the answer mark as such. If not, edit your question to further clarify the question. – ΩmegaMan Oct 19 '21 at 17:03