logo
Published on john.parnefjord.se (http://john.parnefjord.se)

Findstr in Windows

By johnp
Created 2007-12-19 14:27

For those of you that have ever used Linux/Unix professionaly know that you can't do without grep. In Microsofts world most of the tasks are oriented against using GUI:s and thats perhaps why I can feel a bit frustrated from time to time with Windows. Sometimes the tiniest, simplest task, that could easily be solved on Linux, just takes forever or has to be dealt with some special tool. Which quite often means installing a heap of tools to check out before one can deal with the actual problem.

One of the tools I miss is on Windows is grep. Of course one can download Grep for Windows, but there is actually one tool that is shipped with Windows XP from start, findstr, that mimics grep. The documentation is quite poor though.

Recently, the staff working with the desktop computers at my work had to add a line to the hosts file in about 200 computers. Doing it manually? Don't think so! Instead they settled at running a bat-script when user start Windows, but they didn't know how to change the hosts file and they only wanted it to be changed ones . So I started digging around what tools are available for working with text from the command prompt. And there is was - findstr.

The resulting script checks if the ip address is present in hte hosts file. If not then add a new line and then append the ip address and domain name in question. Just save as a bat file and run.

rem If IP does not exist then append to hosts file
cd C:\Windows\System32\Drivers\etc\
FINDSTR  "192.168.100.10" hosts >NUL 2>&1 || echo. >> hosts && echo 192.168.100.10 MYDOMAIN.LOCAL >> hosts 

You can check that the new line was entered into hosts by:

 type C:\Windows\System32\Drivers\etc\hosts  

I believe that we will see quite a change on the Windows platform with a lot more tools without a GUI, just plain tools to be run in the command prompt. With the advent of Windows Server 2008, it will now be possible to run a windows server headless, and that will give users the need of simple tools.


Source URL:
http://john.parnefjord.se/node/41