简体   繁体   English

按文件名 Powershell 中的日期列出文件

[英]List files by date in filename Powershell

I hope you are well.我希望你一切都好。

I am new in Powershell and am trying to make a script that allow to list files 7 days old based on the date in the filename, then copy/move them to another directory.我是 Powershell 的新手,正在尝试制作一个脚本,允许根据文件名中的日期列出 7 天前的文件,然后将它们复制/移动到另一个目录。

File Structure: 2020_06_11_DB.txt YYYY_MM_dd文件结构:2020_06_11_DB.txt YYYY_MM_dd

I have this piece of script set up but it only match the file with the reference date (DateRef) and not the least (old) of it:我设置了这段脚本,但它只将文件与参考日期(DateRef)相匹配,而不是至少(旧):

$SourceDB = "C:\DB\"
$DestinationDB = "C:\Destination\DB\"
$DateRef = Get-Date ((Get-Date).AddDays(-7)) -Format yyyy_MM_dd

$Query = Get-ChildItem $SourceDB -Filter "*$DateRef*" |
ForEach-object {
    Copy-Item -Path $_.FullName -Destination $DestinationDB
}

I tried operators like -le and -lt but with no success.我尝试过像 -le 和 -lt 这样的运算符,但没有成功。 I would appreciate if you can help me.如果你能帮助我,我将不胜感激。

Get-ChildItem $SourceDB -Filter '*.txt' | Where-Object {$_.BaseName -le "*$DateRef*"}

Thank you!谢谢!

When comparing dates, you will want to work with DateTime objects.比较日期时,您需要使用DateTime对象。 So the challenge is extracting date strings from file names and converting them to DateTime objects before doing the comparison.因此,挑战是在进行比较之前从文件名中提取日期字符串并将它们转换为DateTime对象。

$SourceDB = "C:\DB\"
$DestinationDB = "C:\Destination\DB\"
$DateRef = (Get-Date).AddDays(-7).Date

$Query = Get-ChildItem $SourceDB -File | Where {
    $_.BaseName -match '\d{4}_\d{2}_\d{2}' -and [datetime]::ParseExact(([regex]::Match($_.BaseName,'\d{4}_\d{2}_\d{2}').Value),'yyyy_MM_dd',$null) -le $DateRef } |
        Foreach-Object {
            Copy-Item -Path $_.FullName -Destination $DestinationDB
        } 
    

Explanation:解释:

I don't want to discourage you from using -Filter because it is better than filtering inside of Where-Object .我不想阻止您使用-Filter因为它比在Where-Object内部过滤要好。 So you could filter partially that way first with something like -Filter '*_*_*' or wildcard your path -Path "$SourceDB*[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]*" .因此,您可以首先使用-Filter '*_*_*'或通配符您的路径-Path "$SourceDB*[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]*"部分过滤-Path "$SourceDB*[0-9][0-9][0-9][0-9]_[0-9][0-9]_[0-9][0-9]*"

Using -File parameter in Get-Childitem , returns only files.使用-File参数Get-Childitem ,只返回文件。

(Get-Date).AddDays(-7).Date is 7 days before today at midnight. (Get-Date).AddDays(-7).Date是今天午夜前 7 天。

$_.BaseName -match '\\d{4}_\\d{2}_\\d{2}' matches files with format ####_##_## . $_.BaseName -match '\\d{4}_\\d{2}_\\d{2}'匹配格式为####_##_##

[regex]::Match($_.BaseName,'\\d{4}_\\d{2}_\\d{2}').Value returns the matched string ####_##_## . [regex]::Match($_.BaseName,'\\d{4}_\\d{2}_\\d{2}').Value返回匹配的字符串####_##_## [datetime]::ParseExact() converts the matched string (format 'yyyy_MM_dd') into a DateTime object. [datetime]::ParseExact()将匹配的字符串(格式为 'yyyy_MM_dd')转换为DateTime对象。

-le is less than or equal to operator. -le小于或等于运算符。 So -le $DateRef returns dates older than or equal to $DateRef .所以-le $DateRef返回早于或等于$DateRef

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

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