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

Author Topic: How to find and replace a string in text file using batch file  (Read 96218 times)

0 Members and 1 Guest are viewing this topic.

robtem

  • Guest
I want to write a batch file which can look through a textfile and find a string and replace it with another string. I saw some posting in a forum regarding this with the solution below, but it wont work if a text file contain special characters like <>.

set txtfile=D:\test.txt
set newfile=D:\new_test.txt
if exist "%newfile%" del /f /q "%newfile%"
set /p search=Search String=
set /p replace=Replace With=
for /f "tokens=*" %%a in (%txtfile%) do (
   set newline=%%a
   call set newline=%%newline:%search%=%replace%%%
   call echo %%newline%% >>%newfile%
)
Please help

ghostdog74



    Specialist

    Thanked: 27
    Re: How to find and replace a string in text file using batch file
    « Reply #1 on: August 23, 2007, 05:38:10 AM »
    show how your input file looks like. the string you want to replace, the expected output. its better this way

    DeltaSlaya



      Apprentice
    • Google
      Re: How to find and replace a string in text file using batch file
      « Reply #2 on: August 23, 2007, 06:01:04 AM »
      I don't think you can use < and >'s in batch, you can't evaluate them at least.
      System specs:
      Intel Core 2 Duo E6600 (up to 3.3 stock V and air)
      ASUS Striker Extreme
      XFX 8600GT XXX Edition
      2x 1gB Corsair XMS2 DDR2-800
      Seagate Barracuda 320gB SATA
      Raidmax Ninja 918 (520W ATXV2.0 PSU)
      -

      robtem

      • Guest
      Re: How to find and replace a string in text file using batch file
      « Reply #3 on: August 23, 2007, 06:06:26 AM »
      input looks like

      <Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

      expected
      <License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

      so, basically it replace a word Contents with License

      robtem

      • Guest
      Re: How to find and replace a string in text file using batch file
      « Reply #4 on: August 23, 2007, 06:10:12 AM »
      So DeltaSlaya, you mean we cant find and replace a string in something like .xml file?

      ghostdog74



        Specialist

        Thanked: 27
        Re: How to find and replace a string in text file using batch file
        « Reply #5 on: August 23, 2007, 07:41:33 AM »
        input looks like

        <Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

        expected
        <License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

        so, basically it replace a word Contents with License
        then just replace the word Contents with License without including the "<"....it should work..

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: How to find and replace a string in text file using batch file
        « Reply #6 on: August 23, 2007, 07:56:31 AM »
        You can use the < character or any other special character provided you use the ^ (escape) character:

        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        set txtfile=D:\test.txt
        set newfile=D:\new_test.txt
        if exist "%newfile%" del /f /q "%newfile%"
        set /p search=Search String=
        set /p replace=Replace With=
        for /f "tokens=*" %%a in (%txtfile%) do (
           set newline=%%a
           set newline=!newline:%search%=%replace%!
           echo !newline! >> %newfile%
        )

        Enter the search string as ^<Contents and the replace string  as ^<License

        Good luck.  8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        robtem

        • Guest
        Re: How to find and replace a string in text file using batch file
        « Reply #7 on: August 23, 2007, 09:11:55 AM »
        You can use the < character or any other special character provided you use the ^ (escape) character:

        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        set txtfile=D:\test.txt
        set newfile=D:\new_test.txt
        if exist "%newfile%" del /f /q "%newfile%"
        set /p search=Search String=
        set /p replace=Replace With=
        for /f "tokens=*" %%a in (%txtfile%) do (
           set newline=%%a
           set newline=!newline:%search%=%replace%!
           echo !newline! >> %newfile%
        )

        Enter the search string as ^<Contents and the replace string  as ^<License

        Good luck.  8)

        The problem here will be at the line set newline=%%a, it wont set it because of special characters

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: How to find and replace a string in text file using batch file
        « Reply #8 on: August 23, 2007, 09:35:19 AM »
        I have been unable to reproduce your problem. I changed the file paths but everything else is as posted:

        Code: [Select]
        d:\wfc\testlib>replace
        Search String=^<Contents
        Replace With=^<License

        d:\wfc\testlib>type test.txt
        <Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

        d:\wfc\testlib>type new_test.txt
        <License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

        Did you use the code that was posted? Can we see the results? It might be easier to switch to VBScript.

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        ghostdog74



          Specialist

          Thanked: 27
          Re: How to find and replace a string in text file using batch file
          « Reply #9 on: August 23, 2007, 09:46:36 AM »
          there, a vbscript for you...
          Code: [Select]
          Option Explicit
          Dim objFSO,objFile,myFile,line,toSearch,toReplace
          Set objFSO=CreateObject("Scripting.FileSystemObject")
          myFile="C:\temp\test.txt"
          toSearch = "Contents"
          toReplace = "License"
          Set objFile=objFSO.OpenTextFile(myFile,1)
          Do Until objFile.AtEndOfLine
              line = objFile.ReadLine
          If InStr(1,line,toSearch) > 0 Then
          line = Replace(line,toSearch,toReplace)
          WScript.Echo line
          Else
          WScript.Echo line
          End If
          Loop
          Set objFSO=Nothing
          Set objFile =Nothing
          output:
          Code: [Select]
          C:\vbscript>more c:\temp\test.txt
          xxx:yyy:mmm:nnn:ooo:zzz:
          aaa:bbbmmm:nnn:ooo::ccc:
          mmm:nnn:ooo:
          computername =computer1
          <Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>
          mmm:mmm:nnn:ooo:nnn:ooo:

          C:\vbscript>cscript /nologo test2.vbs
          xxx:yyy:mmm:nnn:ooo:zzz:
          aaa:bbbmmm:nnn:ooo::ccc:
          mmm:nnn:ooo:
          computername =computer1
          <License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>
          mmm:mmm:nnn:ooo:nnn:ooo:

          to pipe to a new file, use  >

          robtem

          • Guest
          Re: How to find and replace a string in text file using batch file
          « Reply #10 on: August 23, 2007, 10:13:52 AM »
          I have been unable to reproduce your problem. I changed the file paths but everything else is as posted:

          Code: [Select]
          d:\wfc\testlib>replace
          Search String=^<Contents
          Replace With=^<License

          d:\wfc\testlib>type test.txt
          <Contents:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

          d:\wfc\testlib>type new_test.txt
          <License:<Data:<Licensee:Mylicense><SiteID:123><ProductName:MyProduct>>>

          Did you use the code that was posted? Can we see the results? It might be easier to switch to VBScript.

           8)

          Sorry, this works. but for some reason when I set it directly using
          set search=^<Contents
          set replace=^<License

          it output "The system cannot find the file specified." in the command console, and it generate the output file which is exactly as the source file (didnt replace). What is the difference between setting directly and prompting for search and replace?


          Fen_Li



            Beginner

          • G-Smart thing Smart
          • Thanked: 2
            Re: How to find and replace a string in text file using batch file
            « Reply #11 on: August 23, 2007, 11:20:43 AM »
            Quote
            it output "The system cannot find the file specified." in the command console

            try use doublequote (") to set variable contain special char..
            set "search=^<Contents"
            set "replace=^<License"

            ..Still Newbie KID..

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: How to find and replace a string in text file using batch file
            « Reply #12 on: August 23, 2007, 12:21:26 PM »
            If you want to do it directly, you don't need the search and replace set statements. Embed the arguments directly in the code:

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set txtfile=D:\wfc\testlib\test.txt
            set newfile=D:\wfc\testlib\new_test.txt
            if exist "%newfile%" del /f /q "%newfile%"
            for /f "tokens=*" %%a in (%txtfile%) do (
               set newline=%%a
               set newline=!newline:^<Contents=^<License!
               echo !newline! >> %newfile%
            )

            Learning VBScript on a Windows machine makes more sense than messing around with all these crazy symbols used in the command environment....check out the ScriptCenter for more details.

            Just a thought.  8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            robtem

            • Guest
            Re: How to find and replace a string in text file using batch file
            « Reply #13 on: August 24, 2007, 03:27:27 AM »
            The reason i didnt use vb script is what i am trying to do will not support that. but it works fine now by applying the last posting. I appreciate

            deepaib123



              Newbie

              • Experience: Beginner
              • OS: Unknown
              Re: How to find and replace a string in text file using batch file
              « Reply #14 on: May 17, 2018, 12:06:51 PM »
              I wanted to replace "_Pragrma ("Some string")" in .c files in code.
              please can anyone help me with this?