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

Author Topic: Find and Replace one string with another inside the file using DOS batch  (Read 72370 times)

0 Members and 1 Guest are viewing this topic.

Raven19528



    Hopeful
  • Thanked: 30
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Find and Replace one string with another inside the file using DOS batch
    « Reply #15 on: September 29, 2011, 10:10:38 AM »
    Wow, this thread just keeps getting revived from the grave. Fun.

    There is a way to do this in batch, though there are much better tools out there to accomplish the task as shown throughout the thread. The batch method is outlined and not fully coded below:

    This command would be used for one file and would need to be embedded in another for command if multiple files were needing to be changed.
    for /f "delims=" %%G in (changefile.txt) do (
      set oldstring=%%G
      set newstring=!oldstring:~0,  52=0,  21!
      echo !newstring!>>temp.txt
    )
    del changefile.txt
    ren temp.txt changefile.txt


    Again, this could be run on multiple files using this for command embedded into another for command that would determine what is placed in the "changefile.txt" area. Obviously you could put multiple "set newstring=" commands in if there were multiple changes that were needing to be made.

    Can we let this thread die now? It has lived a long life and should be allowed to rest in peace.
     ::)
    "All things that are
    Are with more spirit chased than enjoy'd" -Shakespeare

    Raven19528



      Hopeful
    • Thanked: 30
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Re: Find and Replace one string with another inside the file using DOS batch
      « Reply #16 on: September 30, 2011, 11:02:16 AM »
      for /f "delims=" %%G in (changefile.txt) do (
        set oldstring=%%G
        set newstring=!oldstring:~0,  52=0,  21!
        echo !newstring!>>temp.txt
      )
      del changefile.txt
      ren temp.txt changefile.txt


      One error: there is no ~ after the colon in the set newstring line. It should read:
      set newstring=!oldstring:0,  52=0,  21!
      "All things that are
      Are with more spirit chased than enjoy'd" -Shakespeare