繁体   English   中英

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

[英]Get full path of the files in PowerShell

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

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

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

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

添加| select FullName | 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

这是一个较短的:

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

如果相对路径是你想要的,你可以只使用-Name标志。

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

试试这个:

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) 中得到了解决: FileInfo / Directory 实例的字符串化行为自 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

我使用此行命令在“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
}
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-rec-re被接受,但不幸的是-r .. 这是对单字符有意义的唯一正确选择-如果我们按照 POSIXy UNIXy 约定)!</rant>)

我正在使用下面的脚本来提取所有文件夹路径:

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

我需要在不同文件夹中的 TB 级 .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"
   

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM