Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VB Script to compare with a reg key

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,56712
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VB Script to compare with a reg key
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2 3 4   next >   >>
Login
Message << Older Topic   Newer Topic >>
 VB Script to compare with a reg key - 2/15/2008 1:19:20 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
Hello,

I have a script that works but needs to be modified.  The modification needs to happen where it checks for the date stamp.  I will try to put the logic together for you. 

If a file gets updated then copy it over to the usb key.  Also place a datestamp file in a reg key.  The next time the script runs it needs to check that reg key for the date stamp if updated and copy the new files over.

If this doesn't make any sence, please let me know and I will try to clarify it for you.

Code below: 


Option Explicit
'Dim objWMIService, objItem, colItems
Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\")

strMbox = "."

strMbox = msgBox("Please enter your usb key and press the enter key to continue")

If strMbox = 1 Then

   strmbox ="."

end if

strComputer = "."


arrDriveType = array("Unknown",_
           "No Root Directory",_
           "Removable Disk",_
           "Local Disk",_
           "Network Drive",_
           "Compact Disk",_
           "RAM Disk")

set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")

For Each Disk in ColDisks

     strType = arrDriveType(Disk.DriveType)
     strID = Disk.DeviceID
     strSys = Disk.SystemName

     If strType = "Removable Disk" then

           'strMbox = MsgBox("USB drive letter is " & Disk.DeviceID)
           UsbKey = strId & "\"
           USBFound = True
     End If

Next

If USBFound = False Then
     MsgBox "No USB drive was found."
     WScript.Quit
End If

For Each file in objFolderA.Files
   If DateDiff("n", file.DateLastModified, Now) =< 60 Then
          objFSO.CopyFile file.Path, UsbKey
   End If


Next

Set objFSO=Nothing
 
 
Post #: 1
 
 RE: VB Script to compare with a reg key - 2/15/2008 3:10:12 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
Previous post is I need help, please help,

(in reply to dmp_92)
 
 
Post #: 2
 
 RE: VB Script to compare with a reg key - 2/16/2008 6:30:47 AM   
  DiGiTAL.SkReAM


Posts: 1128
Score: 6
Joined: 9/6/2005
From: Florida, USA
Status: offline
What is your question?  You stated what you are trying to do, but do you have a particular question?


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to dmp_92)
 
 
Post #: 3
 
 RE: VB Script to compare with a reg key - 2/19/2008 12:32:42 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
This is a section of code I have been trying to work on.  I need this to check for a paticular reg key.  If there is no reg key or if the date stamp on the reg key is greater then the date stamp on the file then I need the script to continue other wise end script.

Option Explicit
'Dim objWMIService, objItem, colItems
Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\")


'****************** check to see if the files are more current ************************************************

' For Each file in objFolderA.Files
' Check for reg key "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING" for date stamp
' if no reg key found on computer then continue with script
' if reg key is more current then file then
' WScript.Quit 

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 4
 
 RE: VB Script to compare with a reg key - 2/19/2008 1:51:27 AM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
would something like this work. May help in what your trying to achive


      

_____________________________

"You start coding. I'll go find out what they want."

(in reply to dmp_92)
 
 
Post #: 5
 
 RE: VB Script to compare with a reg key - 2/19/2008 2:34:45 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
Thanks for the code.  It will definalty help out.  Now comes the hardest part of the code which I am stuck on.   This is what I have

'****************************** This section will create new Registry Keys ***********************************************************************
' const HKEY_LOCAL_MACHINE = &H80000002
'strComputer = "."
'Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
'    strComputer & "\root\default:StdRegProv")
'
'KeyPath = "Software\Emergency Planning\MySubKey"
'Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)
'
'If (Return = 0) And (Err.Number = 0) Then
'   
'   Wscript.Echo "HKEY_LOCAL_MACHINE\Software\Emergency Planning\MySubKey created"
'Else
'    Wscript.Echo "CreateKey failed. Error = " & Err.Number
'End If

This code is suppose to create a reg key with a date stamp in it.  Then the code you provided will check on that on the second time that they would like to copy it over.

(in reply to gdewrance)
 
 
Post #: 6
 
 RE: VB Script to compare with a reg key - 2/19/2008 2:55:05 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
I have altered some stuff, but I can not get the reg key to write to the registry.

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
   strComputer & "\root\default:StdRegProv")

KeyPath = "Software\Emergency Planning\File"
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)

If (Return = 0) And (Err.Number = 0) Then
  
  Wscript.Echo "HKEY_LOCAL_MACHINE\Software\Emergency Planning\File created"
Else
   Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If

(in reply to dmp_92)
 
 
Post #: 7
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:12:23 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Do you get an error? Do yo have On Error Resume Next anywhere in your code?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 8
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:16:23 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
I do not get an error and I do not have On Error Resume Next anywhere in my code.  When I look at my registry it is not there.

I will put On Error Resume Next  and post again.

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:19:47 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
The is working fine and will never error.  I will post the section below.

On Error Resume Next
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
   strComputer & "\root\default:StdRegProv")

KeyPath = "Software\Emergency Planning\File"
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)

If (Return = 0) And (Err.Number = 0) Then
  
  Wscript.Echo "HKEY_LOCAL_MACHINE\Software\Emergency Planning\File created"
Else
   Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If

(in reply to dmp_92)
 
 
Post #: 10
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:45:35 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Well if you really want the key to be created then putting On Error Resume Next in will not address the problem. As a matter of fact it is a bad idea. If you had have said that you had On Error Resume next already I would have told you to remove it.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 11
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:49:24 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
Okay,

So how do I get this little piece of code to work for me?

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: VB Script to compare with a reg key - 2/19/2008 3:57:16 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Hard to say without errors. Especially since that code works fine for me.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 13
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:02:12 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
Have you checked your registry and search for the key?

I have and it is not there for me.

I will test this out on another computer.

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:07:09 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
I did check my registry and the key was there.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 15
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:16:18 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
You were right, it does work when running it by itself.  I was adding it to my entire script and it was not working.  What I did notice is that it created a folder but no data is in that key.  Is there a way to get a date stamp in that key when this is active?

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:26:00 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
A registry key does not hold data inherently. Think of it as a table in a database (since the regitry is a database). Until you put rows into the table, the table can exist without having any real data in it. For the registry, once you have a key created, you would need to use RegWrite to write a Name - Value pair to the key to hold the date.

Note, what I said about keys is not entirely correct since every key has one Name - Value pair when it is created. The Name is "Default" and the value is empty. So you could use that for the date if you want, but I am not a fan of using Default. I prefer to create a Name - Value pair where the Name is more meaningful than Default.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 17
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:38:43 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
I understand what you are saying.  I have modified the code.  The one thing I need to know is how I can put the current date in.


const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
   strComputer & "\root\default:StdRegProv")

KeyPath = "Software\Emergency Planning\File"
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\Software\Emergency Planning\File\", "" today() ""

'"Hello world!"


If (Return = 0) And (Err.Number = 0) Then
  
  Wscript.Echo "HKEY_LOCAL_MACHINE\Software\Emergency Planning\File created"
Else
   Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If

The line with

WshShell.RegWrite "HKLM\Software\Emergency Planning\File\", "" today() ""

my today line does not work.

(in reply to ebgreen)
 
 
Post #: 18
 
 RE: VB Script to compare with a reg key - 2/19/2008 5:54:06 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
This will put the date and time into the default value:

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
  strComputer & "\root\default:StdRegProv")
KeyPath = "Software\Emergency Planning\File"
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\Software\Emergency Planning\File\", Now()
'"Hello world!"

If (Return = 0) And (Err.Number = 0) Then
 
Wscript.Echo "HKEY_LOCAL_MACHINE\Software\Emergency Planning\File created"
Else
  Wscript.Echo "CreateKey failed. Error = " & Err.Number
End If




If you just want the date, let me know.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dmp_92)
 
 
Post #: 19
 
 RE: VB Script to compare with a reg key - 2/19/2008 6:10:09 AM   
  dmp_92

 

Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
That's exactly what I needed.  When I put it all together there is one issue.  It doesn't do the inital check for the date stamp or reg key to see if it is there or what is more current.

Here is the entire code:

Option Explicit
Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\")
Set WshShell = WScript.CreateObject("WScript.Shell")


Set WshShell = CreateObject("Wscript.Shell")

varToday = Weekday(Date)

Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\File"

'Check if scan has run today and if so exit
On Error Resume Next
LastRunDate = WshShell.RegRead(Verify & "File")
If LastRunDate =  cstr(Date) Then
    WScript.Quit
End If

strMbox = "."

strMbox = msgBox("Please insert your Emergency Planning usb key and press the enter key to continue")

If strMbox = 1 Then

   strmbox ="."

end if

strComputer = "."


arrDriveType = array("Unknown",_
           "No Root Directory",_
           "Removable Disk",_
           "Local Disk",_
           "Network Drive",_
           "Compact Disk",_
           "RAM Disk")

set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")

For Each Disk in ColDisks

     strType = arrDriveType(Disk.DriveType)
     strID = Disk.DeviceID
     strSys = Disk.SystemName

     If strType = "Removable Disk" then

           'strMbox = MsgBox("USB drive letter is " & Disk.DeviceID)
           UsbKey = strId & "\"
           USBFound = True
     End If

Next

If USBFound = False Then
     MsgBox "No USB drive was found."
     WScript.Quit
End If

For Each file in objFolderA.Files
   If DateDiff("n", file.DateLastModified, Now) =< 60 Then
          objFSO.CopyFile file.Path, UsbKey
   End If

WshShell.RegWrite "HKLM\Software\Emergency Planning\File\", Now()


MsgBox "All files have sucessfully copied"


Next

Set objFSO=Nothing

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2 3 4   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VB Script to compare with a reg key Page: [1] 2 3 4   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts