Solved

VBscript Help

Posted on 2012-04-13
9
580 Views
Last Modified: 2012-04-21
I have a script that is searching for extentions on a drive.  If that extention is found it writes the information to a file.  It works great except when I try to add the obj.datelastmodified to the output file.  when I add that object the file returns empty.  If I take that out then I get the results.


How can I get my output to show the last modified date?

'  SCAN FOR FILES WITH EXTENSION
strInput = "mdb"
strInput1 = "wp"
strInput2 = "wp4"
strInput3 = "wp5"
strInput4 = "wp6"
strInput5 = "wp7"
strInput6 = "wb2"
strInput7 = "wpd"

Set colComputers = GetObject("LDAP://ou=SCAN,ou=Servers,DC=domain,DC=local")
For Each objComputer In colComputers
strComputer = objComputer.CN
on error Resume next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile Where (Extension = '" & strInput &"' OR Extension = '" & strInput1 &"' OR Extension = '" & strInput2 &"' OR Extension = '" & strInput3 &"' OR Extension = '" & strInput4 &"' OR Extension = '" & strInput5 &"' OR Extension = '" & strInput6 &"' OR Extension = '" & strInput7 &"') AND (Drive = 'F:') ")
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\SCANS\New.csv",2,true)
For Each objFile in colFiles 
Wfile.writeline(strComputer & "," & objFile.Drive & "\" & objFile.Path & "\" & objFile.FileName & "." & objFile.Extension & "," & objFile.FileSize & "," & objFile.DateLastModified)
next
next

Open in new window

0
Comment
Question by:rschraeger
9 Comments
 
LVL 9

Expert Comment

by:jsdray
ID: 37843430
Just out of curiosity...try setting a variable for your output...

myString = strComputer & "," & objFile.Drive & "\" & objFile.Path & "\" & objFile.FileName & "." & objFile.Extension & "," & objFile.FileSize & "," & objFile.DateLastModified

then write it out...

Wfile.writeline(myString)
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 37843440
Is it a typo, or did you really use
  obj.datelastmodified
instead of
  objFile.datelastmodified
?
0
 

Author Comment

by:rschraeger
ID: 37843758
It is objfile.datelastmodified
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 37843930
Try writing the DateLastModified value to a variable and examining it in a MsgBox.  You might need to check it for validity before writing it to a text file.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37844190
Remove the following line, and run again, seeing if you get any errors.
on error Resume next

Open in new window

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37844250
You need to change

objFile.DateLastModified

to

objFile.LastModified

The WMI execution isn't returning a FileSystem file object, but rather a CIM_Datafile class member.

That being said, what you will see in the CSV file will likely be in a format like this for the date:

20120120112210.687139-300

Which may or not be what you want.

~bp
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37844300
0
 
LVL 9

Accepted Solution

by:
Scottyworld earned 500 total points
ID: 37858880
As Bill said, you need to use objFile.LastModified and pipe the result through a function that converts the date to a readable format
The below script should do all of that for you
' SCAN FOR FILES WITH EXTENSION
strInput = "mdb"
strInput1 = "wp"
strInput2 = "wp4"
strInput3 = "wp5"
strInput4 = "wp6"
strInput5 = "wp7"
strInput6 = "wb2"
strInput7 = "wpd"

Set colComputers = GetObject("LDAP://ou=SCAN,ou=Servers,DC=domain,DC=local")
For Each objComputer In colComputers
	strComputer = objComputer.CN
	on error Resume next
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colFiles = objWMIService.ExecQuery _
	("Select * from CIM_DataFile Where (Extension = '" & strInput &"' OR Extension = '" & strInput1 &"' OR Extension = '" & strInput2 &"' OR Extension = '" & strInput3 &"' OR Extension = '" & strInput4 &"' OR Extension = '" & strInput5 &"' OR Extension = '" & strInput6 &"' OR Extension = '" & strInput7 &"') AND (Drive = 'F:') ")
	Set fso = CreateObject("Scripting.FileSystemObject")
	set wfile = fso.opentextfile("c:\SCANS\New.csv",2,true)

	For Each objFile in colFiles
		strLastMod = DatetimeToDate(objFile.LastModified)
		Wfile.writeline(strComputer & "," & objFile.Drive & "\" & objFile.Path & "\" & objFile.FileName & "." & objFile.Extension & "," & objFile.FileSize & "," & strLastMod)
	next
next

Function DatetimeToDate(strDate)
   DatetimeToDate = CDate(Mid(strDate, 5, 2) & "/" & Mid(strDate, 7, 2) & "/" & Left(strDate, 4) & " " & Mid (strDate, 9, 2) & ":" & Mid(strDate, 11, 2) & ":" & Mid(strDate, 13, 2))
End Function

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question