WMI File Size

hoshie329
hoshie329 used Ask the Experts™
on
I am working on a script that utilizes WMI to get the size of a particular file.

  So far, it doesn't appear to be even looking in the directory -- I receive 0 files found

can any one tell why this isn't returning any file count.

after it gets the files I need it to look return the file size of the file I specify
Computer = "."
Drive = "c:"
Path = "\\Documents and Settings\\someone\\My Documents"
strFile = "test.txt" 
 'Normalize the Path variable for use in the query
if right(Path,1) <> "\" then
       Path = Path & "\"
End If
Path = replace(Path,"\","\\")

 'For testing:
'wscript.echo Computer & " " & Drive & " " & Path

 'Execute the WMI query to get the file information
Set WMIService = GetObject("winmgmts:\\" & Computer & "\root\cimv2")
WMIQuery = "Select FileSize from CIM_DataFile where drive = '" & Drive & "' and Path = '" & Path & "'"
Set Results = WMIService.ExecQuery(WMIQu<wbr ></wbr>ery)

 'For testing:
wscript.echo "Found " & Results.Count & " files"

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
ok... No sooner than I posted this I found the issue with reporting 0 files found

Now if anyone can guide me to getting the size of the single file i am looking for

8 files were found, one of the 8 is the file i need the size of,...I don't want any information on the remaining 7 files

Author

Commented:
OK, I have modified the code a little,

I am now receiving the file count correctly, I am also getting the file Size - I am having problems getting the file name.

Am I go about this in the wrong direction? I have tried to pass the file name as part of the variable, but I get an error when I do this also.

Is possible to pass the file name as a variable and only collect the needed information on this singe file...I would prefer this method

Thanks
strComputer = "."
Drive = "c:"
Path = "\Documents and Settings\jburford\My Documents"
strFile = "test.txt" 
 'Normalize the Path variable for use in the query
if right(Path,1) <> "\" then
       Path = Path & "\"
End If
Path = replace(Path,"\","\\")

 'For testing:
wscript.echo Computer & " " & Drive & " " & Path

 'Execute the WMI query to get the file information
Set WMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
WMIQuery = "Select FileSize from CIM_DataFile where drive = '" & Drive & "' and Path = '" & Path & "'"
Set Results = WMIService.ExecQuery(WMIQuery)
    

 'For testing:
wscript.echo "Found " & Results.Count & " files"

For Each FileFound In Results
    wscript.echo FileFound.FileName & " " & FileFound.FileSize

Next

Open in new window

Author

Commented:
OK, now the script is only looking for the size of the file I need

I am receiving an error Line    29
                                     Char   1
                                     Error   Object Required: "
Dim Computer, Drive, Path, strFile 'Arguments
Dim WMIService, WMIQuery  'WMI stuff
Dim Results, FileFound

strComputer = "."
Drive = "c:"
Path = "\Documents and Settings\jburford\My Documents\"
strFile = "running-config.txt" 
 'Normalize the Path variable for use in the query
if right(Path,1) <> "\" then
       Path = Path & "\"
End If
Path = replace(Path,"\","\\")

 'For testing:
wscript.echo Computer & " " & Drive & " " & Path 

 'Execute the WMI query to get the file information
Set WMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
WMIQuery = "Select FileSize from CIM_DataFile where drive = '" & Drive & "' and Path = '" & Path & "' and File = '" & strFile & "'"
Set Results = WMIService.ExecQuery(WMIQuery)

'For Each FileFound In Results
wscript.echo FileFound.FileSize

Open in new window

Author

Commented:
Sorry, I also cleaned up the script the error would be on line 24
changes needed:

line 20, change your wmi query - use filename instead of file.

WMIQuery = "Select FileSize from CIM_DataFile where drive = '" & Drive & "' and Path = '" & Path & "' and FileName = '" & strFile & "'"

You've commented out line 23, remove the leading apostrophe:

For Each FileFound In Results

Add a line 25

Next
Dim Computer, Drive, Path, strFile 'Arguments
Dim WMIService, WMIQuery  'WMI stuff
Dim Results, FileFound

strComputer = "."
Drive = "c:"
Path = "\Documents and Settings\jburford\My Documents\"
strFile = "running-config.txt" 
 'Normalize the Path variable for use in the query
if right(Path,1) <> "\" then
       Path = Path & "\"
End If
Path = replace(Path,"\","\\")

 'For testing:
wscript.echo Computer & " " & Drive & " " & Path 

 'Execute the WMI query to get the file information
Set WMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
WMIQuery = "Select FileSize from CIM_DataFile where drive = '" & Drive & "' and Path = '" & Path & "' and FileName = '" & strFile & "'"
Set Results = WMIService.ExecQuery(WMIQuery)

For Each FileFound In Results
  wscript.echo FileFound.FileSize
Next

Open in new window

Author

Commented:
With the above changes I do not get any errors, But I never see the file size displayed to the screen either

Thanks

Author

Commented:
ok,


  I now have a much simpler code that does return the file size, I have tried to configure it to round the return value to 2 decimal places but receive the following error,

Line   7
Char  3
Error  Object doesn't support this property or method

Is there any way to round the return value to 2 places past the decimal?
shost="."
filespec="C:\Documents And Settings\jburford\My Documents\roxiocreator2010sp2.exe"

set svc=getobject("winmgmts:\\" & shost & "\root\cimv2")
Set ofile=svc.get("cim_datafile.name='" & filespec & "'")
  ofile2 = ofile.filesize / 1048576
  ofile2 = Round(ofile,2)
wscript.echo ofile.name & vbcrlf & ofile2 & " MB"
Set ofile=Nothing : Set ofile2=Nothing :set svc=Nothing

'Conversion to Megabytes
'1/1024 = KB
'1/1024^2 = MB <== Currently in Use
'1/1024^3 = GB

Open in new window

you have a syntax error on line 7.  Change it to the following:

  ofile2 = Round(ofile2,2)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial