簡體   English   中英

根據文件名中的日期戳移動文件

[英]Move Files Based on Date Stamp in File Name

不幸的是,我是PowerShell的新手,我有一些每月想用PowerShell存檔的文件。 每個文件的文件名中都有YYYYMM日期戳。 我想移動日期戳早於24個月的文件。

例:

file1_201903.txt  
file2_201902.txt  
...  
file3_201703.txt (this should be archived)  
file4_201702.txt (this should be archived)

請注意,源文件位於帶有多個子文件夾的目錄中。 我希望腳本檢查所有子文件夾。 無需在目標中復制文件夾。

到目前為止,這是我嘗試過的方法:

$SourceDir = 'C:\source'
$DestDir   = 'C:\destination'
$YearsAgo  = 2

$Then = (Get-Date).AddYears(-$YearsAgo).Date

Get-ChildItem -Path $SourceDir |
    Where-Object {
        $DatePart = ($_.BaseName -split '_')[1]

        $FileDate = [DateTime]::ParseExact($DatePart, 'yyyyMMdd', [CultureInfo]::CurrentCulture)

        $FileDate -lt $Then
    } |
    Move-Item -Destination $DestDir

文件名中的日期部分沒有Day的值。 因此,格式應為yyyyMM ,而不是yyyyMMdd

由於格式是可排序的字符串,因此您不必轉換為DateTime對象,可以繼續比較字符串:

$SourceDir = 'C:\source'
$DestDir   = 'C:\destination'
$YearsAgo  = -2
$Then      = '{0:yyyyMM}' -f (Get-Date).AddYears($YearsAgo)  # returns a String "201703"

Get-ChildItem -Path $SourceDir | ForEach-Object {
    $DatePart = ( $_.BaseName -split '_' )[1]
    # comparing sortable date strings
    if ($DatePart -lt $Then) {
        $_ | Move-Item -Destination $DestDir
    } 
}

如果確實要比較DateTime對象,則應這樣做:

$SourceDir = 'C:\source'
$DestDir   = 'C:\destination'
$YearsAgo  = -2

$RefDate   = ('{0:yyyyMM}' -f (Get-Date).AddYears($YearsAgo))  # returns a String "201703"
# convert this string into a DateTime object
$Then      = [DateTime]::ParseExact( $RefDate, 'yyyyMM', [cultureinfo]::CurrentCulture )

Get-ChildItem -Path $SourceDir | ForEach-Object {
    $DatePart = ( $_.BaseName -split '_' )[1]
    $FileDate = [DateTime]::ParseExact( $DatePart, 'yyyyMM', [cultureinfo]::CurrentCulture )
    # comparing DateTime objects
    if ($FileDate -lt $Then) {
        $_ | Move-Item -Destination $DestDir
    } 
}

實際上,那看上去還不錯。 ;-)但是您的Where-Object -FilterScript塊需要一些調整:

$SourceDir = 'C:\source'
$DestDir   = 'C:\destination'
$YearsAgo  = -2
$Then = ( Get-Date ).AddYears( $YearsAgo ).Date

Get-ChildItem -Path $SourceDir -Recurse |
    Where-Object {
        [datetime]::ParseExact( $(( $_.BaseName -split '_' )[1]), 'yyyyMMdd', [cultureinfo]::CurrentCulture ) -lt $Then
    } |
        Move-Item -Destination $DestDir

您可以對Foreach-Object使用更具描述性的方式。 有時候,這樣更容易閱讀/理解/關注:

Get-ChildItem -Path $SourceDir -Recurse |
    ForEach-Object{
        $DatePart = ( $_.BaseName -split '_' )[1]
        $FileDate = [datetime]::ParseExact( $DatePart, 'yyyyMMdd', [cultureinfo]::CurrentCulture )
        if ($FileDate -lt $Then) {
            Move-Item -Path $_.FullName -Destination $DestDir
        } 
    }

不幸的是,我目前無法測試。 所以請讓我知道。 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM