Solved

VBscript Help

Posted on 2012-04-13
9
576 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 68

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

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 51

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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now