【问题标题】:Powershell - get a simple list of files (with their path included) using Get-ChildItemPowershell - 使用 Get-ChildItem 获取一个简单的文件列表(包括它们的路径)
【发布时间】:2020-04-21 10:41:41
【问题描述】:

我想要一个 Powershell 脚本,它输出通过递归找到的文件,并在一定时间后修改日期。

目前,它是一个这样的命令;

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } 

但是,输出并不容易处理,因为我得到了这样的结果:

            Directory: C:\Test
            Mode                LastWriteTime         Length Name
            ----                -------------         ------ ----
            ------        02/01/2020    15:58       69005864 x.exe
            ------        02/01/2020    15:17         144182 y.pptx
            ------        02/01/2020    17:34       57452572 z.exe

我希望我的输出改为:

C:\Test\x.exe
C:\Test\y.pptx
C:\Test\z.exe

以及所有其他文件夹中的文件。怎么做呢?

【问题讨论】:

  • 您正在寻找FullName 属性。 [code] | Select-Object -ExpandProperty FullName

标签: windows powershell file search scripting


【解决方案1】:

powershell 的输出是一种错觉。它实际上是在输出具有一组属性的对象。某些对象类型具有控制其外观的格式文件。查看真实属性列表的一种方法是将其传递给format-list * 或简称fl *。您可以在此处看到 FullName 属性,其中显示了文件的完整路径。即使在此清单中,VersionInfo 属性也会从某些格式文件中获得额外的格式。

get-childitem foo | fl *


PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\users\js\foo\foo
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\users\js\foo
PSChildName       : foo
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : False
Mode              : -a----
VersionInfo       : File:             C:\users\js\foo\foo
                    InternalName:
                    OriginalFilename:
                    FileVersion:
                    FileDescription:
                    Product:
                    ProductVersion:
                    Debug:            False
                    Patched:          False
                    PreRelease:       False
                    PrivateBuild:     False
                    SpecialBuild:     False
                    Language:

BaseName          : foo
Target            : {}
LinkType          :
Name              : foo
Length            : 4
DirectoryName     : C:\users\js\foo
Directory         : C:\users\js\foo
IsReadOnly        : False
Exists            : True
FullName          : C:\users\js\foo\foo
Extension         :
CreationTime      : 1/2/2020 1:44:37 PM
CreationTimeUtc   : 1/2/2020 6:44:37 PM
LastAccessTime    : 1/2/2020 1:44:37 PM
LastAccessTimeUtc : 1/2/2020 6:44:37 PM
LastWriteTime     : 1/2/2020 1:44:37 PM
LastWriteTimeUtc  : 1/2/2020 6:44:37 PM
Attributes        : Archive

另一个例子。通常标题与属性名称匹配:

dir | select mode,lastwritetime,length,name

Mode   LastWriteTime         length Name
----   -------------         ------ ----
d----- 1/3/2020 11:35:44 AM         foo2
-a---- 1/2/2020 3:53:32 PM   4      0.txt
-a---- 1/2/2020 3:53:37 PM   4      9.txt
-a---- 12/24/2019 1:45:07 PM 42     file.json
-a---- 12/23/2019 2:26:40 PM 63     file.json~
-a---- 1/2/2020 1:44:37 PM   4      foo
-a---- 1/2/2020 3:54:14 PM   50     listnum.ps1
-a---- 1/2/2020 3:53:15 PM   41     listnum.ps1~

【讨论】:

    【解决方案2】:

    这是一个很好的问题,尤其是在命令提示符背景下,您可以轻松输入 dir /s/b 并获得类似的结果。这是我通常做的事情:

    Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } | ForEach-Object { $_.FullName }
    

    其实我是用别名来减少打字的:

    dir -r | ?{ $_.LastWriteTime -ge "01/01/2020 19:57:00" } | %{ $_.FullName }
    

    或者如果您想按照@Ash 的建议使用Select-Object

    Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "01/01/2020 19:57:00" } | Select-Object -Property FullName | Format-Table -HideTableHeaders -AutoSize
    

    使用别名:

    dir -r | ?{ $_.LastWriteTime -ge "01/01/2020 19:57:00" } | Select-Object FullName | ft -a -h
    

    后者稍微不简洁。

    【讨论】:

    • 我的建议还不错,但在它变得不那么简洁之前。
    • OP 要求提供一个简单的列表。只需添加Select-Object ... 默认情况下会添加标题,这不会返回简单列表。
    • 不像我的建议中所说的-ExpandProperty
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    相关资源
    最近更新 更多