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

Author Topic: Replacing different text in many files "Find and Replace"  (Read 10684 times)

0 Members and 1 Guest are viewing this topic.

Soli004

    Topic Starter


    Rookie

    Replacing different text in many files "Find and Replace"
    « on: October 22, 2008, 07:43:48 AM »
    Hello!
    Have been searching for a way to find and replace part of text in files in a easy way with BAT command.
    I have about 100 files each time that is needed to change part of text, based on what text it is.
    Its about 20-25 text lines that need to be changed. I do have a little program "Find and Replace" but I have to manually write or copy and paste each line every time. I hope this can be down more automatic instead.
    Can this be done with a BAT command?  ???

    Regards
    Soren

    For example:
    Find in this folder of *.mif files with this text line and save them out as a *.txt file

    Find
    Symbol ("hello.bmp",0,24,0)
    Replace with
    Symbol (34,255,12)

    Next

    Find
    Symbol ("hola.bmp",0,24)
    Replace with
    Symbol (34,128,12)

    Next

    Find
    Symbol ("hej.bmp",0,24)
    Replace with
    Symbol (34,75353,12)
    etc. etc

    diablo416



      Hopeful
      Re: Replacing different text in many files "Find and Replace"
      « Reply #1 on: October 22, 2008, 08:16:24 AM »

      since i dont have find and replace i cannot tell you how to use it,

      did you create it yourself? , the lines you need to remove from the files are the unique in anyway?

      Soli004

        Topic Starter


        Rookie

        Re: Replacing different text in many files "Find and Replace"
        « Reply #2 on: October 22, 2008, 09:26:05 AM »
        Hello,
        "Replace in files" is the correct word and it is a freeware you can download
        The lines I need to replace is unique and exist in many places, all the files are tables with coordinates and the unique text line is connected to each coordinate so it can be several hundreds of them in each file. As I understand you can not edit parts of the text and save it with a line of BAT commands so if I am correctly informed you have to save it as a new file with the changes...?

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Replacing different text in many files "Find and Replace"
        « Reply #3 on: October 22, 2008, 01:26:15 PM »
        Quote
        As I understand you can not edit parts of the text and save it with a line of BAT commands so if I am correctly informed you have to save it as a new file with the changes...?

        That's correct! This little snippet will append .chg to each filename. You may have to add a path to the *.txt parameter.

        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        for /f %%x in ('dir /a:-d /s /b *.txt') do (
        for /f "tokens=* delims=" %%i in (%%x) do (
        set input=%%i
        set input=!input:Symbol ("hello.bmp",0,24,0)=Symbol (34,255,12)!
        set input=!input:Symbol ("hola.bmp",0,24)=Symbol (34,128,12)!
        set input=!input:Symbol ("hej.bmp",0,24)=Symbol (34,75353,12)!
        echo !input! >> %%x.chg
        )
        )
        endlocal

        Good luck.  8)


        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        Soli004

          Topic Starter


          Rookie

          Re: Replacing different text in many files "Find and Replace"
          « Reply #4 on: October 22, 2008, 02:55:48 PM »
          Sidewinder
          I did do as you suggested, but there is nada happening, not even an error message.
          I change the command lines.. see below.

          There is up to 100 unique files (file1,file2,file3,file4,file5 etc) and each of them has only one of the lines that needs to be changed, but this line can occur several hundreds times in each file.

          Only the last line of command "set input=!input:Streetname Char(32)=Stopname Char(90)" occurs in every single file and has to be changed in all files.

          Perhaps this explains it better  :-[
          Regards



          @echo off
          setlocal enabledelayedexpansion
          for /f %%x in ('dir /a:-d /s /b D:\Data\*.txt') do (
             for /f "tokens=* delims=" %%i in (%%x) do (
                set input=%%i
                set input=!input:Symbol ("KundeOrdre_Red.bmp",0,24,0)=Symbol (34,16711680,12)!
                set input=!input:Symbol ("KundeOrdre_Skin_PSE2.bmp",0,24,0)=Symbol (34,16760992,12)!
                set input=!input:Symbol ("KundeOrdre_Lime_PSE2.bmp",0,24,0)=Symbol (34,13696928,12)!
                set input=!input:Symbol ("KundeOrdre_LightBlue_PSE2.bmp",0,24,0)=Symbol (34,46320,12)!
                set input=!input:Symbol ("KundeOrdre_LightGreen_PSE2.bmp",0,24,0)=Symbol (34,53373,12)!
                set input=!input:Symbol ("KundeOrdre_LightOrange_PSE2.bmp",0,24,0)=Symbol (34,14718208,12)!
                set input=!input:Symbol ("KundeOrdre_WineRed_PSE2.bmp",0,24,0)=Symbol (34,12583008,12)!
                set input=!input:Symbol ("KundeOrdre_DarkBlue_PSE2.bmp",0,24,0)=Symbol (34,128,12)!
                set input=!input:Symbol ("KundeOrdre_L_Magenta_PSE2.bmp",0,24,0)=Symbol (34,16765183,12)!
                set input=!input:Symbol ("KundeOrdre_LightRed_PSE2.bmp",0,24,0)=Symbol (34,16736352,12)!
                set input=!input:Symbol ("KundeOrdre_LightPurple_PSE2.bmp",0,24,0)=Symbol (34,11559167,12)!
                set input=!input:Symbol ("KundeOrdre_Blue.bmp",0,24,0)=Symbol (34,255,12)!
                set input=!input:Symbol ("KundeOrdre_Green.bmp",0,24,0)=Symbol (34,65280,12)!
                set input=!input:Symbol ("KundeOrdre_Magenta.bmp",0,24,0)=Symbol (34,16711935,12)!
                set input=!input:Symbol ("KundeOrdre_Cyan.bmp",0,24,0)=Symbol (34,65535,12)!
                set input=!input:Symbol ("KundeOrdre_Brown.bmp",0,24,0)=Symbol (34,8388608,12)!
                set input=!input:Symbol ("KundeOrdre_LightYellow.bmp",0,24,0)=Symbol (34,16776960,12)!
                set input=!input:Symbol ("KundeOrdre_Purple.bmp",0,24,0)=Symbol (34,8388736,12)!
                set input=!input:Symbol ("KundeOrdre_Orange.bmp",0,24,0)=Symbol (34,16741680,12)!
                set input=!input:Streetname Char(32)=Stopname Char(90)!
                echo !input! >> %%x.chg
             )
          )
          endlocal

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Replacing different text in many files "Find and Replace"
          « Reply #5 on: October 22, 2008, 04:03:02 PM »
          I was curious if the parenthesis would be transparent to the set statement. I guess not. ;D

          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          for /f %%x in ('dir /a:-d /s /b D:\Data\\*.txt') do (
             for /f "tokens=* delims=" %%i in (%%x) do (
                set input=%%i
                set input=!input:Symbol ^("KundeOrdre_Red.bmp",0,24,0^)=Symbol ^(34,16711680,12^)!
                set input=!input:Symbol ^("KundeOrdre_Skin_PSE2.bmp",0,24,0^)=Symbol ^(34,16760992,12^)!
                set input=!input:Symbol ^("KundeOrdre_Lime_PSE2.bmp",0,24,0^)=Symbol ^(34,13696928,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightBlue_PSE2.bmp",0,24,0^)=Symbol ^(34,46320,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightGreen_PSE2.bmp",0,24,0^)=Symbol ^(34,53373,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightOrange_PSE2.bmp",0,24,0^)=Symbol ^(34,14718208,12^)!
                set input=!input:Symbol ^("KundeOrdre_WineRed_PSE2.bmp",0,24,0^)=Symbol ^(34,12583008,12^)!
                set input=!input:Symbol ^("KundeOrdre_DarkBlue_PSE2.bmp",0,24,0^)=Symbol ^(34,128,12^)!
                set input=!input:Symbol ^("KundeOrdre_L_Magenta_PSE2.bmp",0,24,0^)=Symbol ^(34,16765183,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightRed_PSE2.bmp",0,24,0^)=Symbol ^(34,16736352,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightPurple_PSE2.bmp",0,24,0^)=Symbol ^(34,11559167,12^)!
                set input=!input:Symbol ^("KundeOrdre_Blue.bmp",0,24,0^)=Symbol ^(34,255,12^)!
                set input=!input:Symbol ^("KundeOrdre_Green.bmp",0,24,0^)=Symbol ^(34,65280,12^)!
                set input=!input:Symbol ^("KundeOrdre_Magenta.bmp",0,24,0^)=Symbol ^(34,16711935,12^)!
                set input=!input:Symbol ^("KundeOrdre_Cyan.bmp",0,24,0^)=Symbol ^(34,65535,12^)!
                set input=!input:Symbol ^("KundeOrdre_Brown.bmp",0,24,0^)=Symbol ^(34,8388608,12^)!
                set input=!input:Symbol ^("KundeOrdre_LightYellow.bmp",0,24,0^)=Symbol ^(34,16776960,12^)!
                set input=!input:Symbol ^("KundeOrdre_Purple.bmp",0,24,0^)=Symbol ^(34,8388736,12^)!
                set input=!input:Symbol ^("KundeOrdre_Orange.bmp",0,24,0^)=Symbol ^(34,16741680,12^)!
                set input=!input:Streetname Char^(32^)=Stopname Char^(90^)!
                echo !input! >> %%x.chg
             )
          )
          endlocal

          Quote
          I did do as you suggested, but there is nada happening, not even an error message.

          That is very surprising, I got an error on the first set statement. Sounds like you had no files in the *.txt collection. Your files do have txt extensions, right?

          Quote
          There is up to 100 unique files (file1,file2,file3,file4,file5 etc) and each of them has only one of the lines that needs to be changed, but this line can occur several hundreds times in each file.

          This logic flows so that each line is checked for all the search arguments and the corresponding replacement made. After the run, you should have a .txt.chg file for every original .txt file.

           8)

          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          Soli004

            Topic Starter


            Rookie

            Re: Replacing different text in many files "Find and Replace"
            « Reply #6 on: October 23, 2008, 01:09:34 AM »
            Sidewinder

            Quote
            That is very surprising, I got an error on the first set statement. Sounds like you had no files in the *.txt collection. Your files do have txt extensions, right?

            It's actually *.MIF extensions, but I can change to *txt as well I tried both. There is also other files in the folder with the same filename but different extension (*.dat *.id etc) could this cause problems? I did remove all other files and used only the *.MIF and also changed to *.txt but still have "nada" and no errormessage. I tried on two systems XP and Windows Server 20003 the same thing....

            I then copied the command lines into the run "cmd window" and there I got message like following...

            %%x was unexpected at this time

            and also

            %%i was unexpected at this time

            I get a tiny little file with name %%x.chg, when opened it has one line of text
            !input!

            Any hint what this can be?

            Regards



            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Replacing different text in many files "Find and Replace"
            « Reply #7 on: October 23, 2008, 03:43:14 AM »
            Quote
            It's actually *.MIF extensions, but I can change to *txt as well I tried both. There is also other files in the folder with the same filename but different extension (*.dat *.id etc) could this cause problems?

            I tested with files with *.txt extensions. It's easier to change the script than the file labels, so long as they match.

            Quote
            There is also other files in the folder with the same filename but different extension (*.dat *.id etc) could this cause problems?

            The script filters *.txt extensions. Any other files are ignored.

            Quote
            I then copied the command lines into the run "cmd window" and there I got message like following...

            %%x was unexpected at this time

            Yeah, the for command is tricky like that. At the command line you use single % symbols; in a script you use %%

            Quote
            I get a tiny little file with name %%x.chg, when opened it has one line of text
            !input!

            I can't reproduce your results. Of course my test files were designed to have the script find a string and make a replacement. The fact that the file label is %%x.chg shows the first for loop did not pickup any files. Did you copy/paste the script code? The batch file size should be 2094 bytes. If you could post part of a mif file that contains a string we're searching for, I'll check again.

            What do you want to do with the *.dat and *.id files? We can arrange to have them processed by the script. Give us all the extensions you want included.

             8)

            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            Soli004

              Topic Starter


              Rookie

              Re: Replacing different text in many files "Find and Replace"
              « Reply #8 on: October 23, 2008, 06:54:48 AM »
              Sidewinder

              Well... the *.dat etc is of no interest in this script, but there is a *.MID that I need to process as well but that is another story, perhaps after when this is working good :)

              Thanks for your time

              Part of the MIF file below

              Version 300
              Charset "WindowsLatin1"
              Delimiter ","
              CoordSys Earth Projection 8, 112, "m", 15.8082777778, 0, 1, 1500000, 0 Bounds (-6748143.32561, -10000855.7646) (9748143.32561, 10000855.7646)
              Columns 1
                Streetname Char(32)
              Data

              Pline 3860
              1328370 6402663
              1328377.01 6402657
              1328370 6402646
              1328373 6402644
              1328381.01 6402640
              1328389.01 6402633
              1328399 6402625
              1328407 6402632
              1328414.01 6402636
              1328101.01 6403810
              1328082 6403825
              1328081 6403826
              1328055.01 6403849.99
              1328023 6403879.99
              1328018 6403883.99
                  Pen (2,54,255)
              Point 1326391 6408779.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326985.01 6410162.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1327129 6411343.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1327184 6411541
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326883 6411760
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326794.01 6411837
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326796 6411633
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326473 6411872
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326535.01 6412028.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326674 6411933.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1326529 6412483.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1327030 6412363
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)
              Point 1327326 6412616.99
                  Symbol ("KundeOrdre_Blue.bmp",0,24,0)

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: Replacing different text in many files "Find and Replace"
              « Reply #9 on: October 23, 2008, 07:53:29 AM »
              If you check back to reply #5, you'll notice there is a double backslash in the file path on the for statement. This would explain why %%x never took on a value.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              for /f %%x in ('dir /a:-d /s /b d:\data\*.mif') do (
                 for /f "tokens=* delims=" %%i in (%%x) do (
                    set input=%%i
                    set input=!input:Symbol ^("KundeOrdre_Red.bmp",0,24,0^)=Symbol ^(34,16711680,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Skin_PSE2.bmp",0,24,0^)=Symbol ^(34,16760992,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Lime_PSE2.bmp",0,24,0^)=Symbol ^(34,13696928,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightBlue_PSE2.bmp",0,24,0^)=Symbol ^(34,46320,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightGreen_PSE2.bmp",0,24,0^)=Symbol ^(34,53373,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightOrange_PSE2.bmp",0,24,0^)=Symbol ^(34,14718208,12^)!
                    set input=!input:Symbol ^("KundeOrdre_WineRed_PSE2.bmp",0,24,0^)=Symbol ^(34,12583008,12^)!
                    set input=!input:Symbol ^("KundeOrdre_DarkBlue_PSE2.bmp",0,24,0^)=Symbol ^(34,128,12^)!
                    set input=!input:Symbol ^("KundeOrdre_L_Magenta_PSE2.bmp",0,24,0^)=Symbol ^(34,16765183,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightRed_PSE2.bmp",0,24,0^)=Symbol ^(34,16736352,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightPurple_PSE2.bmp",0,24,0^)=Symbol ^(34,11559167,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Blue.bmp",0,24,0^)=Symbol ^(34,255,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Green.bmp",0,24,0^)=Symbol ^(34,65280,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Magenta.bmp",0,24,0^)=Symbol ^(34,16711935,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Cyan.bmp",0,24,0^)=Symbol ^(34,65535,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Brown.bmp",0,24,0^)=Symbol ^(34,8388608,12^)!
                    set input=!input:Symbol ^("KundeOrdre_LightYellow.bmp",0,24,0^)=Symbol ^(34,16776960,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Purple.bmp",0,24,0^)=Symbol ^(34,8388736,12^)!
                    set input=!input:Symbol ^("KundeOrdre_Orange.bmp",0,24,0^)=Symbol ^(34,16741680,12^)!
                    set input=!input:Streetname Char^(32^)=Stopname Char^(90^)!
                    echo !input! >> %%x.chg
                 )
              )
              endlocal

              I prbly shud take an online tpyng course, but Id never be able to tpye in the addr.  ;D
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              Soli004

                Topic Starter


                Rookie

                Re: Replacing different text in many files "Find and Replace"
                « Reply #10 on: October 23, 2008, 11:13:52 AM »
                Sidewinder
                Yes I saw it... but trust in your expertise so I thought it was supposed to be like that  ::)

                Anyway it did not help I still have the same "nada". What I do is to put the script in a txtfile that I rename to "ReplaceTxtInMIF.bat". I then have this bat file in the same folder as the files. I double click on the bat file, the cmd window flash once and disappear in a blink of an eye. Nothing happens and no error message. I did try to run it in cmd window by removing one % as you told me (command line only use one %), copying the script and past it in the cmd window and it start to run. After process finished I have *MIF.chg for each file, but the file only has one textline as follow:

                !input!
                !input!
                !input!
                !input!
                etc
                etc
                etc

                Is there something I do wrong??  ???

                Regards

                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: Replacing different text in many files "Find and Replace"
                « Reply #11 on: October 23, 2008, 12:04:10 PM »
                The posted code was designed as a batch file. The batch code can be in any directory
                as long the the path on the first for statement points to the location of the MIF files.

                I ran the portion of the MIF file you posted and got these results:

                Quote
                Version 300
                Charset "WindowsLatin1"
                Delimiter ","
                CoordSys Earth Projection 8, 112, "m", 15.8082777778, 0, 1, 1500000, 0 Bounds (-6748143.32561, -10000855.7646) (9748143.32561, 10000855.7646)
                Columns 1
                  Stopname Char(90)
                Data
                Pline 3860
                1328370 6402663
                1328377.01 6402657
                1328370 6402646
                1328373 6402644
                1328381.01 6402640
                1328389.01 6402633
                1328399 6402625
                1328407 6402632
                1328414.01 6402636
                1328101.01 6403810
                1328082 6403825
                1328081 6403826
                1328055.01 6403849.99
                1328023 6403879.99
                1328018 6403883.99
                    Pen (2,54,255)
                Point 1326391 6408779.99
                    Symbol (34,255,12)
                Point 1326985.01 6410162.99
                    Symbol (34,255,12)
                Point 1327129 6411343.99
                    Symbol (34,255,12)
                Point 1327184 6411541
                    Symbol (34,255,12)
                Point 1326883 6411760
                    Symbol (34,255,12)
                Point 1326794.01 6411837
                    Symbol (34,255,12)
                Point 1326796 6411633
                    Symbol (34,255,12)
                Point 1326473 6411872
                    Symbol (34,255,12)
                Point 1326535.01 6412028.99
                    Symbol (34,255,12)
                Point 1326674 6411933.99
                    Symbol (34,255,12)
                Point 1326529 6412483.99
                    Symbol (34,255,12)
                Point 1327030 6412363
                    Symbol (34,255,12)
                Point 1327326 6412616.99
                    Symbol (34,255,12)

                To reproduce your results, I had to disable the setlocal enabledelayedexpansion statement. Rather than guess, post the batch file you're using. I suspect something mechanical is wrong with the batch file, not the underlying logic.

                 8)

                Edit:

                Quote
                I then have this bat file in the same folder as the files. I double click on the bat file, the cmd window flash once and disappear in a blink of an eye

                Trying running the batch file from the command prompt, not by double clicking in Explorer.
                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                Soli004

                  Topic Starter


                  Rookie

                  Re: Replacing different text in many files "Find and Replace"
                  « Reply #12 on: October 23, 2008, 01:50:14 PM »
                  Yes you are correct, when copy direct from your script to the batch file it work immediately, something happens on the way when copy between different programs even though I did not see any difference.

                  Thanks for reminding me :)

                  Another question came up, that if you want to choose a folder and run the script on all subfolders that has MIF files, how would that be?

                  Thanks again for great help,

                  Kind regards  :)

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Replacing different text in many files "Find and Replace"
                  « Reply #13 on: October 23, 2008, 02:07:46 PM »
                  Quote
                  Another question came up, that if you want to choose a folder and run the script on all subfolders that has MIF files, how would that be?

                  That logic was already built into the dir command by using the /s switch.

                  Quote
                  Yes you are correct, when copy direct from your script to the batch file it work immediately, something happens on the way when copy between different programs even though I did not see any difference.

                  Not sure what you mean, but I'm glad everything is working.

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

                  -- Albert Einstein

                  Soli004

                    Topic Starter


                    Rookie

                    Re: Replacing different text in many files "Find and Replace"
                    « Reply #14 on: November 04, 2008, 11:36:01 AM »
                    Hello,
                    I got spaces problem. Using the lines below with space I used " but still it will not find the files.
                    Is there another solution for spaces in the directory?

                    Tried this way, but did not work

                    @echo off
                    setlocal enabledelayedexpansion
                    for /f %%x in ('dir /a:-d /s /b c:\"documents and settings"\soli004\data\*.mif') do (
                       for /f "tokens=* delims=" %%i in (%%x) do (
                          set input=%%i

                    Tried this way, but did not work

                    @echo off
                    setlocal enabledelayedexpansion
                    for /f %%x in ('dir /a:-d /s /b "c:\documents and settings\soli004\data\*.mif"') do (
                       for /f "tokens=* delims=" %%i in (%%x) do (
                          set input=%%i

                    Best regards