当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell-获取文件的完整路径

作者:精品下载站 日期:2024-12-14 05:28:29 浏览:14 分类:玩电脑

PowerShell-获取文件的完整路径


[玩转系统] PowerShell-获取文件的完整路径

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

要在 powershell 中获取文件的完整路径:

  • 转到文件所在的文件夹。您也可以转到任何父文件夹。
  • Get-ChildItem-Recurse 选项结合使用。
  • 将其通过管道传输到 where-object cmdlet 以过滤文件名。在此示例中,我们将文件名指定为 test.ps1
  • 将其通过管道传递给 forEach-Object cmdlet 并获取当前对象的 FullName 属性。

使用 Get-ChildItem Cmdlet:

Get-ChildItem -Recurse |Where-Object {($_.name -eq "test.ps1")}| %{$_.FullName}

输出 :

D:\PowerShell\test.ps1

以下是在 PowerShell 中获取目录中文件的完整路径的多个示例。每个选项都将与 Get-ChildItem cmdlet 结合使用。

使用 Get-ChildItem Cmdlet:

要获取目录中文件的完整路径,请在 PowerShell 中使用 Get-ChildItem cmdlet。

使用 Get-ChildItem Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Recurse | %{$_.FullName}

输出 :

D:\PowerShell\Testing\important
D:\PowerShell\Testing\a.py
D:\PowerShell\Testing\abc.txt
D:\PowerShell\Testing\dummy.txt
D:\PowerShell\Testing\hello.py
D:\PowerShell\Testing\How neuroencoding improves performance-1.docx
D:\PowerShell\Testing\myApp.py
D:\PowerShell\Testing\myApp2.py
D:\PowerShell\Testing\my_dict.json
D:\PowerShell\Testing\To do.txt
D:\PowerShell\Testing\important\alexa.txt
D:\PowerShell\Testing\important\django.py
D:\PowerShell\Testing\important\neuro.py
D:\PowerShell\Testing\important\python.txt
D:\PowerShell\Testing\important\result.docx

在上面的代码中,使用 Get-ChildItem cmdlet 获取 D:\PowerShell\Testing 目录及其子目录中可用文件的全名。 这里,-Path参数可以指定我们要从中获取文件路径的目录路径。

我们来一一看看上面命令中用到的所有参数来理解一下:

  • -Recurse 参数通知 PowerShell 递归搜索 D:\PowerShell\Testing 目录及其所有子目录。
  • | 称为管道运算符;它可以将上一个命令的输出作为下一个命令的输入。在此示例中,它将 D:\PowerShell\Testing 目录中的所有文件名称传递给此 %{$_.FullName} 命令。
  • % 用作 ForEach-Object cmdlet 的别名
  • {$_.FullName} 显示我们要对所选对象执行的操作。顾名思义,我们想要获取指定目录下所有文件的全名。

现在让我们考虑一个场景,我们必须从指定目录中提取所有 .txt 文件。为此,请看下面的示例:

使用 Get-ChildItem Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | %{$_.FullName}

输出 :

D:\PowerShell\Testing\abc.txt
D:\PowerShell\Testing\dummy.txt
D:\PowerShell\Testing\To do.txt
D:\PowerShell\Testing\important\alexa.txt
D:\PowerShell\Testing\important\python.txt

在此示例中,-Filter 参数与 Get-ChildItem cmdlet 一起使用,以过滤目录中可用的所有 .txt 文件。 >D:\PowerShell\Testing 及其子目录。

要获取当前目录中文件的完整路径,请使用不带路径属性的 Get-ChildItem

使用 Get-ChildItem Cmdlet:

Get-ChildItem -Recurse | %{$_.FullName}

输出 :

D:\PowerShell\Testing\important
D:\PowerShell\Testing\a.py
D:\PowerShell\Testing\abc.txt
D:\PowerShell\Testing\dummy.txt
D:\PowerShell\Testing\hello.py
D:\PowerShell\Testing\How neuroencoding improves performance-1.docx
D:\PowerShell\Testing\myApp.py
D:\PowerShell\Testing\myApp2.py
D:\PowerShell\Testing\my_dict.json
D:\PowerShell\Testing\To do.txt
D:\PowerShell\Testing\important\alexa.txt
D:\PowerShell\Testing\important\django.py
D:\PowerShell\Testing\important\neuro.py
D:\PowerShell\Testing\important\python.txt
D:\PowerShell\Testing\important\result.docx

ForEach-Object Cmdlet 与 Get-ChildItem 结合使用

要获取目录中文件的完整路径,请在 PowerShell 中使用 Get-ChildItem cmdlet 和 ForEach-Object cmdlet。

使用 ForEach-Object Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.py -Recurse  | ForEach-Object{$_.FullName} 

输出 :

D:\PowerShell\Testing\a.py
D:\PowerShell\Testing\hello.py
D:\PowerShell\Testing\myApp.py
D:\PowerShell\Testing\myApp2.py
D:\PowerShell\Testing\important\django.py
D:\PowerShell\Testing\important\neuro.py

在此示例中,Get-ChildItemForEach-Object 一起使用来过滤指定目录中可用的具有 .py 扩展名的所有文件。文件夹及其子文件夹。这里,Get-ChildItem 递归地获取所有文件的名称,并将它们传递给 ForEach-Object cmdlet,以获取目录中每个文件的完整路径。

假设您想要获取给定目录及其子目录中文件的完整路径以及文件的大小。

使用 ForEach-Object Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter "*.txt" | ForEach-Object { $_.FullName + " has a size of " + $_.Length + " bytes." }

输出 :

D:\PowerShell\Testing\abc.txt has a size of 814 bytes.
D:\PowerShell\Testing\dummy.txt has a size of 406 bytes.
D:\PowerShell\Testing\To do.txt has a size of 129 bytes.

我们可以观察所有具有 .txt 扩展名的文件的路径及其大小。这里,$_.Length参数用于获取每个文件的长度/大小。

Select-Object Cmdlet 与 Get-ChildItem 结合使用

要检索目录中文件的完整路径,请在 PowerShell 中使用 Get-ChildItemSelect-Object cmdlet。

使用选择对象 Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.py -Recurse | Select-Object -ExpandProperty FullName

输出 :

FullName
--------
D:\PowerShell\Testing\a.py
D:\PowerShell\Testing\hello.py
D:\PowerShell\Testing\myApp.py
D:\PowerShell\Testing\myApp2.py
D:\PowerShell\Testing\important\django.py
D:\PowerShell\Testing\important\neuro.py

在上面的命令中,Select-ObjectGet-ChildItem 一起使用。这是获取指定目录中可用文件的路径的另一种方法。这里,Select-Object 用于选择对象的特定属性,在我们的例子中是ExpandProperty FullName。这用于扩展对象的 FullName 属性,以获取给定目录及其子目录中可用文件的完整路径,其中 D:\PowerShell\Testing上面的例子。

Format-List Cmdlet 与 Get-ChildItem 结合使用

要访问目录中文件的完整路径,请在 PowerShell 中使用 Get-ChildItem cmdlet 和 Format-List

使用格式列表 Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | Format-List FullName

输出 :

FullName : D:\PowerShell\Testing\abc.txt
FullName : D:\PowerShell\Testing\dummy.txt
FullName : D:\PowerShell\Testing\To do.txt
FullName : D:\PowerShell\Testing\important\alexa.txt
FullName : D:\PowerShell\Testing\important\python.txt

上述命令中使用 Format-List cmdlet 来获取列表形式的输出。上面的示例获取了 D:\PowerShell\Testing 目录及其子目录中扩展名为 .txt 的文件的路径。上面的输出经过格式化并在每个文件路径的开头写入 FullName:

让我们看另一个示例,获取所有带有 .docx 扩展名的文件。

使用格式列表 Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.docx -Recurse | Format-List FullName

输出 :

FullName : D:\PowerShell\Testing\How neuroencoding improves performance-1.docx
FullName : D:\PowerShell\Testing\important\result.docx

这段代码与前一段类似。不过,这一次,我们获得了所有扩展名为 .docx 的文件的路径。

Format-Table Cmdlet 与 Get-ChildItem 结合使用

要获取目录中文件的完整路径,请在 PowerShell 中使用 Get-ChildItem cmdlet 和 Format-Table cmdlet。

使用格式表 Cmdlet:

Get-ChildItem -Path "D:\PowerShell\Testing" -Filter *.txt -Recurse | Format-Table FullName  -AutoSize

输出 :

FullName
--------
D:\PowerShell\Testing\abc.txt
D:\PowerShell\Testing\dummy.txt
D:\PowerShell\Testing\To do.txt
D:\PowerShell\Testing\important\alexa.txt
D:\PowerShell\Testing\important\python.txt

在上面的 PowerShell 代码段中,将 Format-Table cmdlet 与 Get-ChildItem cmdlet 一起使用来获取带有 .txt 的文件的完整路径 扩展名在指定目录及其子目录中可用。由于使用 Format-Table cmdlet,输出被格式化为表格,并显示每个文件的完整路径。 -AutoSize 参数用于调整列的宽度以适合数据。

这就是如何在 PowerShell 中获取文件的完整路径。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯