I need a batch file to add a record to hosts file in windows, however I do not need just a file append writing because I would like to check if this record is already present. Is it possibile?
type "%SystemRoot%\system32\drivers\etc\hosts" | find "my_record" ||echo my_record>>"%SystemRoot%\system32\drivers\etc\hosts"
try this.You'll need administrator permissions to add something in hosts file
hmmm tricky one that.
First thing I would say is forget about trying to do this with a batch file.
Batch files alone lack the power to do anything like this unless used with external tools.
While it's certainly possible, unless you know exactly what command line tools you need to install and how you'll end up experimenting endlessly to find a good combination.
If you do want to go this route however, then I would suggest you take a look at the GnuWin32 project on sourceforge where they've ported all the standard unix command line tools to windows, and which include Sed, Grep, Awk and many more that are perfect for what you're trying to do.
I would however strongly suggest that you look at using Powershell, or even better if you have any .NET experience a command line tool in C#.
I can't off the top of my head think of a powershell example, but you could do it in C# with something like:
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main()
{
const string filename = "path\to\your\hosts\file";
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(filename))
{
string line;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
string newLineToAdd = "127.0.0.1 somedomain.com"
bool found = false;
foreach(string line in lines)
{
if(line.Equals(newLineToAdd))
{
found = true;
}
}
if(!found)
{
lines.Add(newLineToAdd);
}
using (StreamWriter writer = new StreamWriter(filename))
{
foreach(string line in lines())
{
writer.WriteLine(line);
}
}
}
}
Please note, that's not a full example, and I've certainly not had time to test compile it, but in theory what you need to do (Irrespective of the language you choose) is:
1) Load all the lines of the file into an array 2) Scan each line in the array check to see if it equals the line you want to add 3) If it does not already exists in the array, add the new line to the array 4) Write all the lines in the array back to disk, completely overwriting the old file.
Some things to note about my example:
1) I've hard coded everything, you'll want to pass in some parameters (Unfortunately I don't have time at the moment to type up a full featured sample)
2) Modifying the host file MUST have admin privileges, therefore you will either need to run the program from a shortcut marked to run as admin, or you will need to set your .NET project up to compile and be executed as an admin program by adding some extra code to it to use the UAC functions in windows. (The first method is the quickest and easiest)
Hopefully however my notes here should get you started in the right direction.
If I get time later on (and I remember....) I'll come back and see if I can improve things a bit more.
suggestion based on the code by npocmaka:
for /f "tokens=1*" %%i in ("%my_record%") do type "%SystemRoot%\system32\drivers\etc\hosts" | find "%%i" | find "%%j" >nul||echo %my_record%>>"%SystemRoot%\system32\drivers\etc\hosts"
This solves not the issue of the false IP match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With