/build/static/layout/Breadcrumb_cap_w.png

How to change Hard coded path

Hi, I have a script but I have no idea how to replace hard coded path in it, if I`m changing anything then it is not working anymore, can some experienced guy look in it and give me advice?:

On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Sets variables
FilePath="c:\windows\system32\drivers\etc\hosts"
SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

'Opens text file in read mode (Read=1, Write=2, Append=8)
Set objFile = objFSO.OpenTextFile(FilePath, 1)

'Adds all text in text file to "FileContent" string
FileContent = objFile.ReadAll
objFile.Close

'Instr searches "FileContent" looking for "SearchTerm", if not found returns 0
if Instr(FileContent,SearchTerm) = 0 then
  
   'Not found code
   'Opens text file in append mode so you can add text (Read=1, Write=2, Append=8)
   Set objFile = objFSO.OpenTextFile(FilePath, 8)
   'Writes new line to text fle vbCrLf inserts a return(s) to ensure new line
   objFile.WriteLine (vbCrLf & NewText & vbCrLf)
   objFile.Close

Else
  
   'Found search term
   ReplaceOldLine = Replace(FileContent, "# 127.0.0.1       localhost", NewText)
   'Opens text file in write mode to replace old line (Read=1, Write=2, Append=8)
   Set objFile = objFSO.OpenTextFile(FilePath, 2)
   're-writes whole content of file with replaced values (if needed)
   objFile.WriteLine ReplaceOldLine
   objFile.Close

end If

WScript.Quit


0 Comments   [ + ] Show comments

Answers (2)

Answer Summary:
Option Explicit 'On Error Resume Next Dim objFSO, objWshShell, objFile Dim WinDir, FilePath, SearchTerm, NewText, FileContent, ReplaceOldLine Set objFSO = CreateObject("Scripting.FileSystemObject") Set objWshShell = CreateObject("WScript.Shell") WinDir = objWshShell.ExpandEnvironmentStrings("%WinDir%") FilePath= WinDir & "\system32\drivers\etc\hosts" SearchTerm="# 127.0.0.1 localhost" NewText=" 127.0.0.1 localhost" Set objFile = objFSO.OpenTextFile(FilePath, 1) FileContent = objFile.ReadAll objFile.Close If Instr(FileContent,SearchTerm) = 0 then Set objFile = objFSO.OpenTextFile(FilePath, 8) objFile.WriteLine (vbCrLf & NewText & vbCrLf) objFile.Close Else ReplaceOldLine = Replace(FileContent, SearchTerm, NewText) Set objFile = objFSO.OpenTextFile(FilePath, 2) objFile.WriteLine ReplaceOldLine objFile.Close End If Set objFile = Nothing Set objFSO = Nothing Set objWshShell = Nothing WScript.Quit
Posted by: jagadeish 12 years ago
Red Belt
2

Option Explicit

'On Error Resume Next

Dim objFSO, objWshShell, objFile
Dim WinDir, FilePath, SearchTerm, NewText, FileContent, ReplaceOldLine

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshShell = CreateObject("WScript.Shell")


WinDir = objWshShell.ExpandEnvironmentStrings("%WinDir%")


FilePath= WinDir & "\system32\drivers\etc\hosts"

SearchTerm="# 127.0.0.1       localhost"
NewText=" 127.0.0.1       localhost"

Set objFile = objFSO.OpenTextFile(FilePath, 1)

FileContent = objFile.ReadAll
objFile.Close

If Instr(FileContent,SearchTerm) = 0 then

   Set objFile = objFSO.OpenTextFile(FilePath, 8)
   objFile.WriteLine (vbCrLf & NewText & vbCrLf)
   objFile.Close

Else
  

   ReplaceOldLine = Replace(FileContent, SearchTerm, NewText)
   Set objFile = objFSO.OpenTextFile(FilePath, 2)
   objFile.WriteLine ReplaceOldLine
   objFile.Close

End If

Set objFile = Nothing
Set objFSO = Nothing
Set objWshShell = Nothing

WScript.Quit


 


Comments:
  • The number of space between # and 127.0.0.1 in the script should match with the number of space between # and 127.0.0.1 in your host file.. :) - jagadeish 12 years ago
  • Also.. The number of space between 127.0.0.1 and localhost in the script should match with the number of space between 127.0.0.1 and localhost in your host file.. :) - jagadeish 12 years ago
Posted by: lanselots 12 years ago
Fifth Degree Brown Belt
0

Thank you

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ