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!

Help With VB Script

Status
Not open for further replies.

Briandr

MIS
Jul 11, 2003
177
US
Hi All,

I want to update a specific line (say line 4) in a text file with the computer name. So in my text file I have:

Computername=changeme

I don't want to append a line at the top or the bottom of the text file. My code would imply I wish to append a line, but that is not what I want. I know this code is not close to being correct, but after wrestling with it I threw in the towel. Here it is in its present form.

Const ForAppending = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHNetwork = Createobject("Wscript.Network")
strComputerName = WSHNetwork.Computername
Set objFile = objFSO.OpenTextFile("c:\computername.txt", ForAppending)
objFile.WriteLine Now "Computername=" & strComputername
objFile.Close

Thanks.
 
I made some very minor tweaks, but it works! Only one question. If I leave Username= blank in the ICA file then it works. However, if Username=changeme then it does not erase the value that is already there. Is there a way to account for a value already being there or will I just have to leave it blank, which is fine too.

Thanks to all.

Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHNetwork = Createobject("Wscript.Network")
strComputerName = WSHNetwork.Computername

'open the data file
Set oTextStream = objFSO.OpenTextFile("c:\meditech.ica")
'make an array from the data file
ConfigFileArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

For Each Line In ConfigFileArray
If Left(Line,46) = "Username=" Then
Line = "Username=" & strComputerName
End If
Report = Report & Line & vbCrLf
Next

Set objFile = objFSO.OpenTextFile("c:\meditech.ica", ForWriting)
objFile.Write Report
objFile.Close
 
Try making this snippet similar to what Mark gave you for the computername:
Code:
For Each Line In ConfigFileArray
    If Left(Line,[COLOR=red]46[/color]) = "Username=" Then

should be:
Code:
For Each Line In ConfigFileArray
    If Left(Line,[COLOR=red]9[/color]) = "Username=" Then

The way you have it now will always return false because the string "Username=" does not contain 46 characters; therefore it will never match the first 46 characters of the 'Line' variable.
 
Where the heck did "46" come from? In my example it was "12".

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Not sure what I confused. But it is working properly using "9", thankfully.

I think I had resigned myself to the fact the darn thing would not work. I am not going to look gift horse in the mouth.

I'll take it. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top