| 1. |
Solve : updating hosts file with a batch? |
|
Answer» Hello, I've tried to find what i was looking for on the forums and the internet and have not been able to find an answer yet. nope, hosts file has over 11k lines in it (thanks to spybot S&then show some samples and where the IP you need to change is. show also your sample output. Quote I will download that now and see what i can do =] thank you very much for your help.don't underestimate the power of googling.... check here this is at the end of the hosts file atm: Code: [Select]127.0.0.1 www.searchdestroydownload.com 127.0.0.1 searchdestroydownload.com 127.0.0.1 win-loads.net 127.0.0.1 www.win-loads.net 127.0.0.1 www.win-pc-defender.com 127.0.0.1 win-pc-defender.com # End of entries inserted by Spybot - Search & Destroy 127.0.0.1 activate.adobe.com ***.***.***.*** pat ***.***.***.*** dan this is the ip.txt Code: [Select]?*?.?*?.?*?.?*? dan i need to replace the last line of the hosts file [***.***.***.*** dan] with [?*?.?*?.?*?.?*? dan] lol, was using three "?" but it came up as a smiley note his ip is dynamic, and it wont have 3 numbers in each octet every time.try this then Code: [Select] Set regEx = New RegExp regEx.Pattern = "\s+" 'pattern to substitute all spaces/tabs with 1 space Set objFS = CreateObject("Scripting.FileSystemObject") strFile = "c:\test\host.txt" strIPFile = "c:\test\ip.txt" Set objFile = objFS.OpenTextFile(strIPFile,1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine strLine = regEx.Replace(strLine, " ") splitted = Split(strLine," ") strIPtoChange = splitted(0) strUserName = splitted(1) Loop Set objFile = objFS.OpenTextFile(strFile,1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine,strUserName) > 0 Then splitted = Split(strLine) splitted(0) = strIPtoChange strLine = Join(splitted," ") End If WScript.Echo strLine Loop ok, i have sync.bat, and sync2.vbs in the same folder as DansIP.txt Sync.bat: Code: [Select]@echo off cls cd D:\FROMDAN\IP start D:\FROMDAN\IP\>cscript /nologo sync2.vbs new ip address dan pause I only get the error that new is not an internal/external command. sync2.vbs: Code: [Select]Set regEx = New RegExp regEx.Pattern = "\s+" 'pattern to substitute all spaces/tabs with 1 space Set objFS = CreateObject("Scripting.FileSystemObject") strFile = "c:\WINDOWS\system32\drivers\etc\hosts.txt" strIPFile = "D:\FROMDAN\IP\DansIP.txt" Set objFile = objFS.OpenTextFile(strIPFile,1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine strLine = regEx.Replace(strLine, " ") splitted = Split(strLine," ") strIPtoChange = splitted(0) strUserName = splitted(1) Loop Set objFile = objFS.OpenTextFile(strFile,1) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine,strUserName) > 0 Then splitted = Split(strLine) splitted(0) = strIPtoChange strLine = Join(splitted," ") End If WScript.Echo strLine Loop hosts file remains unchanged.you do not need to use start to call cscript. hostfile remain unchanged because all i do in the vbs is to echo out the changed lines. I leave it to you to figure out how to rename file. Its basic DOS Code: [Select]@echo off cscript /nologo myscript.vbs > newfile REM rename the newfile back to oldfile. got another batch file to work: Code: [Select]@echo off cls pushd "%systemroot%\system32\drivers\etc" type hosts|find /i /v "#dan's dynamic address" > hosts.new move "%cd%\hosts.new" "%cd%\hosts" for /f "usebackq" %%a in ("D:\FROM_DAN\IP\DansIP.txt") do ( >>hosts echo %%a dan #dan's dynamic address. ) popd |
|