Solved

VBscript Help

Posted on 2012-04-13
9
587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 54

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 54

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 54

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 management. 5 57
Assigning VBA code to a specific worksheet 3 45
powershell script 9 82
Creating An Intelligent Dropdown 8 59
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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