zoukankan      html  css  js  c++  java
  • Powershell按文件最后修改时间删除多余文件

    1. 删除目录内多余文件,目录文件个数大于$count后,按最后修改时间倒序排列,删除最旧的文件。

    Sort-Object -Property LastWriteTime -Descending 按照文件的最后修改时间倒序排列

    Select-Object -Skip $count 跳过开头的$count条数据,取剩余的数据
     
    $count = 3
    $filePathList = "E:MySql1",
    "E:MySql2",
    "E:MySql3"
    
    foreach($filePath in $filePathList)
    {
        $files = Get-ChildItem -Path $filePath | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip $count
        if ($files.count -gt 0) {
            foreach($file in $files)
            {
                Remove-Item $file.FullName -Recurse -Force
            }
        }    
    }
     
     
     
     
     
     

    cd D:FtpServer小平台NewJob
    ls -Directory|? {$_ -like 'T*'}|del -r

    cd D:webDevSmallPlatformAPI
    ls |? {$_ -like 'T*'}

    $count = 1
    $filePathList = "D:webDevSmallPlatformAPI"

    foreach($filePath in $filePathList)
    {
    $files = Get-ChildItem -Path $filePath | Sort-Object -Property LastWriteTime -Descending | Select-Object -Skip $count
    if ($files.count -gt 0) {
    foreach($file in $files)
    {
    Remove-Item $file.FullName -Recurse -Force
    }
    }
    }

     
     

    2. 删除目录内所有文件修改时间超过timeOutDay的文件。

     
    $timeOutDay = 30
    $filePath = "H:DataBackupFile1",
    "H:DataBackupDatabase2"
    
    $allFile = Get-ChildItem -Path $filePath
    
    foreach($file in $allFile)
    {
        $daySpan = ((Get-Date) - $file.LastWriteTime).Days
        if ($daySpan -gt $timeOutDay)
        {
            Remove-Item $file.FullName -Recurse -Force
        }
    }
     
  • 相关阅读:
    leetcode5 Longest Palindromic Substring
    leetcode17 Letter Combinations of a Phone Number
    leetcode13 Roman to Integer
    leetcode14 Longest Common Prefix
    leetcode20 Valid Parentheses
    leetcode392 Is Subsequence
    leetcode121 Best Time to Buy and Sell Stock
    leetcode198 House Robber
    leetcode746 Min Cost Climbing Stairs
    tomcat下使用druid配置jnid数据源
  • 原文地址:https://www.cnblogs.com/xinzhyu/p/15392645.html
Copyright © 2011-2022 走看看