2016-05-01 67 views
2

我正在創建一個腳本,每當磁盤空間達到閾值時都會發送郵件。我發現一個簡單的工具來獲取磁盤驅動器的空間使用情況。但是我想過濾輸出併爲驅動器設置一個超過設定閾值的條件。獲取變量後跟百分號符號批處理腳本的輸出

使用psinfo.exe工具,我能夠獲得磁盤使用率輸出。此外,我只想在下面的輸出中獲取後面跟有百分號(%)的數字,並將空閒空間的條件設置爲超出閾值。

 
Volume Type  Format  Label      Size  Free Free 
    C: Fixed  NTFS  OS     181.62 GB 91.69 GB 50.5% 
    D: Fixed  NTFS  New Volume   273.90 GB 183.22 GB 66.9% 

請您幫我完成腳本。

+1

編輯您的問題,並張貼您嘗試作爲代碼幹了什麼到現在爲止! – Hackoo

回答

0

如果你的磁盤標籤不包含空格或點你可以試試這個:

@echo off 

set "threshold=50" 

for /f "skip=1 tokens=1,11,12 delims=:.%% " %%a in ('psinfo') do (
    echo -%%a-%%b-%%c- 
    if %%b geq %threshold% (
     echo disk %%a exceeds the threshold %threshold% 
     echo free disk space is %%b.%%c%% 
     echo some other code here 
    ) 

) 
+0

感謝您的快速支持@npocmaka ..但你可以在我的輸出中看到它有一個空間的標籤:(...任何其他解決方案? – Hitesh

0
@echo off 
setlocal EnableDelayedExpansion 

set "treshold=50" 

for /F "skip=1 delims=" %%a in ('psinfo') do (
    set "disk=" 
    for %%b in (%%a) do (
     if not defined disk set "disk=%%b" 
     set "percent=%%b" 
    ) 
    set /A "num=percent,num=num*10+!percent:~-2,1!" 
    if !num! gtr %treshold%0 (
     echo Disk !disk! exceed %treshold%%%: !percent! 
    ) 
) 
相關問題