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

VB Script Help Needed

Status
Not open for further replies.

trytofixalot

Technical User
Jun 28, 2010
8
GB
Hi, can someone provide some guidance on this please.
I have a vbs that parses a comma seperated text file looking for my computername, and if exists writes to regisry values, however i cannot get the loop function to work. The script currently only works if my computername is on the top of the list in the text file. Thanks !

An example of the mylist.txt files is -
computername123,Engineering,London

The VBS is -

option explicit
dim wsh, fso, inputfile, machinename, inputline, linetosplit, strhost, strbu, strseg

set wsh = createobject("wscript.shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set inputfile = fso.openTextFile("C:\Users\username\Documents\Tools and Scripts\BUSeg Script\mylist.txt",1, FALSE)
machinename = Wsh.ExpandEnvironmentStrings("%computername%")

Do While Not (InputFile.atEndOfStream)

inputline = inputfile.readline
linetosplit = split(inputline,",")
strhost = linetosplit(0)
strbu = linetosplit(1)
strseg = linetosplit(2)
if UCASE(strhost) = UCASE(machinename) then
wsh.regwrite "HKLM\Software\MyCompany\Application\Business",strbu
wsh.regwrite "HKLM\Software\MyCompany\Application\Segment",strseg
else
wsh.regwrite "HKLM\Software\MyCompany\Application\Business","Unknown"
wsh.regwrite "HKLM\Software\MyCompany\Application\Segment","Unknown"


end if

loop
 
What about this ?
Code:
...
Do While Not (InputFile.atEndOfStream)
  inputline = inputfile.readline
  linetosplit = split(inputline,",")
  strhost = linetosplit(0)
  strbu = linetosplit(1)
  strseg = linetosplit(2)
  if UCASE(strhost) = UCASE(machinename) then
    wsh.regwrite "HKLM\Software\MyCompany\Application\Business",strbu
    wsh.regwrite "HKLM\Software\MyCompany\Application\Segment",strseg
    Exit Do
  end if
loop
if UCASE(strhost) <> UCASE(machinename) then
  wsh.regwrite "HKLM\Software\MyCompany\Application\Business","Unknown"
  wsh.regwrite "HKLM\Software\MyCompany\Application\Segment","Unknown" 
end if

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[1] The logic, inherited from the original script, still seems terribly...wrong, although much improved from having the luxury of writing to registry one over the other in the original script. You've got to pray you encounter no row with strhost being the computername or that if you encounter one, it happens at the last row?!

[2] I would say, do not use "wsh" as a variable name. In this case, you've dimensioned it (dim wsh). That is fortunate and have the problem covered. Wsh is a reserved word and it is an alias of wscript.

[3] You've to put more control of the resulting ubound of the split. You would say, you make sure it is 2 or more...
 
> Wsh is a reserved word

Actually, the real problem is that it isn't a reserved word, and probably should be ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top