Adding text to Service file

S

Sunil Jain

Hello Everybody,

Any body knows how to add line to service file in etc
folder, if it is not there.

Sample VB Script for custom action or any other suggestion
are welcomed...

Thanks a lot for your help in advance

Cheers
Sunil
 
S

Sunil Jain

Got the VB Script for the same....



Dim varLine1 'Variable to store the current value to be
written
Dim varLine2 'Variable to store the current value to be
written
Dim varLine3 'Variable to store the current value to be
written
Dim varLine4 'Variable to store the current value to be
written

'Defining the value of each line to be searched and written
varLine1="gds_db 3050/tcp"
call InsertValues(varLine1)

varLine2="hds_db 3050/tcp"
call InsertValues(varLine2)

varLine3="ids_db 3050/tcp"
call InsertValues(varLine3)

varLine4="kds_db 3050/tcp"
call InsertValues(varLine4)


'InsertValues function will search value passed as
varibale and if not found it will write this variable at
end of file.
Sub InsertValues(varLine)

Dim varFileLines 'Variable to store the curretly read
line
Dim sFlag 'Varaiable to store the flag value
Dim WshShell 'Object pointing to the WScript

'Initializing the WScript object
Set WshShell = CreateObject("WScript.shell")
'System root
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")
'File Path
sFileName = SysRoot & "\system32\drivers\etc\services"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sFileName, 1)

sFlag = "False" 'Assumption the search string is not found

'looping through the services file
Do Until objFile.AtEndOfStream
varFileLines = objFile.ReadLine
If Trim(varFileLines) = varLine Then
sFlag = "True" 'Search string is found
Exit Do 'Quiting the loop
End If
Loop

'Closing the object
Set objFile = Nothing

'Checking for the flag
If Trim(sFlag) = "False" Then
'Writing the line since the search string is not found
in the file
Set objFile = objFSO.OpenTextFile(sFileName, 8)
objFile.WriteLine varLine
End If

Set objFile = Nothing

End Sub

Cheers
Sunil Jain
 
S

Sunil Jain

Got the VB Script for the same...


Dim varLine1 'Variable to store the current value to be
written
Dim varLine2 'Variable to store the current value to be
written
Dim varLine3 'Variable to store the current value to be
written
Dim varLine4 'Variable to store the current value to be
written

'Defining the value of each line to be searched and written
varLine1="gds_db 3050/tcp"
call InsertValues(varLine1)

varLine2="hds_db 3050/tcp"
call InsertValues(varLine2)

varLine3="ids_db 3050/tcp"
call InsertValues(varLine3)

varLine4="kds_db 3050/tcp"
call InsertValues(varLine4)


'InsertValues function will search value passed as
varibale and if not found it will write this variable at
end of file.
Sub InsertValues(varLine)

Dim varFileLines 'Variable to store the curretly read
line
Dim sFlag 'Varaiable to store the flag value
Dim WshShell 'Object pointing to the WScript

'Initializing the WScript object
Set WshShell = CreateObject("WScript.shell")
'System root
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%")
'File Path
sFileName = SysRoot & "\system32\drivers\etc\services"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sFileName, 1)

sFlag = "False" 'Assumption the search string is not found

'looping through the services file
Do Until objFile.AtEndOfStream
varFileLines = objFile.ReadLine
If Trim(varFileLines) = varLine Then
sFlag = "True" 'Search string is found
Exit Do 'Quiting the loop
End If
Loop

'Closing the object
Set objFile = Nothing

'Checking for the flag
If Trim(sFlag) = "False" Then
'Writing the line since the search string is not found
in the file
Set objFile = objFSO.OpenTextFile(sFileName, 8)
objFile.WriteLine varLine
End If

Set objFile = Nothing

End Sub

Cheers
Sunil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top