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)
'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
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)
'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