作成 2010.01.08
更新 2011.10.24
VBScript でディスクの空き容量を取得する2
コード
Option Explicit

Dim wmiLocator
Dim wmiService
Dim objEnumerator
Dim objInstance
Dim strDisks()
Dim strDivide(5)
Dim intI
Dim intJ
Dim intK
Dim tmpSize

Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
Set wmiService = wmiLocator.ConnectServer
Set objEnumerator = wmiService.ExecQuery("Select * From Win32_LogicalDisk")

ReDim Preserve strDisks(5, 1)
strDisks(0, 0) = "Letter"
strDisks(1, 0) = "Full(Byte)"
strDisks(2, 0) = "Free(Byte)"
strDisks(3, 0) = "FileSystem"
strDisks(4, 0) = "Description"

intI = 1
For Each objInstance In objEnumerator
  ReDim Preserve strDisks(5, intI + 1)
  strDisks(0, intI) = objInstance.Name
  strDisks(1, intI) = objInstance.Size
  strDisks(2, intI) = objInstance.FreeSpace
  strDisks(3, intI) = objInstance.FileSystem
  strDisks(4, intI) = objInstance.Description
  intI = intI + 1
Next

Set objEnumerator = wmiService.ExecQuery("Select * From Win32_Volume")
For Each objInstance In objEnumerator
  If IsNull(objInstance.DriveLetter) Then
    ReDim Preserve strDisks(5, intI + 1)
    strDisks(0, intI) = objInstance.Name
    strDisks(1, intI) = objInstance.Capacity
    strDisks(2, intI) = objInstance.FreeSpace
    strDisks(3, intI) = objInstance.FileSystem
    strDisks(4, intI) = objInstance.Label
    intI = intI + 1
  End If
Next

' 位置調整
For intK = 0 To 4
  tmpSize = 0
  For intJ = 0 To intI - 1
    If tmpSize < Len(strDisks(intK, intJ)) Then tmpSize = Len(strDisks(intK, intJ))
  Next
  If intK = 1 Or intK = 2 Then
    strDisks(intK, 0) = Left(strDisks(intK, 0) & "                                ", tmpSize + 1)
    For intJ = 1 To intI - 1
      strDisks(intK, intJ) = Right("                                " & strDisks(intK, intJ) & " ", tmpSize + 1)
    Next
  Else
    For intJ = 0 To intI - 1
      strDisks(intK, intJ) = Left(strDisks(intK, intJ) & "                                ", tmpSize + 1)
    Next
  End If
  strDivide(intK) = Left("-----------------------------------------", tmpSize) & " "
Next

' 表示
WScript.Echo strDisks(0, 0) & strDisks(1, 0) & strDisks(2, 0) & strDisks(3, 0) & strDisks(4, 0)
WScript.Echo strDivide(0) & strDivide(1) & strDivide(2) & strDivide(3) & strDivide(4)
For intJ = 1 To intI - 1
  WScript.Echo strDisks(0, intJ) & strDisks(1, intJ) & strDisks(2, intJ) & strDisks(3, intJ) & strDisks(4, intJ)
Next
実行結果
df コマンド風に取得するつもりがうまく行かなかった。
C:\>cscript ディスクの空き容量を取得する2.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Letter                                Full(Byte)    Free(Byte)    FileSystem Description
----------------------------------------- ------------- ------------- ---------- -----------
C:                                 119926681600   82213801984 NTFS       ローカル固定ディスク
D:                                                                       CD-ROM ディスク
X:                                8795958759424 8210852638720 NTFS       ローカル固定ディスク
\\?\Volume{2ffd7e43-bfdb-11e0-8cc8-806e6f6e6963}\     104853504      73773056 NTFS       システムで予約済み

C:\>
タグ: VBScript WMI

©2004-2017 UPKEN IPv4