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