Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: how can i copy and replace sting in a txt file  (Read 35815 times)

0 Members and 1 Guest are viewing this topic.

mioo_sara

    Topic Starter


    Intermediate

    how can i copy and replace sting in a txt file
    « on: May 01, 2023, 12:49:58 AM »
    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

    Quote
    192.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



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Re: how can i copy and replace sting in a txt file
    « Reply #1 on: May 01, 2023, 03:16:17 AM »
    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
    Code: [Select]
    @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
    « Last Edit: May 01, 2023, 03:47:55 AM by Hackoo »

    mioo_sara

      Topic Starter


      Intermediate

      Re: how can i copy and replace sting in a txt file
      « Reply #2 on: May 01, 2023, 05:54:43 AM »
      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
      Code: [Select]
      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
      « Last Edit: May 01, 2023, 06:11:44 AM by mioo_sara »

      mioo_sara

        Topic Starter


        Intermediate

        Re: how can i copy and replace sting in a txt file
        « Reply #3 on: May 01, 2023, 05:59:52 AM »
        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



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Re: how can i copy and replace sting in a txt file
        « Reply #4 on: May 01, 2023, 06:44:54 AM »
        Give a try for this code, and tell me how it works on your side ?
        Code: [Select]
        @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

          Topic Starter


          Intermediate

          Re: how can i copy and replace sting in a txt file
          « Reply #5 on: May 01, 2023, 08:48:00 AM »
          when  i  run  this  in  a  .bat file  just stayed  on  this  blank  screen
          Quote
          The 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

           
          Quote
          192.168.233.188       4e-53-d0-68-34-bf     dynamic