我需要获取属于特定类型的所有文件,包括子文件夹中存在的文件。
我正在使用 Get-ChildItem 进行如下操作:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
然而,它只返回给我文件名而没有整个路径。
我需要获取属于特定类型的所有文件,包括子文件夹中存在的文件。
我正在使用 Get-ChildItem 进行如下操作:
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
然而,它只返回给我文件名而没有整个路径。
gci "C:\WINDOWS\System32" -r -include .txt | select fullname
为什么还没有人使用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 = $fileList[$i].FullName
。假设 $i
是一个数字值。 - NostraDavidGet-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...
[替代语法]
对于一些人来说,方向管道操作符并不是他们的口味,他们更喜欢链式调用。在roslyn问题跟踪器中分享了一些有趣的意见,请参见dotnet/roslyn#5445。
基于情况和上下文,这些方法之一可以被认为是隐含的(或间接的)。例如,在这种情况下,使用管道操作符处理可枚举类型需要特殊的标记$_
(也称为PowerShell的"THIS"标记
),可能对某些人来说令人反感。
对于这样的人,这里有一种更简洁、直观的方法,即点链接:
(gci . -re -fi *.txt).FullName
(<rant>注意PowerShell的命令参数解析器接受部分参数名称。因此,除了-recursive
之外,-recursiv
、-recursi
、-recurs
、-recur
、-recu
、-rec
和-re
也被接受,但不幸的是-r
不被接受..这是唯一正确的选择,如果我们按照POSIXy UNIXy的惯例,这是唯一有意义的单个-
字符!</rant>)
-File
开关-fi
也是如此:-fil
和-file
有效,但-f
无效。按照POSIX风格,应该是--file
(多字母)和-f
(单字母,除非-f保留给其他东西,比如force
开关,那么它可以是其他东西,比如-l
或根本没有单字母选项)。 - vulcan raven$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"
}