186

我需要获取所有文件,包括属于特定类型的子文件夹中的文件。

我正在做这样的事情,使用Get-ChildItem

Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}

但是,它只返回文件名而不是整个路径。

4

15 回答 15

273

添加| select FullName到上面行的末尾。如果您之后需要实际执行此操作,则可能必须将其通过管道传输到 foreach 循环中,如下所示:

get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
     Write-Host $_.FullName
}
于 2012-10-29T17:01:42.217 回答
130

这应该比使用后期过滤执行得快得多:

Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
于 2012-10-29T20:11:41.000 回答
42

您也可以像这样使用Select-Object

Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
于 2014-06-28T16:19:50.360 回答
27

这是一个较短的:

(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
于 2014-02-10T16:31:47.160 回答
14

如果相对路径是您想要的,您可以使用该-Name标志。

Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name

于 2016-04-06T08:30:47.113 回答
7

PS 5 中真正令人讨厌的事情,其中​​ $_ 不会是 foreach 中的完整路径。这些是 FileInfo 和 DirectoryInfo 对象的字符串版本。出于某种原因,路径中的通配符可以修复它,或者使用 Powershell 6 或 7。您也可以通过管道连接到中间的 get-item。

Get-ChildItem -path C:\WINDOWS\System32\*.txt -Recurse | foreach { "$_" }

Get-ChildItem -path C:\WINDOWS\System32 -Recurse | get-item | foreach { "$_" }

这似乎是 .Net 的一个问题,在 .Net Core (Powershell 7) 中得到了解决: FileInfo / Directory instances 的字符串化行为自 v6.0.2 #7132 起发生了变化

于 2018-11-28T22:28:24.743 回答
7

尝试这个:

Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
于 2015-08-10T11:35:24.297 回答
7
Get-ChildItem -Recurse *.txt | Format-Table FullName

那是我用的。我觉得它更容易理解,因为它不包含任何循环语法。

于 2016-03-23T19:05:49.010 回答
5

这对我有用,并产生了一个名称列表:

$Thisfile=(get-childitem -path 10* -include '*.JPG' -recurse).fullname

我通过 using 找到了它get-member -membertype properties,这是一个非常有用的命令。它为您提供的大多数选项都附加了一个.<thing>,就像fullname这里一样。你可以坚持同样的命令;

  | get-member -membertype properties 

在任何命令的末尾,以获取有关您可以使用它们执行的操作以及如何访问这些操作的更多信息:

get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties
于 2016-01-28T10:27:36.880 回答
2

我使用此行命令在“C:\Temp”中搜索“.xlm”文件,结果在文件“result.txt”中打印全名路径:

(Get-ChildItem "C:\Temp" -Recurse | where {$_.extension -eq ".xml"} ).fullname > result.txt 

在我的测试中,这种语法对我来说很漂亮。

于 2019-12-06T12:12:32.453 回答
2
gci "C:\WINDOWS\System32" -r -include .txt | select fullname
于 2015-09-11T08:30:42.010 回答
2

为什么还没有人使用 foreach 循环?这里的优点是您可以轻松地命名变量:

# Note that I'm pretty explicit here. This would work as well as the line after:
# Get-ChildItem -Recurse C:\windows\System32\*.txt
$fileList = Get-ChildItem -Recurse -Path C:\windows\System32 -Include *.txt
foreach ($textfile in $fileList) {
    # This includes the filename ;)
    $filePath = $textfile.fullname
    # You can replace the next line with whatever you want to.
    Write-Output $filePath
}
于 2019-12-08T17:20:06.900 回答
1

[替代语法]

对于某些人来说,定向管道操作员不是他们的口味,但他们更喜欢链接。请参阅 roslyn 问题跟踪器中共享的有关此主题的一些有趣意见:dotnet/roslyn#5445

根据案例和上下文,这种方法之一可以被认为是隐含的(或间接的)。例如,在这种情况下,对 enumerable 使用管道需要特殊令牌$_(aka PowerShell's "THIS" token)可能会让某些人感到反感。

对于这样的人,这里有一种更简洁、直接的方式,使用点链接

(gci . -re -fi *.txt).FullName

(<rant> 请注意,PowerShell 的命令参数解析器接受部分参数名称。因此,除了-recursive; -recursiv, -recursi, , , , 和 之外 ; , , -recurs, -recur, -recu,-rec-re被接受,但不幸的是不是.. 这是唯一对单个字符-r有意义的正确选择(如果我们-遵循 POSIXy UNIXy 约定)!</rant>)

于 2015-11-22T10:56:06.777 回答
1

我正在使用以下脚本来提取所有文件夹路径:

Get-ChildItem -path "C:\" -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | Out-File "Folder_List.csv"

没有完整的文件夹路径。113个字符后,来了:

Example - C:\ProgramData\Microsoft\Windows\DeviceMetadataCache\dmrccache\en-US\ec4d5fdd-aa12-400f-83e2-7b0ea6023eb7\Windows...
于 2018-11-29T08:00:49.213 回答
0

我有一个问题,我有一个可执行文件,其中包含目录路径字符串作为参数和格式。像这样:

"C:\temp\executable.exe" "C:\storage\filename" "C:\storage\dump" -jpg

我需要在不同文件夹中的 .jpg 文件中执行此命令。

$files = Get-ChildItem  -Path C:\storage\*.jpg -Recurse -Force | Select-Object -ExpandProperty FullName

for ($i=0; $i -lt $files.Count; $i++) {
    [string]$outfile = $files[$i]
    
    Start-Process -NoNewWindow -FilePath "C:\temp\executable.exe" -ArgumentList $outfile, "C:\storage\dump", "-dcm"
   

}
于 2021-07-07T12:07:53.267 回答