how can i copy and replace sting in a txt file

Started by mioo_sara, May 01, 2023, 12:49:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mioo_sara

hi  to  all  i  have a  text file  that  contain an  ip  but  should  be  update  every day  for  an vpn  how  can  i replace  this  ip  in  a txt  file  with   another  one

my ip.txt  file  contain  this

Quote192.168.233.188       4e-53-d0-68-34-bf     dynamic   
(@ECHO OFF
ECHO Configuring Proxy Settings please wait...
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d  192.168.158.134:8080 /f
EXIT

i  want first  line (192.168.233.188) just  this  one replace  with  ip  in  5th  line  (192.168.158.134)

attention==  first  line  ip  will  change  every  day

how  can  i do  it  can  you  help?

i  batch  script  could  be  very  nice
thanks

Hackoo

#1
Hi !
First question : How did you get the ip and save it into config.txt ?
EDIT :
If I presume that you get the IP from the command line ARP -A in the first line
You can do something like that without using the config.txt
@echo off
Title Change VPN Settings
for /f "tokens=2" %%a in ('arp -a ^| findstr /i "\<Interface\>"') do set IP=%%a
echo The IP address is %IP%
pause
ECHO Configuring Proxy Settings please wait...
ECHO REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
ECHO REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d  %IP%:8080 /f
pause

Note : I added the command ECHO To be sure if the IP is correct, then you can remove every ECHO before the command REG ADD

mioo_sara

#2
thanks  dear Hackoo
your  cod is  working  but i  am  using  vpn from my  mobile  phone  for  my  laptop  pc  so 
i get  my  new  ip in  this  way

Quote@echo off
REM clear arp cache (optional):
arp -d
REM ask everybody on the subnet for a response:
for /l %%a in (1,1,255) do start /b ping -n 2 -w 500 192.168.1.%%a >nul
REM wait for the processes to finish:
timeout 2 >nul
REM show the responses (with IP and MAC):
arp -a>mynew-ip.txt
pause


in  mynew-ip.txt  i  have this
Interface: 192.168.11.181 --- 0xc
  Internet Address      Physical Address      Type
  192.168.11.49           4e-53-d0-68-34-bf     dynamic   


192.168.11.49==== is  my  new  working  ip  that  i  need  every  day so  you  should  use this  ip  in  your  cod

mioo_sara

if  its  complicated  and  time  consuming  just  answer  my  question  in  first  post  of  this  topic  and  i will  solve  the  other  parts  with  external  aps  automatically
just  replace  first  line  ip  in  post #1 with  old  ip in  5th  line
thanks

Hackoo

Give a try for this code, and tell me how it works on your side ?
@echo off
for /f "tokens=1" %%a in ('findstr "192.168.11." mynew-ip.txt') do set IP=%%a
echo The new working IP is %IP%
pause

mioo_sara

when  i  run  this  in  a  .bat file  just stayed  on  this  blank  screen
QuoteThe new working IP is
Press any key to continue . . .

after ip  is  empty
actually  my  internet  ip  is  in  mynew-ip.txt  in  the first  line  of the  file  and stored  next  to  this  .bat  file  in  current  window

Quote192.168.233.188       4e-53-d0-68-34-bf     dynamic