dos命令实现磁盘可用空间监控的脚本

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 Then

               '检查C盘剩余空间,如果需要检查其他磁盘,请修改盘符
               If D.DriveLetter = "D" Then
               GetDriversInfo = GetDriversInfo & "partition:" & D.DriveLetter & vbCrLf
               GetDriversInfo = GetDriversInfo & "Total:" & cSize( D.TotalSize) & vbCrLf
               GetDriversInfo = GetDriversInfo & "Use:" & Int((100*((D.TotalSize-D.FreeSpace)/D.TotalSize))) &" %" & vbCrLf
			   GetDriversInfo = GetDriversInfo & "Available space:" & cSize(D.FreeSpace) & vbCrLf
               GetDriversInfo = GetDriversInfo & "|"
               End If
             Else
           End If
         Else
       End If
   Next
End Function

Function 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 If
End Function

checkdisk.bat

@echo off

set _info=info.txt
set _interval=5000
set _threshold=5547.9
:checkstart

del %_info%

cscript //Nologo hard.vbs >> %_info%

set filePath= %_info%
for /f "delims=" %%i in (%filePath%) do (
    set lastLine=%%~i
)
echo %lastLine%

for /F "tokens=1,2,3 delims=: " %%a in ("%lastLine%") do (
   ::echo %%a
   echo %%c

  rem 如果磁盘空间大于或等于70GB,进入休眠;否则进入处理程序
   if %%c geq %_threshold% (
   echo greater than
   goto checkag
   )   else (
   echo less than
   goto doSomething
   )
)

:doSomething
echo dosomthong

:checkag
echo %time% program %_task% run normally, next check will be lauched in %_interval% milliseconds ... 
echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs 
cscript //b //nologo %tmp%\delay.vbs %_interval% 
goto checkstart

另一种实现延时的实现

@echo off

set _info=info.txt
set _interval=5000
set _threshold=5547.9
set _delaytime=5

:checkstart

del %_info%

cscript //Nologo hard.vbs >> %_info%

set filePath= %_info%
for /f "delims=" %%i in (%filePath%) do (
    set lastLine=%%~i
)
echo %lastLine%

for /F "tokens=1,2,3 delims=: " %%a in ("%lastLine%") do (
   ::echo %%a
   echo %%c

  rem 如果磁盘空间大于或等于70GB,进入休眠;否则进入处理程序
   if %%c geq %_threshold% (
   echo greater than
   goto checkag
   )   else (
   echo less than
   goto doSomething
   )
)

:doSomething
echo dosomthong

:checkag
echo %time% program %_task% run normally, next check will be lauched in %_interval% milliseconds ... 
rem echo Wscript.Sleep WScript.Arguments(0) >%tmp%\delay.vbs 
rem cscript //b //nologo %tmp%\delay.vbs %_interval%
timeout /t %_delaytime% /NOBREAK 
goto checkstart

参考

  1. Windows脚本检查磁盘剩余空间并发送Email

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值