项目中经常遇到磁盘空间不足需要定时清理,Linux下面有很多种方法,但是在Windows找了好久都没看到,自己实现了这个功能,本例中需要用到blat发送email工具。以下是具体步骤,
blat下载地址
https://sourceforge.net/projects/blat/
blat 百度网盘地址:
链接:https://pan.baidu.com/s/1z361Hdzoima6NW8G0tijeQ
提取码:nyfh
复制以下代码生成文件名称为hard.vbs
Set fsoobj = CreateObject("Scripting.FileSystemObject")
DriversInfo = GetDriversInfo
DriversInfo = Replace(DriversInfo, "|", vbCrLf)
sReturn = vbCrLf & DriversInfo
Wscript.Echo sReturn
Function GetDriversInfo()GetDriversInfo = ""
Set drvObj = fsoobj.Drives
For Each D In drvObj
Err.Clear
If D.DriveLetter <> "A" Then
If D.isReady The//检查C盘剩余空间,如果需要检查其他磁盘,请修改盘符
If D.DriveLetter = "C" Then
GetDriversInfo = GetDriversInfo & "partition:" & D.DriveLetter & vbCrLf
GetDriversInfo = GetDriversInfo & "Available space:" & cSize(D.FreeSpace) & vbCrLf
GetDriversInfo = GetDriversInfo & "Total:" & cSize( D.TotalSize) & vbCrLf
GetDriversInfo = GetDriversInfo & "Use:" & Int((100*((D.TotalSize-D.FreeSpace)/D.TotalSize))) &" %" & vbCrLf
GetDriversInfo = GetDriversInfo & "|"
End If
Else
End If
Else
End If
Next
End FunctionFunction cSize(tSize)
If tSize >= 1073741824 Then
cSize = Int((tSize / 1073741824) * 1000) / 1000 & " GB"
ElseIf tSize >= 1048576 Then
cSize = Int((tSize / 1048576) * 1000) / 1000 & " MB"
ElseIf tSize >= 1024 Then
cSize = Int((tSize / 1024) * 1000) / 1000 & " KB"
Else
cSize = tSize & "B"
End IfEnd Function
复制以下代码生成文件名为checkdisk.bat
@echo off
::start
cd C:\Users\Lenovo\Desktop\email\blat3222\fullcscript //Nologo hard.vbs >> info.txt
set filePath= info.txt
for /f "delims=" %%i in (%filePath%) do (
set lastLine=%%~i
)
::echo %lastLine%for /F "tokens=1,2 delims=: " %%a in ("%lastLine%") do (
::echo %%a
echo %%b::如果磁盘使用率大于或等于70就发送邮件,可以自己修改
if %%b geq 70 (::邮件发送服务器Host ,邮件地址,邮件密码(发送邮件码)
blat -install smtp.qq.com [email protected] -u [email protected] -pw 12341234blat -body "C harddisk use %%b,please check!!" -to [email protected] -subject "C Harddisk is full "
)
)
::timeout /T 30 /NOBREAK::goto start
::pause
end
hard.vbs和checkdisk.bat都要放到与blat.exe相同的目录下。
做定时检查需要在windows下启动定时任务并执行checkdisk.bat就可以了。