作成 2010.01.21
更新 2010.01.22
更新 2010.01.22
VBScript でユーザー プロファイル フォルダのサイズを取得する
エラーメッセージ
Windows Vista, 7 では Folder オブジェクトの Size プロパティを取得する際、以下のエラーが表示されることがある。
プロファイル フォルダやフォルダ リダイレクトの対象がこれに該当する。
エラー: 書き込みできません。 コード: 800A0046 ソース: Microsoft VBScript 実行時エラー
コード
やり方がわからなかったので力技で。
Option Explicit
Dim FSO, WshShell, objFolder
Dim strProfilePath
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strProfilePath = WshShell.ExpandEnvironmentStrings("%USERPROFILE%")
Set objFolder = FSO.GetFolder(strProfilePath)
WScript.Echo GetFolderSize(objFolder)
Function GetFolderSize(fld)
  Dim size_sum, f1
  On Error Resume Next
  size_sum = fld.Size
  If Err.Number <> 0 Then
    On Error Goto 0
    size_sum = 0
    For Each f1 in fld.SubFolders
      If (f1.Attributes And 1024) = 0 Then
        size_sum = size_sum + GetFolderSize(f1)
      End If
    Next
    For Each f1 in fld.Files
      size_sum = size_sum + f1.Size
    Next
  End If
  On Error Goto 0
  GetFolderSize = size_sum
End Function
タグ: VBScript