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!

Script to find a specific line of text in ini and write it to a file

Status
Not open for further replies.

Steveo89990

IS-IT--Management
Oct 3, 2007
4
US
Need help writing a script to find a specific line of text in an ini file and write it to a text file. But will be looking on several computer to see if same info is there.
 
Searching for a specific line in a text file should not be so so difficult.
But show as the code what have you done so far and what is your problem.
 
This is what I have so far, this will be used to go out to several computers to get the response back and input to the outputfile.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Reads the hostnames file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\Host.txt", "1")
Set objFile = objFSO.OpenTextFile("c:\temp\outputfile.txt", 2)

Do Until ObjTextFile.AtEndOfStream
strHostName = objTextFile.ReadLine
If InStr(strHostName,",") Then
strHostName = left(strHostName,instr(strHostName,",")-1)
End If

on error resume Next

'Change attribute to read
'Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.GetFile("\\" & strHostName & "\c$\windows\system32\oeminfo.ini")

If objFile1.Attributes = objFile1.Attributes AND 1 Then
objFile1.Attributes = objFile1.Attributes XOR 1
End If
'Read the OEMINFO.INI file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile2 = objFSO.OpenTextFile("\\" & strHostName & "\c$\windows\system32\oeminfo.ini", ForReading)



objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.SkipLine
objFile2.ReadLine



objFile2.close

If Err.Number = 0 Then
objFile.Writeline (strHostName & objFile2.ReadLine)

Else objFile.Writeline (strHostName & " NA")
End If


Loop
 
Using the script above has worked in the past but I think I'm missing something. The problem I'm having is pulling just line number 13 and outputting to the text file. If you have something better I'm game!
Stevo

 
You may find something useful here:
thread329-1306288
 
I didn't test your code, but some quick items:
I would get rid of the "on error resume next"
The line "Set objFSO = CreateObject("Scripting.FileSystemObject")" is repeated 3 times... only the first time is necessary.

The lines below seem to be making the ini file read-only... why is this necessary?
Code:
    If objFile1.Attributes = objFile1.Attributes AND 1 Then
            objFile1.Attributes = objFile1.Attributes XOR 1
    End If

What exactly isn't working? Are you always getting hostname & " NA" as output?
 
What exactly isn't working?
At least the objFile2.ReadLine AFTER the objFile2.close
 
It actually is working now, I had to add another objFile2.SkipLine to get the line that I needed. apparently I was getting several of the hostname & "NA" on some of the computers but not all I was getting a blank line which I cannot figure out.But those particular computers had different
info in that line space.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top