【发布时间】:2012-10-19 00:51:40
【问题描述】:
我需要获取所有文件,包括属于特定类型的子文件夹中的文件。
我正在做这样的事情,使用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 }
【讨论】:
*.txt* 这样的文件(-Filter 使用 CMD 通配符)。如果这不是您想要的,请使用-Include *.txt。
ls | % { $_.FullName } 获取文件列表中的完整路径;假设别名 ls=Get-ChildItem
这是一个较短的:
(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
【讨论】:
(gci -r c:\).fullname
(ls -r c:\).fullname
(ls -r c:).fullname
你也可以像这样使用Select-Object:
Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
【讨论】:
Select-Object 返回PSCustomObject,而不是字符串。如果您将结果用作另一个程序的参数,它可能不起作用
PSCustomObject,请使用 -ExpandProperty FullName 而不是简单的 FullName。据我了解,-ExpandProperty 参数导致 cmdlet 将结果作为指定属性的(本机?)类型而不是某个自定义对象返回。
Get-ChildItem -Path 'C:\Program Files\TheProgram' -Recurse | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-03-01')} | Select-Object FullName
试试这个:
Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
【讨论】:
gci "C:\WINDOWS\System32" -r -include .txt | select fullname
【讨论】:
[替代语法]
对于某些人来说,定向管道操作员不是他们的口味,但他们更喜欢链接。在 roslyn 问题跟踪器中查看有关此主题的一些有趣意见:dotnet/roslyn#5445。
根据案例和上下文,这种方法之一可以被认为是隐含的(或间接的)。例如,在这种情况下,针对可枚举使用管道需要特殊令牌 $_(又名 PowerShell's "THIS" token)可能会让某些人感到反感。
对于这样的人,这里有一个更简洁、直接的方法,使用 点链接:
(gci . -re -fi *.txt).FullName
(-recursive;-recursiv,-recursi,-recurs,-recur,-recu, -rec 和 -re 被接受,但不幸的是 -r .. 这是唯一对单个 - 字符有意义的正确选择(如果我们遵循 POSIXy UNIXy 约定)!
【讨论】:
-r 的缩写形式 -Recurse 对我来说很好。
-File 开关-fi 也是如此:-fil 和-file 有效,但-f 无效。按照 POSIX 风格,它应该是 --file(多字母)和 -f(单字母,除非 -f 是为其他东西保留的,比如 force 开关,那么它可以是像 -l 这样的其他东西或根本没有单字母选项)。
这对我有用,并生成了一个名称列表:
$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
【讨论】:
Get-ChildItem -Recurse *.txt | Format-Table FullName
这就是我用的。我觉得它更容易理解,因为它不包含任何循环语法。
【讨论】:
如果相对路径是您想要的,您可以使用 -Name 标志。
Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name
【讨论】:
subfolder/file.txt
在 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) 中得到解决:Stringification behavior of FileInfo / Directory instances has changed since v6.0.2 #7132
【讨论】:
我正在使用以下脚本来提取所有文件夹路径:
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...
【讨论】:
我使用此行命令在“C:\Temp”中搜索“.xlm”文件,结果在文件“result.txt”中打印全名路径:
(Get-ChildItem "C:\Temp" -Recurse | where {$_.extension -eq ".xml"} ).fullname > result.txt
在我的测试中,这种语法对我来说很好用。
【讨论】:
为什么还没有人使用 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
}
【讨论】:
textfile 前添加$:$textfile = $fileList[$i].FullName。假设$i有一个数值,即。
我遇到了一个问题,我有一个可执行文件,其中包含目录路径字符串作为参数和格式。像这样:
"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"
}
【讨论】: