Link to home
Start Free TrialLog in
Avatar of hoshie329
hoshie329Flag for Afghanistan

asked on

WMI File Size

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

Avatar of hoshie329
hoshie329
Flag of Afghanistan image

ASKER

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

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

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

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

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

Thanks
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

ASKER CERTIFIED SOLUTION
Avatar of rscottvan
rscottvan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial