【问题标题】:PowerShell moving files based on datesPowerShell 基于日期移动文件
【发布时间】:2016-10-17 21:31:07
【问题描述】:

我正在尝试将所有六个月或更早的文件移动到存档文件中 我有

#set root folder
$baseFolder = "C:\Users\Caleb\Desktop"
$archiveBase = "C:\Users\Caleb\Desktop"
$limit_to=(Get-date (Get-date -Format d)).AddDays(-180)
#get files inside root
$folder = Get-ChildItem $baseFolder -File

#for each folder

foreach($file in Where-$folder.CreationTime -lt $limit_to ) {

    #build the move destination path
    $a=$file.CreationTime.Date.Year 
    $archive="Archive"
    $name=$a+" "+$archive
    $destination = Join-Path $archiveBase $name


    #Create Directory And Deposite Files
    if(Test-Path $destination)
    {
        mv $file $destination
    }
    else
    {
        mkdir $destination
        mv $file $destination
    }

}

我似乎无法仅获取超过 180 天的文件。 我是初学者,所以我愿意做出任何效率上的改变

【问题讨论】:

  • 您可以通过((Get-ChildItem).CreationTime).Year 访问文件的创建年份。只需将其适当地包含在copy-item 的目标路径中即可。
  • 好吧,对不起,我认为你现在可以解决我的问题@davidhigh

标签: powershell


【解决方案1】:

试试这个

  $baseFolder = "C:\Users\Caleb\Desktop\"
  $archiveBase = "C:\Users\Caleb\Desktop\"
  $limit_to=(Get-date (Get-date -Format d)).AddMonths(-6)

  mkdir $archiveBase -force

  Get-ChildItem $baseFolder -File | where CreationTime -lt $limit_to | foreach {move-item $_.FullName -Destination ($archiveBase + $_.CreationTime.Year + " Archive" + $_.Name) -Force }

【讨论】:

    猜你喜欢
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2021-06-14
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多