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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Properties with vbscript 1

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
529
US
OK, i am trying to get this piece to work, i am enumerating the file properties within a directory and all the child directories of that directory. I am having problems with this one piece. I have simplified my code down to work on this piece and I can't seem to get it to work properly. I will get the "Done" echo but not the rest and if i don't put it in the For Each statement it does give me an error about object does not support this property. When i google this it shows the exact syntax I am using. I am confused at this point.
Code:
dim colFiles, strPath, fs, f, strString
dim objFile

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

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where name = 'c:\\<path>\\<filename>'")



For each objFile in colFiles
		
	Wscript.Echo "File name: " & objFile.FileName
	Wscript.Echo "File name: " & objFile.FileName
	Wscript.Echo "Path: " & objFile.Path
Next		


	Wscript.Echo "Done"

Also one note, i did try the
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(objFile)
Wscript.Echo f.Path
method but that didn't work, it only works if i put the actual path in. I did try the objFile.Path as well and that didn't work either.
 
Ok, I have figured something out the code above does work,
Code:
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where name = 'c:\\<path>\\<filename>'")

I was using
Code:
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where path = 'c:\\<path>\\'")
for my file enumeration that is the piece that isn't working properly



 
Will you be using this script locally or will you be running it against remote machines?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If you intend to run it locally I would use FileSystemObject and a recursive function/sub...i.e.

Code:
Option Explicit

' create FSO
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
RecurseFolder "d:\scripts"

Sub RecurseFolder(strFolderPath)
 ' get folder
 Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath)

 ' On Error Resume Next
 Dim objFile
 For Each objFile In objFolder.Files
         WScript.Echo objFile.Name
         WScript.Echo objFile.Path
 Next
 ' On Error Goto 0
 
 ' On Error Resume Next
 Dim objSubFolder 
 ' loop through sub folders
 For Each objSubFolder In objFolder.SubFolders
'         WScript.Echo objSubFolder.Name
         ' have the sub call itself sending the sub folder path
         RecurseFolder objSubFolder.Path
 Next
 ' On Error Goto 0
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Well that sounds good, I wasn't exactly sure how to do it with the file system object and make it recursive. Can you explain how this will recurse through all of the subfolders and the subfolders of the subfolders and so on.
 
Nevermind I see how, Thank you this will work great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top