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.
 
there's a way. take a look at this:
'text file looks like this:
'Name,Department,Title
'Ken Myer,Finance,Fiscal Specialist
'Pilar Ackerman,Research,Manager
'Jonathan Haas,Headquarters,Fiscal Specialist
'mary mullins,human resources,programmer/analyst
'john doe,finance,office assistant
'jane doe,accounting,secretary

Code:
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\a_a"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathtoTextFile & ";" & _
        "Extended Properties=""text;HDR=YES;FMT=Delimited"""
strFile = "test.txt"       
sqlstring = "SELECT * FROM " & strFile & " WHERE name LIKE ('%mullins%');"  
objRecordset.Open sqlstring, objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF 
    msg = msg & objRecordset.Fields.Item("Name") & vbcrlf
    msg = msg & objRecordset.Fields.Item("Department") & vbcrlf
    msg = msg & objRecordset.Fields.Item("Title") & vbcrlf
    msg = msg & "----------------" & vbcrlf
    objRecordset.MoveNext
Loop
msg = msg & CStr(objRecordset.RecordCount) & " record(s) selected."          
msgbox msg, vbOkOnly, "text querying"
hope this helps.


 
Thanks for the reply. The value I am looking to change is going to be different as a tech images a computer. What I want is the script to read in the computer name perhaps using the variable %computername%. Since I don't know VB that well I am not sure if the code you suggest will work. It very well may be fine. This is kinda of what I am after.

 
OK, so from what I gather you want to read a file, find the line that starts with Computername and then change that line only right?

Code:
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:\Temp\computername.txt")
'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,12) = "Computername" Then 
		Line =  "Computername = " & strComputerName
	End If
	Report = Report & Line & vbCrLf
Next

Set objFile = objFSO.OpenTextFile("c:\Temp\computername.txt", ForWriting)
objFile.Write Report
objFile.Close

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.
 
Hi Mark,

Yes. This is exactly what I am after. I haven't tested this as I am on MAC at home. As long as I can change the computer name in the text file to the computer the script runs on then I am good.

Thank you!
 
Hi Mark / All,

I need to make a minor adjustment to this. The file I want to update is not a actual txt file. It is a Citrix configuration file (extension is ICA, I think) that can be opened with Notepad. So what I would like to do is rename the file prior to opening it. Let VB do the work and then at the very end rename it back. Should be easy to do, right?

Thanks.
 
Why rename it? If the file is a text file the posted code will work just fine. Just change the file name that the script opens to reflect the proper file.

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.
 
Its not a true text file even though it looks like one. I probably should have mentioned that originally. Like I said you can open it with Notepad. When I ran the code you posted above nothing happened to Line 12. I will run some more tests again, but I expect it won't update correctly. Thanks.
 
If it is not a true text file then you are correct, it won't work right on the write back.

Are you just trying to update an ICA connection on desktops? If so why not use a login script to delete the existing connection and copy the new one to the desktop?

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.
 
Logon script might be an option.

Would it be possible to rename the ICA to TXT
Set oTextStream = objFSO.OpenTextFile("c:\Temp\computername.txt")

Set objFile = objFSO.OpenTextFile("c:\Temp\computername.txt", ForWriting)
objFile.Write Reporto
bjFile.Close

The next line here renames the file

So I am bascially renaming before and after the fact so the code you suggest could work. Not a good idea??



 
Renaming to txt will not help because the file isn't a text file. The FileSystemObject will detect the file type no matter what the extension.

You can verify what it detects the file type as by using some simple code:
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = objFSO.GetFile(path_to_ica_file)
Wscript.echo oFile.Type

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.
 
It cannot... It just checks against the file association setting: it is file-extension-based! You can't reliable assert the content is a text file or not that way, no.
 
I see what your saying that you can't really trick the FileSystemObject in VB.

I ran it and it says:

Citrix ICA Client

Was just hoping for an easy way around this. I could rename this as a text file and it would work.

What about using a VB FileMove in another independant script?
 
Tsuji is correct in that renaming the file will make it report the file type is the same as the file extension, however I can run the same test using a bitmap file. If I check the file type against a BMP the FileSystemObject reports it as Bitmap document. If I rename it to a TXT it will then report that file as a text document, however I can't manipulate it like a text document because the file structure is different.



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.
 
Thanks. At this point too much time has been invested into this and I don't have the programming background to look at other options besides VB, which probably won't work either.
 
Is the file 'human-readable' when you open it in notepad? If so, it is probably an ASCII text file with a custom extension. You can manually update such a file (or use VBScript) even if the extension isn't ".txt". It may technically be a 'text' file even if the registered extension says something else.
 
Hi jges,

It is 'human-readable' when opened.

I thought the same thing, but Mark and the other guy who responded did not agree. My preference would have been to use VB, if possible. Its not a huge deal at this point, but thanks for jumping in.

 
I think your post confused us a little:
Briandr said:
Its not a true text file even though it looks like one.

Did you just mean the extension is not ".txt" or that it is truly NOT a text file (like when you try to open a .jpg in notepad all you get is gobledegook)?
 
The extension is not ".txt". The extension is ".ica"

All the text is clear. Nothing is messed up. Its like viewing a config file in Word. Nothing is distorted.
 
The code that Mark posted earlier should work, you will just have to change the filenames.

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

'open the data file
Set oTextStream = objFSO.OpenTextFile([COLOR=red]"c:\Temp\computername.ica"[/color])
'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,12) = "Computername" Then
        Line =  "Computername = " & strComputerName
    End If
    Report = Report & Line & vbCrLf
Next

Set objFile = objFSO.OpenTextFile([COLOR=red]"c:\Temp\computername.ica"[/color], ForWriting)
objFile.Write Report
objFile.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top