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

VBscript calls batch file, then loops

Status
Not open for further replies.

pauljrb

MIS
Feb 3, 2009
5
GB
I have a script that writes a .reg file and a batch file to import the .reg file. After the batch file has been writen I call it to do the import (it's a REG_BINARY key so I though regedit would be the easiest way..)
However when the script runs it loops and the batch file runs several times. code below, I'm a bit of a VBscript amateur so bear with me :)

Code:
Const HKEY_CURRENT_USER = &H80000001
set fso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles"
oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
   ProfileName = subkey 
		Next
RegFileName = "c:\Documents and Settings\All Users\setArchiveLocation.reg"
set regFileObj = fso.createtextfile(RegFileName,true)

regFileObj.WriteLine "Windows Registry Editor Version 5.00"
regFileObj.WriteLine VbCr
regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\"& ProfileName &"]"
regFileObj.WriteLine VbCr
regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\"& ProfileName &"\0a0d020000000000c000000000000046]"
regFileObj.WriteLine VbCr
regFileObj.WriteLine """001f0324""=hex:54,00,3a,00,5c,00,61,00,72,00,63,00,68,00,69,00,76,00,65,00,31,\"
regFileObj.WriteLine "  00,2e,00,70,00,73,00,74,00,00,00" 
regFileobj.close

BatFileName = "c:\Documents and Settings\All Users\RegImport.bat"
set regImportObj = fso.createtextfile(BatFileName,true)

regImportObj.WriteLine "regedit /s ""C:\Documents and Settings\All Users\setArchiveLocation.reg"""
regImportObj.WriteLine "exit"
' regImportObj.WriteLine "pause"

regImportObj.close

Set shell = CreateObject ("Wscript.Shell")
shell.run ("%comspec% /K ""C:\Documents and Settings\All Users\RegImport.bat"""), 1, True
set shell=nothing

wscript.sleep 1000

fso.DeleteFile "c:\Documents and Settings\All Users\RegImport.bat"

fso.DeleteFile "c:\Documents and Settings\All Users\setArchiveLocation.reg"
wscript.quit
 
to clarify when I comment out the call to the batch file it does not loop
 
Ok I have eliminated the For Next loop and just read in the defaultprofile value from HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\

(was being slightly over complicated!)

looks like it's definately the call to the batch file causing the problem just can't work out why...

new Code

Code:
' gets default outlook profile name
ProfileName = ReadReg("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile")
wscipt.echo ProfileName
' writes .reg file with appropriae values		

RegFileName = "c:\Documents and Settings\All Users\setArchiveLocation.reg"
set fso = CreateObject("Scripting.FileSystemObject")
set regFileObj = fso.createtextfile(RegFileName,true)
		regFileObj.WriteLine "Windows Registry Editor Version 5.00"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\ _
		"& ProfileName &"]"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\ _
		"& ProfileName & "\0a0d020000000000c000000000000046]"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine """001f0324""=hex:54,00,3a,00,5c,00,61,00,72,00,63,00,68,00,69,00,76,00,65,00,31,\"
		regFileObj.WriteLine "  00,2e,00,70,00,73,00,74,00,00,00" 
		regFileobj.close
set regFileObj=nothing

' writes batch file to import previously created .reg file
BatFileName = "c:\Documents and Settings\All Users\RegImport.bat"
set regImportObj = fso.createtextfile(BatFileName,true)

		regImportObj.WriteLine "regedit /s ""C:\Documents and Settings\All Users\setArchiveLocation.reg"""
		regImportObj.WriteLine "exit"
		regImportObj.close
		
set regImportObj=nothing

' runs batch file

Set shell = CreateObject ("Wscript.Shell")
		shell.run ("%comspec% /K ""C:\Documents and Settings\All Users\RegImport.bat"""), 1, True
set shell=nothing

' waits for batch file to run
wscript.sleep 5000

'cleans up 


fso.DeleteFile "c:\Documents and Settings\All Users\RegImport.bat"
fso.DeleteFile "c:\Documents and Settings\All Users\setArchiveLocation.reg"
set fso=nothing
wscript.quit
 
Copied the script to a network share rather than running from my desktop and it works...
 
You could just use your VBS to write directly to the registry without sending it to a batch file then running the batch file...

Code:
Const HKEY_LOCAL_MACHINE = &H80000002

'+--------------------+
'¦Set server name here¦
'+--------------------+
strComputer = "servernamehere"

'+--------------------------------+
'¦Set system objects and variables¦
'+--------------------------------+
Set objRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

'+----------------+
'¦Set DWORD values¦
'+----------------+
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableArchive",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableCrawler",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableExpiry",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableFileWatch",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnablePSTMigrations",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableReplayIndex",0
WriteDWORD objRegistry,HKEY_LOCAL_MACHINE,"SOFTWARE\KVS\Enterprise Vault\Storage","EnableRestore",0

Sub WriteDWORD(oReg,Hive,KeyPath,ValueName,Value)
  oReg.SetDWORDValue Hive,KeyPath,ValueName,Value
End Sub

Sub WriteString(oReg,Hive,KeyPath,ValueName,Value)
  oReg.SetStringValue Hive,KeyPath,ValueName,Value
End Sub
Here is where I learned about vbs and the registry
h
 
Whoops, looks like binary writes aren't quite that easy. That'll teach me to test before I post :)

Anyway, here's how to write binary registry values:

Code:
Const HKEY_LOCAL_MACHINE = &H80000002

'+------------------------+
'¦Set name of Vault server¦
'+------------------------+
strComputer = "."

'+--------------------------------+
'¦Set system objects and variables¦
'+--------------------------------+
Set objRegistry=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

Values = Array(&H11)
WriteBinary HKEY_LOCAL_MACHINE,"SOFTWARE\Test","TestBinary",Values
WriteDWORD HKEY_LOCAL_MACHINE,"SOFTWARE\Test","TestDWORD",&H0000007d
WriteString HKEY_LOCAL_MACHINE,"SOFTWARE\Test","TestString","From VBS"

Sub WriteDWORD(Hive,KeyPath,ValueName,Value)
  objRegistry.SetDWORDValue Hive,KeyPath,ValueName,Value
End Sub

Sub WriteString(Hive,KeyPath,ValueName,Value)
  objRegistry.SetStringValue Hive,KeyPath,ValueName,Value
End Sub

Sub WriteBinary(Hive,KeyPath,ValueName,Value)
  objRegistry.SetBinaryValue Hive,KeyPath,ValueName,Value
End Sub
 
Yeah registry values had me a bit stumped, I managed to get my script working, my issue was that the batch file I called seemed to make the whole script loop, I ended up copying it to a network share and running it from there, it now works, v strange. Thanks for your post I'll run wth that next time, hopefully it won't be for a while!
My working code is below for your reading pleasure!

Code:
set shell = createobject("wscript.shell")
' gets default outlook profile name
ProfileName = shell.regRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile")
'wscript.echo ProfileName
' writes .reg file with appropriae values		

RegFileName = "c:\Documents and Settings\All Users\setArchiveLocation.reg"
set fso = CreateObject("Scripting.FileSystemObject")
set regFileObj = fso.createtextfile(RegFileName,true)
		regFileObj.WriteLine "Windows Registry Editor Version 5.00"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\"& ProfileName &"]"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine "[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\"& ProfileName & "\0a0d020000000000c000000000000046]"
		regFileObj.WriteLine VbCr
		regFileObj.WriteLine """001f0324""=hex:54,00,3a,00,5c,00,61,00,72,00,63,00,68,00,69,00,76,00,65,00,31,\"
		regFileObj.WriteLine "  00,2e,00,70,00,73,00,74,00,00,00" 
		regFileobj.close
set regFileObj=nothing

' writes batch file to import previously created .reg file
BatFileName = "c:\Documents and Settings\All Users\RegImport.bat"
set regImportObj = fso.createtextfile(BatFileName,true)

		regImportObj.WriteLine "regedit /s ""C:\Documents and Settings\All Users\setArchiveLocation.reg"""
		regImportObj.WriteLine "exit"
		regImportObj.close
		
set regImportObj=nothing

' runs batch file

Set shell = CreateObject ("Wscript.Shell")
		shell.run ("%comspec% /C ""C:\Documents and Settings\All Users\RegImport.bat"""), 0, True
set shell=nothing

' waits for batch file to run
wscript.sleep 5000

'cleans up 


fso.DeleteFile "c:\Documents and Settings\All Users\RegImport.bat"
fso.DeleteFile "c:\Documents and Settings\All Users\setArchiveLocation.reg"
set fso=nothing
wscript.quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top