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

    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
            }
        }    
    }

    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
        }
    }
  • 相关阅读:
    CSS hack
    字符串中常用的方法
    排序算法
    拾遗
    数组类型检测
    数组常用的方法
    go 文件服务器(标准库) 添加关机,睡眠,退出功能
    go cmd 交互 初始化执行某些命令
    go 内网IP及外网IP获取
    go 快排实现
  • 原文地址:https://www.cnblogs.com/xiongpq/p/3543229.html
Copyright © 2011-2022 走看看