?
Solved

Search for a specific text match in a file

Posted on 2003-03-31
10
Medium Priority
?
139 Views
Last Modified: 2010-04-07
I am working with 'Sysprep.inf' file and I ma searching for 'ComputerName=' within the file.
However the ComputerName is been populated already and I want to know what is it exactly.
Then I want to take anything after the = sign and put it in a variable to be used to replace text in another file.

If I use the readline command it stops before the blank line in sysprep.
What about using skip n# of lines? How that should work?

0
Comment
Question by:Dr_K
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 10

Expert Comment

by:aeklund
ID: 8241814
It looks like your inf files is the same syntax as an INI file... Here are a few good examples for reading and writing to and INI file syntax:
[section]
key=value
key=value

etc...

http://www.vbaccelerator.com/codelib/inireg/inifile.htm
http://www.freevbcode.com/ShowCode.Asp?ID=306
http://www.freevbcode.com/ShowCode.Asp?ID=4037
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8241828
.inf are just like .ini files use this


Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Title = "Section" ' this is the title in the '[]'
fName = ??Path & "\Sysprep.inf"
Dim retVal As String * 255
Dim x As Integer
x = GetPrivateProfileString(Title, "ComputerName", "", retVal, 255, fName)
retValue = Left(retVal, x)
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8241870
another option :

Private Sub Command1_Click()
Dim sFile As String, Ln As String
Dim arrLn() As String
Dim ff As Integer
Dim ComputerName As String
sFile = "C:\Sysprep.Inf"
' get free file #
ff = FreeFile
' open file
Open sFile For Input As #ff
' loop until end of file
Do Until EOF(ff)
' read line into Ln string
Line Input #ff, Ln
' check for "ComputerName=" in string
If InStr(1, Ln, "ComputerName=") > 0 Then
    ' found, split by "=" into arrLn array
    arrLn = Split(Ln, "=")
    ComputerName = arrLn(1)
    GoTo Bye
End If
Loop
Bye:
Close #ff
MsgBox ComputerName
End Sub
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Dr_K
ID: 8242130
Here is my VB script , but when I get across an empty line in sysprep it stops reading
======================================================
'==========================================================================
'
' VBScript Source File
'
' NAME: <Read Computer Name form Sysprep.inf>
'
' AUTHOR:  ,Kostas
' DATE  : 03/31/2003
'
' COMMENT:=
'
'==========================================================================

Const ForReading = 1
set shell = Wscript.CreateObject("Wscript.shell")
set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set ENV = Shell.Environment
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs.OpenTextFile ("d:\bin\sysprep.inf", ForReading, True)
set info = FSO.OpenTextFile("d:\bin\sysprep.inf",1)
Do While info.AtEndOfLine <> True    
tag = info.readline
     If InStr(tag, "ComputerName") then
          machine = Right(tag,Len(tag)- InStr(tag, "="))
          Exit do
     End If
Loop
machine = "Americas"& Ucase(RTrim(machine))
WScript.Echo (machine)
f.close
======================================================
0
 
LVL 1

Expert Comment

by:codermar
ID: 8242219
Here you go:

' Change to actual path of file:

Const sysPrepPath = "sysprep.inf"

Dim ComputerName as String
ComputerName = getComputerName

Private Function getComputerName() as String
  Dim x as Integer
  Dim tmp as String
  Open sysPrepPath for Input as #1
    Line Input #1, tmp
    If Left(tmp, 13) = "ComputerName=" then
      getComputerName = Right(tmp, Len(tmp) - 13))
    End If
  Close #1
  getComputerName = ""
End Function
0
 

Author Comment

by:Dr_K
ID: 8242281
Tried howver it comes up with the following error
Untitled4.vbs(2, 18) Microsoft VBScript compilation error: Expected end of statement

============= Code follows ==================
Const sysPrepPath = "d:\bin\sysprep.inf"
Dim ComputerName as String
ComputerName = getComputerName
Private Function getComputerName() as String
 Dim x as Integer
 Dim tmp as String
 Open sysPrepPath for Input as #1
   Line Input #1, tmp
   If Left(tmp, 13) = "ComputerName=" then
     getComputerName = Right(tmp, Len(tmp) - 13))
   End If
 Close #1
 getComputerName = ""
End Function
==============================================
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8244066
Didn't know you were in VBScript, here is the fix for the code you posted

Const sysPrepPath = "d:\bin\sysprep.inf"
Dim ComputerName as String
ComputerName = getComputerName

Private Function getComputerName() as String
Dim x as Integer
Dim tmp as String
Open sysPrepPath for Input as #1
  Line Input #1, tmp
  If Left(tmp, 13) = "ComputerName=" then
'    getComputerName = Right(tmp, Len(tmp) - 13))
    getComputerName = Mid(tmp, instr(tmp, "=") + 1)
' You have an extra round bracket at the end,
'also it would be better practice to use Mid instead of Right
  End If
Close #1
'getComputerName = ""
If you left the above line the function would return an empty string every time
End Function


0
 
LVL 10

Accepted Solution

by:
aeklund earned 300 total points
ID: 8246375
Dr K-

Here is a mod version of your code with the corrections needed for it to work:

'==========================================================================
'
' VBScript Source File
'
' NAME: <Read Computer Name form Sysprep.inf>
'
' AUTHOR:  ,Kostas
' DATE  : 03/31/2003
'
' COMMENT:=
'
'==========================================================================

Const ForReading = 1
set shell = Wscript.CreateObject("Wscript.shell")
set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set ENV = Shell.Environment
Set fs = CreateObject ("Scripting.FileSystemObject")
'Set f = fs.OpenTextFile ("d:\bin\sysprep.inf", ForReading, True) 'Not needed
set info = FSO.OpenTextFile("d:\bin\sysprep.inf",1)
Do While info.AtEndOfStream <> True
'Do While info.AtEndOfLine <> True 'Replace with above line
tag = info.readline
    If InStr(tag, "ComputerName") then
         machine = Right(tag,Len(tag)- InStr(tag, "="))
         Exit do
    End If
Loop
machine = "Americas"& Ucase(RTrim(machine))
WScript.Echo (machine)
'f.close 'not needed
info.close 'added
0
 

Author Comment

by:Dr_K
ID: 8247015
Thank you , for the quick answer.  It did work.

0
 
LVL 10

Expert Comment

by:aeklund
ID: 8247044
No problem.. glad to help.  Thx for the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question