Board logo

标题: [原创教程] powershell检测磁盘空间并删除多余文件 [打印本页]

作者: gflrlm    时间: 2018-4-7 19:46     标题: powershell检测磁盘空间并删除多余文件

功能比较简单,直接贴代码,每隔600秒检查D盘空间是否小于2G, 如果是则删除"D:\迅雷下载\.accelerate\新建文件夹"里面的第一个文件【这个文件夹下都是大文件】
  1. $self_filename=$MyInvocation.MyCommand.Name
  2. $host.ui.RawUI.WindowTitle="$self_filename"
  3. $current_dir="."
  4. $Path ="D:\"
  5.   $Filter = '*.*'
  6.   $_Debug = 1
  7.   $DEBUG_LEVEL = 2
  8.   $remove_all_movie_database = 1
  9.   
  10.   Write-Host ""
  11.   Write-Host "请输入要搜索的文件夹路径:例如  D:\test"
  12.   if ($_Debug) {
  13.      $Path = "D:\迅雷下载\..torrents"
  14.   } else {
  15.      $Path = Read-Host
  16.   }
  17.   #if([String]::IsNullOrEmpty($Path))
  18.   
  19.   if(!(Test-Path $Path))
  20.   {
  21.      $Path =".\"
  22.      Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path
  23.   }
  24.   Write-Host ""
  25.   Write-Host ""
  26.   Write-Host "Path   = " $Path
  27. $limit = 2*1024*1024*1024
  28. #Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3 and Freespace<$limit" | Select-Object -Property VolumeName, Freespace, DeviceID
  29. $remove_file="D:\迅雷下载\.accelerate\新建文件夹"
  30. Write-Host "--+++++++++++++++++++"
  31. while(1) {
  32.      Write-Host "-----------------"
  33.    $fileList = Get-ChildItem -Path $remove_file   -Recurse |sort | ?{$_.PsIsContainer -eq $false}| %{$_.FullName}
  34.    $first_file=$fileList[0]
  35.    $disk_d=Get-WmiObject -Class Win32_LogicalDisk  -Filter  "VolumeName='Data'"
  36.    if($disk_d.Freespace -gt $limit) {
  37.       write-host "D: has enough freespace, greater than  $limit"
  38.    }else {
  39.        write-host "D: has not enough freespace, less than  $limit, now delete file: $first_file from  folder 【 $remove_file 】 ."
  40.      rm "$first_file"
  41.    }
  42.    start-sleep -seconds 600 #每隔600秒检查一次
  43. }
  44.   cmd /c "pause"
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2