admin管理员组

文章数量:1302967

I need to list recursively all files with paths, but without the lines for folders, something like this:

dir-name1\file-name1.ext
dir-name1\file-name2.ext
dir-name2\file-name3.ext
dir-name2\file-name4.ext

I use Powershell 2.0 that doesn't recognize some modern syntax. I've tried many suggested solutions, but none works. If it excludes folders, it also removes the file path; if it keeps the path, it doesn't exclude folders.

The only workaround I have so far with the obvious limitations is:

Get-ChildItem -Recurse -Name -Include *.*

Is there a better way without the complexity of writing a script?

I need to list recursively all files with paths, but without the lines for folders, something like this:

dir-name1\file-name1.ext
dir-name1\file-name2.ext
dir-name2\file-name3.ext
dir-name2\file-name4.ext

I use Powershell 2.0 that doesn't recognize some modern syntax. I've tried many suggested solutions, but none works. If it excludes folders, it also removes the file path; if it keeps the path, it doesn't exclude folders.

The only workaround I have so far with the obvious limitations is:

Get-ChildItem -Recurse -Name -Include *.*

Is there a better way without the complexity of writing a script?

Share Improve this question asked Feb 11 at 7:50 safespheresafesphere 1464 bronze badges 5
  • 4 PowerShell 2.0, REALLY ? Try Get-ChildItem -Path D:\Test -Recurse | Where-Object { !$_.PsIsContainer } | Select-Object -ExpandProperty FullName – Theo Commented Feb 11 at 10:23
  • This actually works, thank you! If you turn your comment into a short answer, I would accept it. Powershell is 2.0, because it is what is installed and I've never used it before. Now I need to pipe from one command to the other, hence Powershell :) – safesphere Commented Feb 11 at 17:06
  • 1 Sure, I'll do that, but it will have to wait until tomorrow as I'm on mobile now. – Theo Commented Feb 11 at 19:24
  • 1 Time to upgrade your powershell. – js2010 Commented Feb 12 at 15:14
  • @js2010 not only powershell but also the OS that's used to run it – phuclv Commented Feb 17 at 10:09
Add a comment  | 

1 Answer 1

Reset to default 1

As promised, here my comment as short answer. In PowerShell version 2.0, the Get-ChildItem cmdlet doesn't have switches for -File or -Directory, so in order to get a listing of just files and no directories, you need to filter the DirectoryInfo objects out using a Where-Object clause:

Get-ChildItem -Path D:\Test -Recurse |      # enumerate ALL objects in the path
    Where-Object { !$_.PsIsContainer } |    # filter to receive only FileInfo objects
    Select-Object -ExpandProperty FullName  # return only the full path and file names

If you upgrade your now ancient version of PowerShell to at least version 5.1, you can get the same result (just faster) like this:

(Get-ChildItem -Path D:\Test -Recurse -File).FullName

本文标签: