在PowerShell中获取文件的完整路径


145

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

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

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

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

Answers:


219

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

get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
     Write-Host $_.FullName
}

101

这比使用后期过滤要快得多:

Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }

14
这是真的。注意:该命令实际上获取的文件如下*.txt*-Filter使用CMD通配符)。如果这不是您想要的,请使用-Include *.txt
罗曼·库兹明

26

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

Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName

5
请注意,Select-Object返回值PSCustomObject不是字符串。如果您将结果用作另一个程序的参数,则可能不起作用
Chintsu

5
@Chintsu说的是的。要获取字符串而不是PSCustomObject,请使用-ExpandProperty FullName而不是FullName。据我了解,-ExpandProperty参数会导致cmdlet以指定属性的(native?)类型而不是某些自定义对象的形式返回结果。
doubleDown '16

我也需要时间(我把程序的源头弄乱了,后来想想我应该已经开始对它进行源代码控制了,但是我可以通过自安装以来获取文件来了解对“ master”的第一次提交:Get-ChildItem -Path 'C:\Program Files\TheProgram' -Recurse | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-03-01')} | Select-Object FullName
Neil Guy Lindberg

19

这是一个较短的:

(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt

23
下面是一个简短的例子:(gci -r c:\).fullname
克里斯·N

11
这是一个较短的:(ls -r c:\).fullname
Kalamane

15
下面是一个简短的(ls -r c:).fullname
例子

9

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

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


5
Get-ChildItem -Recurse *.txt | Format-Table FullName

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


5

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)中得到解决: 自v6.0.2#7132起,FileInfo / Directory实例的字符串化行为已更改


4

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

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

我通过使用来找到它,这是get-member -membertype properties一个非常有用的命令。它提供给您的大多数选项都附加了.<thing>,如此fullname处所示。您可以粘贴相同的命令;

  | get-member -membertype properties 

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

get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties


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
}

如何在不使用fileList中的foreach文本文件的情况下获取文本文件的单个文件路径?像textfile = $ fileList [$ i] .fullname之类的东西,我知道这不是最理想的方法,但是我需要这样做:(
Heber Solis

只需添加一个$在眼前textfile$textfile = $fileList[$i].FullName。假设$i有一个数字值。
NostraDavid

1
gci "C:\WINDOWS\System32" -r -include .txt | select fullname

6
您能否详细说明您的答案,并提供有关您提供的解决方案的更多说明?
abarisone 2015年

1

[替代语法]

对于某些人来说,定向管道操作员不是他们的口味,而是他们更喜欢链接。在roslyn问题跟踪器中查看关于此主题的一些有趣观点:dotnet / roslyn#5445

根据情况和上下文,可以将这种方法之一视为隐式(或间接)的。例如,在这种情况下,将管道用于不可枚举要求特殊令牌$_(aka PowerShell's "THIS" token)可能会使某些人感到反感。

对于这样的小伙子们,这是一种通过点链实现的更简洁,直接的方法:

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

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


1
请注意:-r简短的形式-Recurse对我来说很好。
Polymorphix

@Polymorphix,您是对的,它在Windows 10上对我有效(不确定我之前尝试的版本)。但是,样本中的-Fileswitch 同样适用-fi-fil并且-file有效,但无效-f。按照POSIX风格,它应该是--file(多个字母)和-f(单个字母,除非-f为其他内容保留,例如forceswitch,那么它可以是其他类似-l字符或根本没有单个字母选项)。
火神乌鸦

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

0

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

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

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.