Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PowerShell在列表中查找和存储文件名?_Powershell_Loops_File_Tree_Get Childitem - Fatal编程技术网

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

如何使用PowerShell在列表中查找和存储文件名?,powershell,loops,file,tree,get-childitem,Powershell,Loops,File,Tree,Get Childitem,在树命令的上下文中,查看子目录中的: posh> posh> $dir = "/home/nicholas/Calibre Library/Microsoft Office User/549 (1476)" posh> posh> Get-ChildItem -Path $dir –File

命令的上下文中,查看子目录中的:

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> 
上面的文件名是如何分配给变量的


一个
字符串的数组或列表就足够了。但是上面的“名字”是如何被捕捉的呢?或者,“目录”或其他属性?

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

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

$files = Get-ChildItem -Path $dir –File
现在,$files中的每个对象都是一个FileInfo对象,它包含诸如Name、BaseName、Directory、LastWriteTime、CreationTime、Extension等属性。要查看对象上存在哪些属性,可以通过管道
|
对象以获取成员

$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

您还可以通过管道将对象传输到
选择对象
,以仅使用所需的特性甚至自定义特性(计算特性)获取修改后的对象


现在,这只是Powershell的一个小介绍。其实还有很多。我看过你的其他帖子,看到你很感兴趣。有很多非常好的教程。我建议你看看其中的一些,看看真正需要学习的东西。对于初学者,请看一看powershell中的about对象

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

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

$files = Get-ChildItem -Path $dir –File
现在,$files中的每个对象都是一个FileInfo对象,它包含诸如Name、BaseName、Directory、LastWriteTime、CreationTime、Extension等属性。要查看对象上存在哪些属性,可以通过管道
|
对象以获取成员

$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

您还可以通过管道将对象传输到
选择对象
,以仅使用所需的特性甚至自定义特性(计算特性)获取修改后的对象

现在,这只是Powershell的一个小介绍。其实还有很多。我看过你的其他帖子,看到你很感兴趣。有很多非常好的教程。我建议你看看其中的一些,看看真正需要学习的东西。对于初学者,请了解一下powershell中的对象

$fileNames=(Get ChildItem-Path$dir-File).Name
。。只需使用
Get ChildItem-Path$dir-File | fl*
$fileNames=(Get ChildItem-Path$dir-File).Name查找类中的内容以及PowerShell为其添加的内容。。只需使用
getchilditem-Path$dir-File | fl*