【问题标题】:Get list of a directory with powershell. With Created and Modified columns使用 powershell 获取目录列表。使用 Created 和 Modified 列
【发布时间】:2021-12-24 13:51:23
【问题描述】:

我有这个代码:

Get-ChildItem  -recurse | fl name, creationtime, lastwritetime

我得到了这样的结果:

Name          : name.exe
CreationTime  : 18-Dec-21 13:15:16
LastWriteTime : 18-Dec-21 13:15:17

Name          : name.txt
CreationTime  : 24-Dec-21 13:15:44
LastWriteTime : 24-Dec-21 13:27:43

Name          : name.png
CreationTime  : 12-Dec-21 23:55:48
LastWriteTime : 12-Dec-21 23:55:49

Name          : name.csv
CreationTime  : 24-Dec-21 14:15:19
LastWriteTime : 24-Dec-21 14:15:19

Name          : name.csv
CreationTime  : 24-Dec-21 14:16:41
LastWriteTime : 24-Dec-21 14:31:49

Name          : name.csv
CreationTime  : 24-Dec-21 14:08:07
LastWriteTime : 24-Dec-21 14:08:07

Name          : name.csv
CreationTime  : 17-Dec-21 14:02:42
LastWriteTime : 17-Dec-21 14:02:42

我想从命令行格式化列表到表格中。所以我尝试使用这段代码:

Get-ChildItem  -recurse | ft name, creationtime, lastwritetime

但我得到了一个垃圾结果。太垃圾了,我什至无法重新创建它,那是因为名称中有很多字符的文件。

如果我在一个目录中使用相同的代码,其中的文件名称中的字符较少,我可以获得一个像样的文件列表,如下所示:

Name      CreationTime       LastWriteTime     
----      ------------       -------------     
name.txt 24-Dec-21 14:57:04 24-Dec-21 14:57:04
name.txt 24-Dec-21 13:15:44 24-Dec-21 13:27:43
name.csv 24-Dec-21 14:15:19 24-Dec-21 14:15:19
name.csv 24-Dec-21 14:16:41 24-Dec-21 14:31:49
name.csv 24-Dec-21 14:08:07 24-Dec-21 14:08:07

我的问题是如何在命令行选项中设置缩短名称。可选择使用字符过滤器。

谢谢。

【问题讨论】:

    标签: powershell-ise


    【解决方案1】:

    您可以使用格式表来设置您希望输出的显示方式。这是一个示例,您可以根据自己的需要使用和调整。

    Clear-Host
    $fmtGCI = @{Expression={$_.Name};
                Label="File Name";Width=50;Align="Left"},
              @{Expression={$_.CreationTime.Date.ToString("yyyy-MM-dd")};
                Label="Creation";Width=10},
              @{Expression={$_.LastWriteTime.Date.ToString("yyyy-MM-dd")};
                Label="Modified";Width=10}
    
    Get-ChildItem -Path 'G:\BEKDocs\ACTS\Communications Committee\K4Training App' | 
         FT $fmtGCI
    

    注意:为文件名设置较短的宽度只会截断它,因此请注意如何设置它。

    当然,如果需要,您可以添加更多项目,例如文件时间。您还可以将名称分解为 Basename 和 Ext。

    样本输出:

    File Name                                          Creation   Modified  
    ---------                                          --------   --------  
    Videos                                             2021-12-16 2021-12-21
    Bruce Edit.png                                     2021-10-04 2021-10-04
    Bruce interests.png                                2021-10-04 2021-10-04
    Dining Chophouse menus.png                         2021-10-04 2021-10-04
    Dining desert menu.png                             2021-10-04 2021-10-04
    Directory Fitness search.png                       2021-10-04 2021-10-04
    Directory Gardening search.png                     2021-10-04 2021-10-04
    

    【讨论】:

    • 您还可以将名称分解为 Basename 和 Ext。
    • 如何添加创作时间?我试图在这里做广告: ("yyyy-MM-dd") hh:mm:ss 并尝试使用 HH:mm:ss 但无济于事。我得到的是所有文件的标准时间 12:00:00 或 00:00:00,我感兴趣的是在 mm 和 ss 级别上查看 Cration 和 Moddified 之间的区别。谢谢。
    【解决方案2】:

    我不能接受 RetiredGeek 的帖子作为答案,但我可以在这里接受一半的答案。

    这是一半(或超过 80% 的正确答案),因为我弄清楚要改变什么才能得到我想要的/nedded。

    我的主要范围是在 mmss 级别上了解 CreatedModified 之间的差异.

    拿走他的代码并修改:

              @{Expression={$_.CreationTime.Date.ToString("yyyy-MM-dd")};
                Label="Creation";Width=10},
    

    与:

              @{Expression={$_.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")};
                Label="Creation";Width=20},
    

    所以现在代码如下所示:

    Clear-Host
    $fmtGCI = @{Expression={$_.Name};
                Label="File Name";Width=50;Align="Left"},
              @{Expression={$_.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")};
                Label="Creation";Width=20},
              @{Expression={$_.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")};
                Label="Modified";Width=20}
    Get-ChildItem -Path 'D:\+\_+_\2021\12 2021' | FT $fmtGCI
    

    我得到的是这样的:

    File Name                                          Creation             Modified            
    ---------                                          --------             --------            
    Testing1 Testiy Noname1 #Serie - 'Fileyx filexx... 2021-12-31 03:19:23  2021-12-31 03:19:23
    Testing2 Testiy Noname1 #Serie - 'Fileyx filexx... 2021-12-31 04:07:07  2021-12-31 04:07:07
    Testing3 Testiy Noname1 #Serie - 'Fileyx filexx... 2021-12-31 06:05:33  2021-12-31 06:05:33
    Testing4 Testiy Noname1 #Serie - 'Fileyx filexx... 2021-12-31 07:21:24  2021-12-31 07:21:24
    Testing5 Testiy Noname1 #Serie - 'Fileyx filexx... 2021-12-31 08:35:48  2021-12-31 08:35:48
    

    非常感谢@RetiredGeek

    新年快乐:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-11
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 2016-06-28
      相关资源
      最近更新 更多