首页 > 解决方案 > how to find and store filenames in a list with PowerShell?

问题描述

In the context of emulating the tree command, looking at files in sub-directories:

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> 

How is the filename above assigned to a variable?

An array or list of String would be sufficient. But how is the "Name" above captured? Or, for that matter, the "Directory" or other attributes?

标签: powershellloopsfiletreeget-childitem

解决方案


Most cmdlets in powershell return objects. Objects have properties. When you do Get-ChildItem that returns a collection of DirectoryInfo and FileInfo objects each with their own set of properties, albeit very similar.

The following command will retrieve all the files in the path $dir as FileInfo objects and will add them to an array contained in $files

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

Now, each object in $files is a FileInfo object which contain properties like Name, BaseName, Directory, LastWriteTime, CreationTime, Extension, and many more. To see what properties exist on an object you can pipe | the object to Get-Member

$files | Get-Member

This will provide some information about the type of object and its properties and methods. The following list has been truncated for brevity. You may and should try this on your own.

   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;}

Now that you know what properties exist on the objects you may access them like so

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

or

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

You can also pipe the objects to Select-Object to get modified objects back with only the properties you want or even custom (calculated properties).

$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

Now that is only a small intro to Powershell. There is really much much more to it. I've seen your other posts and see that you are interested. There are many really good tutorials out there. I recommend that you have a look at a few of them and see all that there really is to learn. For starters, have a look at this one about objects in powershell


推荐阅读