[英]Get full path of the files in PowerShell
I need to get all the files including the files present in the subfolders that belong to a particular type.我需要获取所有文件,包括属于特定类型的子文件夹中存在的文件。
I am doing something like this, using Get-ChildItem :我正在做这样的事情,使用Get-ChildItem :
Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}
However, it's only returning me the files names and not the entire path.但是,它只返回文件名而不是整个路径。
Add | select FullName
添加| select FullName
| select FullName
to the end of your line above. | select FullName
到上面一行的末尾。 If you need to actually do something with that afterwards, you might have to pipe it into a foreach loop, like so:如果你需要在之后实际做一些事情,你可能必须将它传递到一个 foreach 循环中,如下所示:
get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
Write-Host $_.FullName
}
This should perform much faster than using late filtering:这应该比使用后期过滤执行得更快:
Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
You can also use Select-Object like so:您也可以像这样使用Select-Object :
Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
Here's a shorter one:这是一个较短的:
(Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
If relative paths are what you want you can just use the -Name
flag.如果相对路径是你想要的,你可以只使用-Name
标志。
Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name
Try this:试试这个:
Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
Get-ChildItem -Recurse *.txt | Format-Table FullName
That is what I used.那就是我用的。 I feel it is more understandable as it doesn't contain any loop syntax.我觉得它更容易理解,因为它不包含任何循环语法。
Really annoying thing in PS 5, where $_ won't be the full path within foreach.在 PS 5 中真的很烦人,其中 $_ 不是 foreach 中的完整路径。 These are the string versions of FileInfo and DirectoryInfo objects.这些是 FileInfo 和 DirectoryInfo 对象的字符串版本。 For some reason a wildcard in the path fixes it, or use Powershell 6 or 7. You can also pipe to get-item in the middle.出于某种原因,路径中的通配符修复了它,或者使用 Powershell 6 或 7。您还可以通过管道传递到中间的 get-item。
Get-ChildItem -path C:\WINDOWS\System32\*.txt -Recurse | foreach { "$_" }
Get-ChildItem -path C:\WINDOWS\System32 -Recurse | get-item | foreach { "$_" }
This seems to have been an issue with.Net that got resolved in.Net Core (Powershell 7): Stringification behavior of FileInfo / Directory instances has changed since v6.0.2 #7132这似乎是 .Net 的一个问题,在 .Net Core (Powershell 7) 中得到了解决: FileInfo / Directory 实例的字符串化行为自 v6.0.2 以来发生了变化 #7132
This worked for me, and produces a list of names:这对我有用,并生成一个名称列表:
$Thisfile=(get-childitem -path 10* -include '*.JPG' -recurse).fullname
I found it by using get-member -membertype properties
, an incredibly useful command.我通过使用get-member -membertype properties
找到了它,这是一个非常有用的命令。 most of the options it gives you are appended with a .<thing>
, like fullname
is here.它为您提供的大多数选项都附加了一个.<thing>
,就像这里的fullname
名一样。 You can stick the same command;您可以坚持相同的命令;
| get-member -membertype properties
at the end of any command to get more information on the things you can do with them and how to access those:在任何命令的末尾获取有关您可以使用它们执行的操作以及如何访问这些操作的更多信息:
get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties
I used this line command to search ".xlm" files in "C:\Temp" and the result print fullname path in file "result.txt":我使用此行命令在“C:\Temp”中搜索“.xlm”文件,结果在文件“result.txt”中打印全名路径:
(Get-ChildItem "C:\Temp" -Recurse | where {$_.extension -eq ".xml"} ).fullname > result.txt
In my tests, this syntax works beautiful for me.在我的测试中,这种语法对我来说效果很好。
Why has nobody used the foreach loop yet?为什么还没有人使用 foreach 循环? A pro here is that you can easily name your variable:这里的优点是您可以轻松命名变量:
# 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
[alternative syntax] [替代语法]
For some people, directional pipe operators are not their taste, but they rather prefer chaining.对于某些人来说,定向管道运算符不是他们的口味,但他们更喜欢链接。 See some interesting opinions on this topic shared in roslyn issue tracker: dotnet/roslyn#5445 .请参阅 roslyn 问题跟踪器中分享的关于此主题的一些有趣观点: dotnet/roslyn#5445 。
Based on the case and the context, one of this approach can be considered implicit (or indirect).根据案例和上下文,可以将其中一种方法视为隐式(或间接)。 For example, in this case using pipe against enumerable requires special token $_
(aka PowerShell's "THIS" token
) might appear distasteful to some.例如,在这种情况下,对可枚举对象使用管道需要特殊令牌$_
(又名PowerShell's "THIS" token
)可能对某些人来说令人反感。
For such fellas, here is a more concise, straight-forward way of doing it with dot chaining :对于这样的家伙,这里有一个更简洁、直接的点链接方式:
(gci . -re -fi *.txt).FullName
(<rant> Note that PowerShell's command arguments parser accepts the partial parameter names. So in addition to -recursive
; -recursiv
, -recursi
, -recurs
, -recur
, -recu
, -rec
and -re
are accepted, but unfortunately not -r
.. which is the only correct choice that makes sense with single -
character (if we go by POSIXy UNIXy conventions)! </rant>) (<rant> 请注意,PowerShell 的命令参数解析器接受部分参数名称。所以除了-recursive
; -recursiv
、 -recursi
、 -recurs
、 -recur
、 -recu
、 -rec
和-re
被接受,但不幸的是-r
.. 这是对单字符有意义的唯一正确选择-
如果我们按照 POSIXy UNIXy 约定)!</rant>)
I am using below script to extact all folder path:我正在使用下面的脚本来提取所有文件夹路径:
Get-ChildItem -path "C:\" -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | Out-File "Folder_List.csv"
Full folder path is not coming.没有完整的文件夹路径。 After 113 characters, is coming: 113个字符后,是:
Example - C:\ProgramData\Microsoft\Windows\DeviceMetadataCache\dmrccache\en-US\ec4d5fdd-aa12-400f-83e2-7b0ea6023eb7\Windows...
I had an issue where I had an executable file which had directory path strings as parameters and the format.我遇到了一个问题,我有一个可执行文件,该文件将目录路径字符串作为参数和格式。 Like this:像这样:
"C:\temp\executable.exe" "C:\storage\filename" "C:\storage\dump" -jpg "C:\temp\executable.exe" "C:\storage\filename" "C:\storage\dump" -jpg
I needed to execute this command across terabytes of.jpg files in different folders.我需要在不同文件夹中的 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.