Neeed some .bat help :D

Any other tech-related topics
Post Reply
Message
Author
-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Neeed some .bat help :D

#1 Post by -.- »

Been pming m^2 about my .bat files and I thought I would stop it :D so I'm going to post here lol
Basically I want to edit my hosts file and change some of the ip addresses in it. Like the hosts files have:
127.0.0.1 localhost
102.54.94.97 rhino1.acme.com
102.54.94.97 rhino2.acme.com
102.54.94.97 rhino3.acme.com

And I want to change it to
127.0.0.1 localhost
102.54.94.3 rhino1.acme.com
102.54.94.3 rhino2.acme.com
102.54.94.3 rhino3.acme.com

Here's what I've got so far but it isnt really working well. The first part finds any instances of acme.com and deletes the entire line. The second part prompts for the new port number and uses it in the last part to add the lines back into host. The deleting and renaming back to hosts file works fine :S but that's about it lol

Actually it works fairly well but eveytimeI run bat file again, it adds another blank line because of the echo. and I'm not sure how to remove blank lines in the hosts file. The reason I put in the echo. is because if it isn't there the new lines just add to the end of the localhost line and not a new line. So I can't do without it, but with it, it just keeps adding blank lines each time I run it lol.

Any help in fixing or rewriting this would be appreciated. It's a fun way to learn this but I'm out of ideas what to google on this lol.

Code: Select all

@echo off
setlocal enabledelayedexpansion
findstr/c:"acme.com" /i /v C:\WINDOWS\system32\drivers\etc\hosts > C:\WINDOWS\system32\drivers\etc\hosts1
:PICK
CLS
Echo "Enter Port Number (1,2,3,4)
set /p PORT=
If %PORT%==1 GOTO EDIT
If %PORT%==2 GOTO EDIT
If %PORT%==3 GOTO EDIT
If %PORT%==4 GOTO EDIT
CLS
Echo "The PORT you picked does not exist, try again"
PAUSE
GOTO PICK

:EDIT
ECHO.>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino1.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino2.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
ECHO 121.128.133.%PORT% rhino3.acme.com>>C:\WINDOWS\system32\drivers\etc\hosts1
del C:\WINDOWS\system32\drivers\etc\hosts
ren C:\WINDOWS\system32\drivers\etc\hosts1 C:\WINDOWS\system32\drivers\etc\hosts

Yucca
Posts: 88
Joined: Sun May 07, 2006 10:59 am

Re: Neeed some .bat help :D

#2 Post by Yucca »

Here is a different approach which replaces string data within a line. This is not very elegant, but it demonstrates the approach. It reads each line in a file and then replaces target text with replacement text. It then writes the modified version out to a new file.

Code: Select all

@echo off

MODE CON cols=100 lines=1000
CLS

setlocal enabledelayedexpansion

::CHANGE THE READFILE AND OUTFILE VARIABLES IN THE NEXT 2 LINES
set readfile=q:\z\oldfile.txt
set outfile=q:\z\newfile.txt

if exist %outfile% del %outfile%

:: Change target and replacement text in third line below
FOR /F "eol=; tokens=*" %%i IN (%readfile%) DO (
set newlin=%%i
echo !newlin:102.54.94.97=102.54.94.3! >> %outfile%)

echo.
echo.
notepad %outfile%
echo Changes completed.  Press any key to close this window . . .
echo.
echo.
pause > nul

--Yucca

-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Re: Neeed some .bat help :D

#3 Post by -.- »

thx it works... but is it possible get it to find the 102.54.94.97 line where the last digits are variable?

I mean I'm trying to make this so it'll prompt me for a ip number and I can change it between
102.54.94.1, 102.54.94.2, 102.54.94.3, 102.54.94.4 depending on which one I want it to go to

So if I put 1 into the prompt, it'll change whatever 102.54.94.__ into 102.54.94.1 then later on I put 2 into prompt and it changes to 102.54.94.2

I'm not sure how I did it was best way to add in the lines because it would enter a new blank line from echo. each time I ran it. But I dont know how to replace just the last ip digits without deleting and adding new ip lines.

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Neeed some .bat help :D

#4 Post by m^(2) »

-.- wrote:thx it works... but is it possible get it to find the 102.54.94.97 line where the last digits are variable?

I mean I'm trying to make this so it'll prompt me for a ip number and I can change it between
102.54.94.1, 102.54.94.2, 102.54.94.3, 102.54.94.4 depending on which one I want it to go to

So if I put 1 into the prompt, it'll change whatever 102.54.94.__ into 102.54.94.1 then later on I put 2 into prompt and it changes to 102.54.94.2

I'm not sure how I did it was best way to add in the lines because it would enter a new blank line from echo. each time I ran it. But I dont know how to replace just the last ip digits without deleting and adding new ip lines.
I have a cool and simple solution. It uses GSAR (GNU Search & Replace).
Preparation:
Change your hosts to:

Code: Select all

127.0.0.1 localhost
__ADDRESS__ rhino1.acme.com
__ADDRESS__ rhino2.acme.com
__ADDRESS__ rhino3.acme.com 
It will be fixed at the 1st start. The script:

Code: Select all

:EDIT
gsar -o -s"__ADDRESS__" -r"102.54.94.%PORT%" "C:\WINDOWS\system32\drivers\etc\hosts"
gsar -o -s"__ADDRESS__" -r"102.54.94.%PORT%" "%0"
The script modifies itself each run, so you always have the latest address from hosts in it's code.

-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Re: Neeed some .bat help :D

#5 Post by -.- »

thx i'll try this :D

edit: hm it didnt autoupdate itself :S but thx to the gsar thing, I made a clumsy and probably ugly code for this lol
I just made it search for any of the 4 ip address possibilites lol, since there will only always be those 4 choices :D

Code: Select all

:EDIT1
gsar -o -s"102.54.94.2" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.3" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.4" -r"102.54.94.%PORT%" "te.txt"
exit

:EDIT2
gsar -o -s"102.54.94.1" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.3" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.4" -r"102.54.94.%PORT%" "te.txt"
exit

:EDIT3
gsar -o -s"102.54.94.1" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.2" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.4" -r"102.54.94.%PORT%" "te.txt"
exit

:EDIT4
gsar -o -s"102.54.94.1" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.2" -r"102.54.94.%PORT%" "te.txt"
gsar -o -s"102.54.94.3" -r"102.54.94.%PORT%" "te.txt"
exit
Last edited by -.- on Mon Dec 28, 2009 2:39 pm, edited 1 time in total.

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Neeed some .bat help :D

#6 Post by m^(2) »

You could try %~dpnxs0 instead of %0. Should be more compatible.

-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Re: Neeed some .bat help :D

#7 Post by -.- »

thx the %~dpnxs0 worked

Um, do you know how I can make it check if the addresses are in host or not?

I have (code below) at the start of the .bat file

Code: Select all

IF EXIST (findstr/c:"acme.com" /i /v hosts.txt) GOTO PICK
Echo. >>hosts.txt
Echo __ADDRESS__ rhino1.acme.com>>hosts.txt
Echo __ADDRESS__ rhino2.acme.com>>hosts.txt
Echo __ADDRESS__rhino3.acme.com>>hosts.txt
GOTO PICK
I'm not doing it right though, The find command works by itself, and the IF statement I think I have right, but I cant get them to work together.
I want it if the 102.54.94.__ rhino1.acme.com lines aren't in hosts, it'll write __ADDRESS__ in before going to ask which one you want to edit the last digit to. The PICK part of the .bat is where I specify that the %port% is

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Neeed some .bat help :D

#8 Post by m^(2) »

-.- wrote:thx the %~dpnxs0 worked

Um, do you know how I can make it check if the addresses are in host or not?

I have (code below) at the start of the .bat file

Code: Select all

IF EXIST (findstr/c:"acme.com" /i /v hosts.txt) GOTO PICK
Echo. >>hosts.txt
Echo __ADDRESS__ rhino1.acme.com>>hosts.txt
Echo __ADDRESS__ rhino2.acme.com>>hosts.txt
Echo __ADDRESS__rhino3.acme.com>>hosts.txt
GOTO PICK
I'm not doing it right though, The find command works by itself, and the IF statement I think I have right, but I cant get them to work together.
I want it if the 102.54.94.__ rhino1.acme.com lines aren't in hosts, it'll write __ADDRESS__ in before going to ask which one you want to edit the last digit to. The PICK part of the .bat is where I specify that the %port% is
EXIST applies only to files.
Run findstr and check ERRORLEVEL.
I'm tired and I don't fully understand your post though, I'll reread it tomorrow. ;)

User avatar
m^(2)
Posts: 890
Joined: Sat Mar 31, 2007 2:38 am
Location: Kce,PL
Contact:

Re: Neeed some .bat help :D

#9 Post by m^(2) »

-.- wrote:thx the %~dpnxs0 worked

Um, do you know how I can make it check if the addresses are in host or not?

I have (code below) at the start of the .bat file

Code: Select all

IF EXIST (findstr/c:"acme.com" /i /v hosts.txt) GOTO PICK
Echo. >>hosts.txt
Echo __ADDRESS__ rhino1.acme.com>>hosts.txt
Echo __ADDRESS__ rhino2.acme.com>>hosts.txt
Echo __ADDRESS__rhino3.acme.com>>hosts.txt
GOTO PICK
I'm not doing it right though, The find command works by itself, and the IF statement I think I have right, but I cant get them to work together.
I want it if the 102.54.94.__ rhino1.acme.com lines aren't in hosts, it'll write __ADDRESS__ in before going to ask which one you want to edit the last digit to. The PICK part of the .bat is where I specify that the %port% is
Should be something like:

Code: Select all

findstr/c:"acme.com" /i hosts.txt
IF %errorlevel% EQU 0 goto PICK
Echo. >>hosts.txt
Echo __ADDRESS__ rhino1.acme.com>>hosts.txt
Echo __ADDRESS__ rhino2.acme.com>>hosts.txt
Echo __ADDRESS__rhino3.acme.com>>hosts.txt
GOTO PICK
I didn't test it, so there might be some errors though :P.

-.-
Posts: 325
Joined: Mon Oct 06, 2008 4:32 pm

Re: Neeed some .bat help :D

#10 Post by -.- »

thanks it worked :D
I guess this thread is done now until I think of something else to try lol, thx for help m^2

Post Reply