如何使用 PowerShell 在列表中查找和存储文件名?

问题描述

emulating tree 命令的上下文中,查看子目录中的 files

posh> 
posh> $dir = "/home/nicholas/Calibre Library/Microsoft Office User/549 (1476)"
posh>                                                                         
posh> Get-ChildItem -Path $dir –File                                          

    Directory: /home/nicholas/Calibre Library/Microsoft Office User/549 (1476)

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-----           2/20/2021  3:22 AM         159883 549 - Microsoft Office User.txt
-----           2/20/2021  2:13 AM         351719 cover.jpg
-----           2/20/2021  2:31 AM           1126 Metadata.opf

posh> 

上面的文件名是如何分配给变量的?

String 的数组或列表就足够了。但是上面的“名称”是如何捕获的?或者,就此而言,“目录”或其他属性

解决方法

powershell 中的大多数 cmdlet 都返回对象。对象具有属性。当您执行 Get-ChildItem 时,它返回 DirectoryInfo 和 FileInfo 对象的集合,每个对象都有自己的一组属性,尽管非常相似。

以下命令将检索路径 $dir 中的所有文件作为 FileInfo 对象,并将它们添加到包含在 $files 中的数组

$files = Get-ChildItem -Path $dir –File

现在,$files 中的每个对象都是一个 FileInfo 对象,其中包含 Name、BaseName、Directory、LastWriteTime、CreationTime、Extension 等属性。要查看对象上存在哪些属性,您可以通过管道 | 将该对象传递给 Get-Member

$files | Get-Member

这将提供有关对象类型及其属性和方法的一些信息。为简洁起见,以下列表已被截断。您可以也应该自己尝试一下。

   TypeName: System.IO.FileInfo

Name                      MemberType     Definition
----                      ----------     ----------
AppendText                Method         System.IO.StreamWriter AppendText()
CopyTo                    Method         System.IO.FileInfo CopyTo(string destFileName),System.IO.FileInfo CopyTo(string destFileName,...
Create                    Method         System.IO.FileStream Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreationTime              Property       datetime CreationTime {get;set;}
CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}
Directory                 Property       System.IO.DirectoryInfo Directory {get;}
DirectoryName             Property       string DirectoryName {get;}
Exists                    Property       bool Exists {get;}
Extension                 Property       string Extension {get;}
FullName                  Property       string FullName {get;}
Length                    Property       long Length {get;}
Name                      Property       string Name {get;}

既然您知道对象上存在哪些属性,您就可以像这样访问它们

PS C:\temp> $files.Name
test.ps1
test.xml
test1.xlsx
test2.csv
testemail.csv
testout.xml
testxml.xml
write.xml

PS C:\temp> $files.FullName
C:\temp\test.ps1
C:\temp\test.xml
C:\temp\test1.xlsx
C:\temp\test2.csv
C:\temp\testemail.csv
C:\temp\testout.xml
C:\temp\testxml.xml
C:\temp\write.xml

您还可以将对象通过管道传输到 Select-Object,以便仅使用您想要的属性或什至自定义(计算属性)返回修改后的对象。

$files | Select-Object Name,CreationTime,@{Label='Age'; Expression= {((Get-Date).Date - ($_.CreationTime).Date).Days}}

Name                                  CreationTime        Age
----                                  ------------        ---
test.ps1                              19.02.2021 10:56:25   3
test.xml                              14.02.2021 19:28:19   8
test1.xlsx                            04.02.2021 19:31:54  18
test2.csv                             04.02.2021 23:00:46  18
testemail.csv                         03.02.2021 15:35:43  19
testout.xml                           14.02.2021 19:32:03   8
testxml.xml                           14.02.2021 19:33:41   8
write.xml                             08.02.2021 17:26:40  14

现在这只是 Powershell 的一个小介绍。其实还有很多。我看过你的其他帖子,看到你有兴趣。那里有很多非常好的教程。我建议您看一看其中的一些,了解真正需要学习的所有内容。首先,查看 this one 关于 powershell 中的对象

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...