[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创教程] 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"
复制代码

返回列表