返回列表 发帖

[原创教程] powershell检测磁盘空间并删除多余文件

功能比较简单,直接贴代码,每隔600秒检查D盘空间是否小于2G, 如果是则删除"D:\迅雷下载\.accelerate\新建文件夹"里面的第一个文件【这个文件夹下都是大文件】
$self_filename=$MyInvocation.MyCommand.Name
$host.ui.RawUI.WindowTitle="$self_filename"
$current_dir="."
$Path ="D:\"
  $Filter = '*.*'
  $_Debug = 1
  $DEBUG_LEVEL = 2
  $remove_all_movie_database = 1
  
  Write-Host ""
  Write-Host "请输入要搜索的文件夹路径:例如  D:\test"
  if ($_Debug) {
     $Path = "D:\迅雷下载\..torrents"
  } else {
     $Path = Read-Host
  }
  #if([String]::IsNullOrEmpty($Path))
  
  if(!(Test-Path $Path))
  {
     $Path =".\"
     Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path
  }
  Write-Host ""
  Write-Host ""
  Write-Host "Path   = " $Path
$limit = 2*1024*1024*1024
#Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3 and Freespace<$limit" | Select-Object -Property VolumeName, Freespace, DeviceID
$remove_file="D:\迅雷下载\.accelerate\新建文件夹"
Write-Host "--+++++++++++++++++++"
while(1) {
     Write-Host "-----------------"
   $fileList = Get-ChildItem -Path $remove_file   -Recurse |sort | ?{$_.PsIsContainer -eq $false}| %{$_.FullName}
   $first_file=$fileList[0]
   $disk_d=Get-WmiObject -Class Win32_LogicalDisk  -Filter  "VolumeName='Data'"
   if($disk_d.Freespace -gt $limit) {
      write-host "D: has enough freespace, greater than  $limit"
   }else {
       write-host "D: has not enough freespace, less than  $limit, now delete file: $first_file from  folder 【 $remove_file 】 ."
     rm "$first_file"
   }
   start-sleep -seconds 600 #每隔600秒检查一次
}
  cmd /c "pause" COPY

返回列表