Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to add last modified and size of file

Status
Not open for further replies.

thutmose

MIS
Mar 31, 2002
24
US
I have this script that for some reason everytime I try to add the size of the file the output comes out at the end of the file or the name of the variable displays rather than the size of the file. Can someone help here is my script. Out of frustration I removed the object.filesize out because it is not working correctly. I would also like to know how to add MB at the end of the number and if it rolls over to a gigabyte size how to tell it to add GB at the end. ANy help would be appreciated, thanks.


strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile where Extension = 'pdf'")
dim filePath
For Each objFile in colFiles
filePath = objFile.Drive & objFile.Path & objFile.FileName & "." & objFile.Extension
Wscript.Echo "FilePath: " & filePath
GetFileOwner objFile.Drive & objFile.Path & objFile.FileName & "." & objFile.Extension

Wscript.Echo
Next

sub GetFileOwner(strFile)
dim owners
Set colItems = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile & "'}" _
& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
owners = owners & objItem.AccountName & ";"

Wscript.Echo "FileOwner: " & owners
Next
end sub
 
Here is a quick example that will list the file size.
If you happen to have (or anticipate) files larger than Terabyte size, you will have to add options to the select case.

Code:
set fso = CreateObject("Scripting.FileSystemObject")
Set objTargetFolder = fso.GetFolder("C:\temp")
For Each objFile In objTargetFolder.Files
	dblSize = objFile.size
	i = 0
	Do Until dblSize < 1000
		i = i + 1
		dblSize = dblSize / 1024
	Loop

	Select Case i
		Case 0
			sizeSuffix = "bytes"
		Case 1
			sizeSuffix = "kB"
		Case 2
			sizeSuffix = "MB"
		Case 3
			sizeSuffix = "GB"
		Case 4
			sizeSuffix = "TB"
		Case Else
			sizeSuffix = "unknown"
	End Select
	msgbox(objFile.name & " " & dblSize & " " & sizeSuffix)
next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top