3
votes

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.

5
If you want relative paths, you could just do Resolve-Path (Get-ChildItem *.ext -recurse).FullName -Relative.AdminOfThings

5 Answers

3
votes

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
votes

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.

2
votes

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:

0
votes

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

0
votes

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!