我需要获取属于特定类型的所有文件,包括子文件夹中存在的文件。
我正在使用 Get-ChildItem 进行如下操作:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
然而,它只返回给我文件名而没有整个路径。
我需要获取属于特定类型的所有文件,包括子文件夹中存在的文件。
我正在使用 Get-ChildItem 进行如下操作:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
然而,它只返回给我文件名而没有整个路径。
在你上面的行末尾添加| select FullName
。如果之后需要对其进行实际操作,可能需要将其导入foreach循环,例如:
get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
Write-Host $_.FullName
}
使用这种方法应该比晚期过滤更快:
Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
您也可以像这样使用 Select-Object:
Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
PSCustomObject
,请使用-ExpandProperty FullName
而不仅仅是FullName
。据我了解,-ExpandProperty
参数会导致cmdlet将结果作为指定属性的(本机?)类型返回,而不是作为某些自定义对象返回。 - doubleDownGet-ChildItem -Path 'C:\Program Files\TheProgram' -Recurse | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-03-01')} | Select-Object FullName
) - Neil Gaetano Lindberg这是一个更短的版本:
(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
(gci -r c:\).fullname
。 翻译意思为:获取"C:"盘符下所有文件和文件夹的完整路径。 - Chris N(ls -r c:\).fullname
。该命令会按逆序列出C盘中所有文件和目录,并显示它们的完整路径。 - Kalamane(ls -r c:).fullname
。 - Valiante如果您需要相对路径,只需使用-Name
标志。
Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name
subfolder/file.txt
。 - Polymorphix(Get-ChildItem "C:\Temp" -Recurse | where {$_.extension -eq ".xml"} ).fullname > result.txt
Get-ChildItem -Recurse *.txt | Format-Table FullName
我所使用的方法是如此。我认为这种方法更易于理解,因为它不包含任何循环语法。
试试这个:
Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
在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以来,FileInfo / Directory实例的字符串化行为发生了变化
$Thisfile=(get-childitem -path 10* -include '*.JPG' -recurse).fullname
我通过使用get-member -membertype properties
命令找到了它,这是一个非常有用的命令。它提供的大多数选项都附带有一个.<thing>
,比如这里的fullname
。你可以使用相同的命令;
| get-member -membertype properties
在任何命令的结尾加上“--help
”可以获取更多关于该命令的使用说明和访问方法:
get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties
*.txt*
这样的文件(-Filter
使用CMD通配符)。如果这不是你想要的,那么请使用-Include *.txt
。翻译为:这是正确的。一个注意事项:这个命令实际上会获取类似于*.txt*
的文件(-Filter
使用CMD通配符)。如果这不是您想要的,请使用-Include *.txt
。 - Roman Kuzminls | % { $_.FullName }
命令可以在文件列表中获取完整路径。 - Mathieu CAROFF