沒事写写VBS和BAT,做个HD使用统计

 

'Win32 series Harddisk Usage Checker
'
Added by m@QiNtOsHi 2010/4/

'Information:
'
Get the HD info and HD usage from win32 api winmgmts
'
you can change the Const Variable of DRV_LETTER or others
'
for specify your purpose

On Error Resume Next
starttime
= Time
Wscript.Echo
"Processing,don't close this window,please wait."

Const adVarChar = 200
Const MaxCharacters = 255
Const adDouble = 12

Const HARD_DISK = 3 磁碟類型
Const DRV_LETTER = "D:" 磁碟標識

Set DataList = CreateObject("ADODB.Recordset") 使用下adodb.recordest
DataList.Fields.Append
"SizeWithSubFolders", adDouble 整個數據List
DataList.Fields.Append
"FriendlySizeWithSubFolders", adVarChar, MaxCharacters
DataList.Fields.Append
"SizeWithoutSubFolders", adDouble
DataList.Fields.Append
"FriendlySizeWithoutSubFolders", adVarChar, MaxCharacters
DataList.Fields.Append
"Path", adVarChar, MaxCharacters
DataList.Open

dateStr
= Right("0000" & Year(Now),4) & Right("00" & Month(Now),2) & Right("00" & Day(Now),2)
timeStr
= Hour( Now ) & Minute( Now )
獲取執行腳本的日期和時間

開始創建FS了
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fs_op = objFSO.OpenTextFile("d:\你的目錄\DSKer_" & dateStr & "_" & timeStr & ".txt", 8, True, -1)

str1stLine
= "Drive" & vbTab & "Size" & vbTab & "Used" & vbTab & "Avail" & vbTab & "Capacity" & vbNewLine

fs_op.Write str1stLine

######你也可以啓動remote模式########
strComputer
= "." dot代表localhost
'strComputer = "xxx.xxx.xxx.xxx"
'
strUsr="Administrator"
'
strPasswd="PASSWD"
LogicalDisks = 0

'If strComputer = "." Then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
本地?
'Else
還是remote?
'Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
'Set objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\CIMV2", strUsr, strPasswd)
'
End If

Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = "& HARD_DISK & "")

For Each objItem in colItems
開算!
LogicalDisks
= LogicalDisks + 1
fs_op.WriteLine objItem.DeviceID
& vbTab & FormatSize(objItem.Size) & vbTab & _
FormatSize(objItem.Size
- objItem.FreeSpace) & vbTab & _
FormatSize(objItem.FreeSpace)
& vbTab & _
round((objItem.Size - objItem.FreeSpace)*100/objItem.Size,2) & "%" & ""
Next

基本的FS操作再熟悉不過了,不多說了
fs_op.WriteLine vbcrlf
fs_op.WriteLine
"Size" & vbTab & vbTab & "Dir/File Path"
fs_op.WriteLine
"###############################################"
RecurseFolderList(DRV_LETTER
& "\")

'MsgBox "Finished.Starttime:" & vbTab & starttime & vbTab & "Endtime:" & vbTab & Time


稍微介紹下遍歷好了
'****************************************
Function RecurseFolderList(FolderName)

On Error Resume Next
Dim f, fc, fj, f1

If Err.Number > 0 Then
RecurseFolderList
= False
Exit Function
End If

On Error GoTo 0
主要靠FolderExists去遍歷,有下層就下層找,沒有就當前找完返回上級
If objFSO.FolderExists(FolderName) Then
Set f = objFSO.GetFolder(FolderName)
Set fc = f.Subfolders
Set fj = f.Files
'For each subfolder in the Folder
For Each f1 In fc
'Do something with the Folder Name
'WScript.Echo f1.attributes & ":" & f1
fs_op.Write vbcrlf
if ( f1.attributes <> 22 And f1.attributes <> 34 ) Then
fs_op.WriteLine
"****** DIR: ******" & vbtab & f1
'Then recurse this function with the sub-folder to get any
'sub-folders
RecurseFolderList (f1)
End if

Next

'For each folder check for any files
For Each f1 In fj
'WScript.Echo f1.attributes & ":" & f1
if ( f1.attributes <> 22 And f1.attributes <> 34 ) Then
fs_op.WriteLine FormatSize(f1.Size)
& vbtab & vbtab & f1
End if
Next

Set f = Nothing
Set fc = Nothing
Set fj = Nothing
Set f1 = Nothing

Else
RecurseFolderList
= False
End If

Set fso = Nothing

End Function
'***************************************


byte,KB,MB,GB,TB的Human readable表示
'***************************************
Function FormatSize(ByteSize)
If ByteSize > 1E12 Then
FormatSize
= FormatNumber(ByteSize/1024/1024/1024/1024,2) & "TB"
ElseIf ByteSize > 1E9 Then
FormatSize
= FormatNumber(ByteSize/1024/1024/1024,2) & "GB"
ElseIf ByteSize > 1E6 Then
FormatSize
= FormatNumber(ByteSize/1024/1024,2) & "MB"
Else
FormatSize
= FormatNumber(ByteSize/1024,2) & "KB"
End If
End Function
'***************************************

-----------------和上面的保存在一個目錄下------------
Gethdvol.bat
-------------------------------------------------
在搞它個檢查完畢自動打開文件的bat:
:begin
@wscript hdinfo.vbs

@Rem Set The Parameter "curr_date"  As The Dos Date Type Like "yyyymmdd",
@Rem  For Appending To Backup Sql File's Suffix
@for /f "tokens=1-4 delims=:/" %%i in ("%date:~0,15%")  do @set curr_date=%%i%%j%%k%%l
@Rem Set The Parameter "curr_time"  As The Dos time Type Like "hhmmss",
@set time2=%time: =0%
@set curr_time=%time2:~0,2%%time2:~3,2%

@explorer.exe D:\你的目錄\DiskSpace_%curr_date%_%curr_time%.txt
pause

 

 

posted @ 2010-04-26 14:45  荖K  阅读(254)  评论(0编辑  收藏  举报