简体   繁体   English

使用PowerShell按日期筛选文件夹

[英]Using PowerShell to filter folders by date

I am going through folders we have on a work drive that have files over 20 years old in them. 我正在查看工作驱动器上的文件夹,这些文件夹中的文件已有20年以上的历史了。 We want to move these files off this drive to clear up room and organize it better. 我们希望将这些文件从该驱动器上移开,以清理空间并更好地组织它。

I have a PowerShell script to look through the folders and output to a csv file. 我有一个PowerShell脚本可以浏览文件夹并输出到csv文件。

It puts into the csv file but there are thousands of files here. 它放入csv文件,但是这里有成千上万个文件。 We only want to have the file with the latest modified on there. 我们只想在该文件上修改最新的文件。 It is giving me every file. 它给了我所有文件。

Can I have it only output the latest modified file from that folder and then move onto the next folder? 我能否仅从该文件夹输出最新的修改文件,然后移至下一个文件夹?

Get-ChildItem -LiteralPath Y:\XYZ\MainFolder -Recurse -Depth 2  |Format-Table| Out-File C:\Users\USER\Desktop\aCSVfile.csv

You'll need PowerShell 5.x otherwise, the -Directory switch of Get-ChildItem will not be available: 您将需要PowerShell 5.x,否则, Get-ChildItem-Directory开关将不可用:

Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } | Export-Csv "test.csv" -NoTypeInformation -Delimiter ";"
  • Get-ChildItem -Path . -Directory -Recurse Get-ChildItem -Path . -Directory -Recurse - Get all directories Get-ChildItem -Path . -Directory -Recurse获取所有目录
  • Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } - check if the last write time is older than 2018-10-04 (you may also use LastAccessTime ) Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } -检查上次写入时间是否早于2018-10-04(您也可以使用LastAccessTime
  • Export-Csv "test.csv" -NoTypeInformation -Delimiter ";" - exports the filtered objects to CSV file. -将过滤的对象导出到CSV文件。

If you want less information in the CSV file you can use Select-Object : 如果您想在CSV文件中减少信息,可以使用Select-Object

Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } |Select-Object Name, PSPath, Parent | Export-Csv "test.csv" -NoTypeInformation -Delimiter ";"          

To get available properties use Get-Member : 要获取可用属性,请使用Get-Member

 Get-ChildItem -Path . -Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date "2018-10-04") } | Get-Member

Output: 输出:

    TypeName: System.IO.DirectoryInfo

    Name                      MemberType     Definition
    ----                      ----------     ----------
    LinkType                  CodeProperty   System.String LinkType{get=GetLinkType;}
    Mode                      CodeProperty   System.String Mode{get=Mode;}
    ... 
    FullName                  Property       string FullName {get;}
    LastAccessTime            Property       datetime LastAccessTime {get;set;}
    LastAccessTimeUtc         Property       datetime LastAccessTimeUtc {get;set;}
    LastWriteTime             Property       datetime LastWriteTime {get;set;}
    LastWriteTimeUtc          Property       datetime LastWriteTimeUtc {get;set;}
    Name                      Property       string Name {get;}
    ...
    Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
    CreationTime              Property       datetime CreationTime {get;set;}
    CreationTimeUtc           Property       datetime CreationTimeUtc {get;set;}
    ...

From here you can use the *Property member types in Select-Object and Where-Object . 在这里,您可以使用Select-ObjectWhere-Object*Property成员类型。

If you are on PowerShell less than 5, you've to check the PSIsContainer property, as explained in this stackoverflow answer . 如果您使用的PowerShell少于5,则必须检查PSIsContainer属性,如本stackoverflow 答案中所述

Hope that's the information you're looking for. 希望这是您正在寻找的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM