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

VBScript getFile information and output

Status
Not open for further replies.

Akkarin

IS-IT--Management
Mar 3, 2011
3
GB
Hey guys, im very new to VBS, this is my first attempt at it.

I am trying to write a script that will;

1) Search a Folder/Subfolders for files that have a LastModified date equal to todays date
2) Return the FileName, Extention, LastModified, User who modified
3) Output eat file found to a registry key

I have managed to create a script to do 1 & most of 2 but i dont understand how i would get around to scripting part 3.

here is what i have so far



On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

arrComputers = Array("EMEAOXFLP130")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
WScript.Echo"Date: " & Date

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE Drive='C:' AND Path like '%CRS%' AND Extension = 'txt'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
IF WMIDateStringToDate(objItem.LastModified) = Date then
WScript.Echo "FileName: " & objItem.FileName
WScript.Echo "Extension: " & objItem.Extension
WScript.Echo "LastModified: " & WMIDateStringToDate(objItem.LastModified)
WScript.Echo "Name: " & objItem.Name
WScript.Echo "========================================"
Else
End If
Next
Next

Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 7, 2) & "/" & Mid(dtmDate, 5, 2) & "/" & Left(dtmDate, 4))
End Function




If any of you guys could help me i would be forever in your debt..

Thanks in advance
 
What do you want you regisyry key to conatin? The file name? some of the file's content? In which hive do you want to store it?

VBS Reference

Registry:

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
i want the key to contain the filename and datemodified. and i want it to be put into a custom registry key located in the HKEY_LOCAL_MACHINE\Software.

does that make sense?
 
to use the ActiveScripts registry example

Code:
Const HKEY_LOCAL_MACHINE = &H80000002

'Pass the file object for easy file property reference.
sub writeToReg (objFile)
   strComputer = "."
   set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
   strKeyPath = "SOFTWARE\AkkarinsFiles"
   strValueName = objFile.Path
   strValue = objFile.DateLastModified
  
   oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
end sub

'Usage
for each objItem in colItems
   ...
   writeToReg (objItem)
next

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are selmon a good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Thank you very much for that. But i would have no idead where or how i can intergrate that into my current script!? or if it is even possible!

Is it just a case of pasting that script into the THEN part of my IF statment and making the necessary amendments!?
 
certainly is:

Code:
On Error Resume Next

' ***  VARIABLE DEFINITION  ***

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
[red]Const HKEY_LOCAL_MACHINE = &H80000002[/red]

arrComputers = Array("EMEAOXFLP130")

' ***  FUNCTIONS  ***

Function WMIDateStringToDate(dtmDate)
   WScript.Echo dtm
   WMIDateStringToDate = CDate(Mid(dtmDate, 7, 2) & "/" & Mid(dtmDate, 5, 2) & "/" & Left(dtmDate, 4))
End Function 

[red]
sub writeToReg (objFile)
   strComputer = "."
   set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
   strKeyPath = "SOFTWARE\AkkarinsFiles"
   strValueName = objFile.Path
   strValue = objFile.DateLastModified
   oReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
end sub
[/red]

' ***  MAIN  ***

For Each strComputer In arrComputers
   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Computer: " & strComputer
   WScript.Echo "=========================================="
   WScript.Echo"Date: " & Date

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE Drive='C:' AND Path like '%CRS%' AND Extension = 'txt'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
      IF WMIDateStringToDate(objItem.LastModified) = Date then
         [red]writeToReg (objItem)[/red]
         WScript.Echo "FileName: " & objItem.FileName
         WScript.Echo "Extension: " & objItem.Extension
         WScript.Echo "LastModified: " & WMIDateStringToDate(objItem.LastModified)
         WScript.Echo "Name: " & objItem.Name
         WScript.Echo "========================================"
      Else
      End If
   Next
Next

Paste the function into you code and call it when you want to add a reg key.

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

"There are selmon a good technological solutions to behavioral problems."
- Ed Crowley, Exchange guru and technology curmudgeon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top