Login | |
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 5:48:46 AM
|
|
 |
|
| |
dmp_92
Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
|
I made a change and now I get some errors on a different piece of 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") '************** Check if scan has run today and if so exit *************************** Set WshShell = CreateObject("Wscript.Shell") varToday = Weekday(Date) Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\" LastRunDate = WshShell.RegRead(Verify & "File\") WScript.Echo "I am comparing " & objFolderA & " TO " & LastRunDate 'If objFolderA.file = LastRunDate Then 'WScript.Quit 'End If The error I get is when it trys to verify if the key is in the registry. Once I removed the registry key I now get errors. How do I tell it to continue if there is no registry key?
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 6:03:42 AM
|
|
 |
|
| |
dmp_92
Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
|
Here is all my code. Option Explicit Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell Dim objReg, keypath, return Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject") Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\") Set WshShell = WScript.CreateObject("WScript.Shell") '************** Check if scan has run today and if so exit *************************** Set WshShell = CreateObject("Wscript.Shell") varToday = Weekday(Date) Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\" 'LastRunDate = WshShell.RegRead(Verify & "File\") WScript.Echo "I am comparing " & LastRunDate & " TO " & Date() 'If objFolderA.file = LastRunDate 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 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\", date() '"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 MsgBox "All files have sucessfully copied" Next Set objFSO=Nothing I got it working but I am still having an issue with the top part. Line 17. When you wipe the reg key or if the reg key is not there then I get this error "Invailid root in registry key"
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 6:07:33 AM
|
|
 |
|
| |
ebgreen
Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
|
Put On Error Resume next right before you try to read the key. Then check the value of Err.Number to see if the read worked. Last, put On Error Goto 0 to put error handling back to normal.
_____________________________
"... 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
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 6:12:45 AM
|
|
 |
|
| |
dmp_92
Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
|
Okay When I put on error Resume next, it worked fine. Since I didn't get at error I went on a changed it to On error Goto 0 and I got an error Line 19 invalid root in registry key. Option Explicit Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell Dim objReg, keypath, return Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject") Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\") Set WshShell = WScript.CreateObject("WScript.Shell") '************** Check if scan has run today and if so exit *************************** Set WshShell = CreateObject("Wscript.Shell") varToday = Weekday(Date) Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\" On Error Goto 0 LastRunDate = WshShell.RegRead(Verify & "File\") WScript.Echo "I am comparing " & LastRunDate & " TO " & Date() 'If objFolderA.file = LastRunDate 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 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\", date() '"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 MsgBox "All files have sucessfully copied" Next Set objFSO=Nothing
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 7:01:39 AM
|
|
 |
|
| |
ebgreen
Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
|
It looks like objFolderA is a Folder object. The Folder object does not have a .File property. It does have a .Files collection. Are you trying to compare the date of a file to the date that is stored in the reg key?
_____________________________
"... 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
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 7:26:58 AM
|
|
 |
|
| |
ebgreen
Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
|
Are you sure that the date/time stamp on the files will be changed when they are copied? Regardless, here is how you would do it: For Each oFile In objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\").files Set oLastFile = oFile Next dteFileDate = oLastFile.DateLastAccessed strFileDate = Month(dteFileDate) & "/" & Day(dteFileDate) & "/" & Year(dteFileDate) If strFileDate = LastRunDate Then
_____________________________
"... 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
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 7:51:26 AM
|
|
 |
|
| |
dmp_92
Posts: 70
Score: 0
Joined: 2/1/2008
Status: offline
|
It still doesn't work. Let me post what I have. Option Explicit Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell Dim objReg, keypath, return, NextSection Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject") Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\") Set WshShell = WScript.CreateObject("WScript.Shell") '************** Check if scan has run today and if so exit *************************** Set WshShell = CreateObject("Wscript.Shell") varToday = Weekday(Date) Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\" On Error Resume Next LastRunDate = WshShell.RegRead(Verify & "File\") WScript.Echo "I am comparing " & LastRunDate & " TO " & Date() For Each oFile In objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\").files Set oLastFile = oFile Next dteFileDate = oLastFile.DateLastAccessed strFileDate = Month(dteFileDate) & "/" & Day(dteFileDate) & "/" & Year(dteFileDate) If strFileDate <> LastRunDate Then WScript.Quit End If 'If strFileDate = LastRunDate Then ' Goto NextSection ' Else ' WScript.Quit 'End If ' ************************************************************************************* NextSection 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 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\", date() '"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 Next MsgBox "All files have sucessfully copied" Set objFSO=Nothing Now I do know it is running because of this section that prompts me WScript.Echo "I am comparing " & LastRunDate & " TO " & Date() But then it ends the script. I do not have any registry key entries and I have placed a new file in the test folder.
|
|
| |
|
|
|
 |
RE: VB Script to compare with a reg key - 2/20/2008 8:04:14 AM
|
|
 |
|
| |
ebgreen
Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
|
This code should do it. You should remove the global On Error Resume Next. Option Explicit Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell Dim objReg, keypath, return, NextSection Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject") Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\") Set WshShell = WScript.CreateObject("WScript.Shell") '************** Check if scan has run today and if so exit *************************** Set WshShell = CreateObject("Wscript.Shell") varToday = Weekday(Date) Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\" On Error Resume Next LastRunDate = "NONE" LastRunDate = WshShell.RegRead(Verify & "File\") WScript.Echo "I am comparing " & LastRunDate & " TO " & Date() For Each oFile In objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\").files Set oLastFile = oFile Next dteFileDate = oLastFile.DateLastAccessed strFileDate = Month(dteFileDate) & "/" & Day(dteFileDate) & "/" & Year(dteFileDate) If strFileDate = LastRunDate Then WScript.Quit End If 'If strFileDate = LastRunDate Then ' Goto NextSection ' Else ' WScript.Quit 'End If ' ************************************************************************************* NextSection 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 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\", date() '"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 Next MsgBox "All files have sucessfully copied" Set objFSO=Nothing
_____________________________
"... 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
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|