The two ">>" means you are appending to the file.
The better way to do this would be to rename the files back to the original before you run it again.
So at the end of the batch file just delete the original IPlist.txt and rename IPlistNew.txt to IPlist.txt.
Code:
set _Domain=forums.techguy.org
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup %_Domain% 8.8.8.8^|findstr "Address:"') do (
SET targetip=%%d
FOR /F "tokens=1* delims= " %%I in (IPlist.txt) do (
IF /I %_Domain% == %%J (
echo %%d %%J>>IPListNew.txt
) else (
echo %%I %%J>>IPListNew.txt
)))
del IPlist.txt
rename IPlistNew.txt IPlist.txt
Instead of running this code three times, you can just put all the domains you want to lookup into a text file and read the text file into a loop.. You would basically nest another For Loop outside of your existing code. If you wanted to update all the domains in IPlist.txt you could use that as your input instead of manually defining each domain you want to lookup.
Code:
FOR /F "tokens=1* delims= " %%A in (IPlist.txt) do (
FOR /F "tokens=2 skip=1 delims=: " %%d in ('nslookup %%B 8.8.8.8^|findstr "Address:"') do (
FOR /F "tokens=1* delims= " %%I in (IPlist.txt) do (
IF /I %%B == %%J (
echo %%d %%J>>IPListNew.txt
) else (
echo %%I %%J>>IPListNew.txt
))))
del IPlist.txt
rename IPlistNew.txt IPlist.txt
Pretty sure I can shorten up the code some more if you go with this second solution. I really shouldn't have to nest an additional For Loop now that I think about it.