Pages

Showing posts with label append hosts file. Show all posts
Showing posts with label append hosts file. Show all posts

Thursday, 2 August 2012

Add Lines to File - VBSCript


Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\oracle\ora81\network\admin\tnsnames.ora", ForReading)

Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine
    intFailure = InStr(strLine, "LINE 1")
    intFailure2 = InStr(strLine, "LINE 2")
    intfailure3 = InStr(strLine, "LINE 3")
    intfailure4 = InStr(strLine, "LINE 4")
    intfailure5 = InStr(strLine, "LINE 5")
    intfailure6 = InStr(strLine, "LINE 6")
    intfailure7 = InStr(strLine, "LINE 7")

    If intFailure > 0 or intFailure2 > 0 or intFailure3 > 0 or intFailure4 > 0 or intFailure5 > 0 or intFailure6 > 0 intFailure7 > 0 Then
        strNewText = strNewText & strLine & vbCrLf
    Else
        strOtherNewText = strOtherNewText & strLine & vbCrLf
    End If
Loop

objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\oracle\ora81\network\admin\tnsnames.ora", ForWriting, True)
objTextFile.Write(strOtherNewText)
objTextFile.Close

'****************************************************************************************************************

dim filesys, filetxt
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("C:\oracle\ora81\network\admin\tnsnames.ora", ForAppending, True)
filetxt.WriteLine("LINE 1")
filetxt.WriteLine("LINE 2")
filetxt.WriteLine("LINE 3")
filetxt.WriteLine("LINE 4")
filetxt.WriteLine("LINE 5")
filetxt.WriteLine("LINE 6")
filetxt.WriteLine("LINE 7")
filetxt.Close

Tuesday, 30 August 2011

Add entries to Hosts File

Following script can be used to append entries in HOSTS file. A text file containing the entry to be appended should be placed in INPUT location as in below script


 Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim TargetFile,objWshShell,objFSO, Input, ProgramFiles, WinDir
Dim objTextFile, Target
Set objWshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
ProgramFiles = ObjWshShell.ExpandEnvironmentStrings("%ProgramFiles%")
WinDir = ObjWshShell.ExpandEnvironmentStrings("%WinDir%")
Set Targetfile = objFSO.GetFile(WinDir & "\system32\drivers\etc\hosts")
Set Input = objFSO.GetFile(ProgramFiles & "\Input.txt")

Set objTextFile = objFSO.OpenTextFile (Input, ForReading)
Set Target = CreateObject("Scripting.FileSystemObject")
Set TargetFile = Target.OpenTextFile (TargetFile, ForAppending, True)
Do Until objTextFile.AtEndOfStream
Input = objTextFile.ReadLine
TargetFile.WriteLine(Input)
Loop
TargetFile.Close
ObjTextFile.Close
objFSO.DeleteFile(ProgramFiles & "\Input.txt")