Announcement

Collapse
No announcement yet.

Batch File-Find/Replace

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Batch File-Find/Replace

    Hi,

    I am trying to come up with a batch file that will find and replace text in a text file. So far I am using this:
    Code:
    @echo off
    if exist c:\newfile.txt del c:\newfile.txt
    for /f "tokens=*" %%a in (c:\myfile.txt) do call :Change "%%a"
    del c:\myfile.txt
    rename c:\newfile.txt myfile.txt
    exit /b
    
    :Change
    set Text=%~1
    if "%Text%"=="VarC=100" (
    echo VarC=200>> c:\newfile.txt
    ) else (
    echo %Text%>> c:\newfile.txt
    )
    exit /b
    to edit myfile.txt:

    Code:
    [setup]
    VarA=1
    VarB=10
    VarC=100
    VarD=0
    However, when using the syntax:

    echo %Text%>> c:\newfile.txt

    I am not obtaining the correct text. I get:
    Code:
    [setup]
    VarA=
    VarB=10
    VarC=200
    instead of:

    Code:
    [setup]
    VarA=1
    VarB=10
    VarC=200
    VarD=0
    Can someone explain what is incorrect and how to solve it?

    Much appreciated,
    Skippybox

    #2
    I don't know much about batch details, but maybe this tutorial by a friend of mine could help :
    0.6180339887
    Rest In Peace, Sam!

    Comment


      #3
      Very nice tutorial Sam_Zen. Will definitely come in handy in the future.

      Comment


        #4
        He Skippybox (BTW, what does "Skippybox" mean ?)

        I am trying to help you. At the outset: Do you actually need the two files "newfile.txt" and "myfile.txt" ? And can you tell me a bit more about the context, in which you intend to use the program ?

        Comment


          #5
          Ok, don't worry. Here is the script:

          Code:
          @echo off
          setlocal enabledelayedexpansion
          
          if not exist "%1" (echo this file does not exist...)&goto :eof
          
          set /p findthis=find this text string:
          set /p replacewith=and replace it with this:
          
          
          for /f "tokens=*" %%a in (%1) do (
          
             set write=%%a
             if %%a==%findthis% set write=%replacewith%
          
             echo !write! 
             echo !write! >>%~n1.replaced%~x1
          )
          save the text as replace.cmd (important, not as .bat)

          And then you can call it like this:

          replace "C:\some path here\subfolder\myfile.txt"

          The script asks you to enter the search&replace strings. If you want a completely automatic script without user interaction, then just replace the variables %findthis% and %replacewith% with %2 and %3 respectively, delete the lines that start with set /p and call your script like this:

          replace "C:\some path here\subfolder\myfile.txt" "old text" "new text"

          If you don't like the screen output, then just delete one instance of echo !write!

          I hope that helps. It's just a small little script, but believe me, I have been working my fingers to the bone because for some reasons it never worked ... The "Dos" command interpreter has very strange rules for dealing with variables, special characters, etc. And I'm a beginner, this is the second batch script I ever wrote... But since you helped me so often here ....
          Last edited by boarder's paradise; 06.12.2008, 06:56 AM.

          Comment


            #6
            This is unusual. Actually I was working on this for you, but then you come along and solve it! I do appreciate you offering to return the favor. I'll see if your script solves the problems. This is actually to adjust ini files, and I would like to use both txt files, since a backup is critical in case of script failure.

            Comment


              #7
              oh ... such a shame, you know yesterday I thought Skippybox "helped me out of the soup" quite a few times, so I thought so I should try and see if I can find something where I can help him. I found this batch thing ...
              ... and is turns out now, it was also another thing to help me, not for you actually ... boooooooo ....
              I can see now, what it was intended for (IrfanView ini file ... )

              OK, if one day there is something you really need, just tell me ...

              And yes, I tested my script, it does what you asked for in your OP.

              Comment


                #8
                Don't worry so much. This will benefit me as well, I'm sure. Things you do can always help in indirect ways, so I'm definitely sure you've already helped. But, while it's not necessary to pay me back, I do appreciate your assistance! Thanks again boarder's paradise for the wonderful code. I was able to adjust it for the intended purpose, so I'll post that in that thread. This really is powerful and cool.

                OK, if one day there is something you really need, just tell me...
                Will do!

                Comment


                  #9
                  hehe, a moment of zen here

                  oh, if you post it somewhere, can you give me the link here, just to look ... ?

                  Originally posted by Skippybox View Post
                  Will do!
                  don't hesitate. thanks for the nice words and CU around
                  Last edited by boarder's paradise; 10.12.2008, 03:58 AM.

                  Comment


                    #10
                    Hi,

                    @boarder's paradise:
                    You should change your script:
                    Code:
                    @echo off
                    setlocal enabledelayedexpansion
                    
                    if not exist "%1" (echo this file does not exist...)&goto :eof
                    
                    set /p findthis=find this text string:
                    set /p replacewith=and replace it with this:
                    
                    
                    for /f "tokens=* delims=" %%a in (%1) do (
                    
                       set write=%%a
                       if "%%a"=="%findthis%" set write=%replacewith%
                    
                       echo !write! 
                       (echo !write!)>>%~n1.replaced%~x1
                    )
                    first "delims=": you will get the whole line even if there are blanks

                    second: if "%%a"=="%findthis%": the quotation marks will allow you to process empty lines, without them you will receive an error.

                    thirdecho !write!)>> : the brackets will allow you to use the double greater sign directly after the output. Your version will append a space. And if you don't use the brackets you can't write 0, 1 or 2 at the end of line.

                    @Skippybox: this version should work for you, the explanation is the same as above:
                    Code:
                    @echo off
                    if exist c:\newfile.txt del c:\newfile.txt
                    for /f "tokens=* delims=" %%a in (c:\myfile.txt) do call :Change "%%a"
                    del c:\myfile.txt
                    rename c:\newfile.txt myfile.txt
                    exit /b
                    
                    :Change
                    set Text=%~1
                    if "%Text%"=="VarC=100" (
                    (echo VarC=200)>> c:\newfile.txt
                    ) else (
                    (echo %Text%)>> c:\newfile.txt
                    )
                    exit /b
                    Regards, Nils.

                    Comment


                      #11
                      Thanks Nils for coming through again and pointing out the issues! Great job as usual.

                      Comment


                        #12
                        Originally posted by boarder's paradise View Post
                        He Skippybox (BTW, what does "Skippybox" mean ?)
                        Meaningless. It is just a unique name. I imagine one could come up with a meaning for it. What is boarder's paradise? Are you a surfer or something?

                        Comment


                          #13
                          Hello Nils,

                          Boarder's Paradise's modified script works well. I tried the modified script you provided for me, but it seems to fail when encountering characters like the pipe (|). It calls the subroutine, but I think it fails at this command:

                          set Text=%~1

                          Is there a way around this error, or would Boarder's Paradise's script be better?

                          Code:
                          @echo off
                          if exist c:\newfile.txt del c:\newfile.txt
                          for /f "tokens=* delims=" %%a in (c:\myfile.txt) do call :Change "%%a"
                          del c:\myfile.txt
                          rename c:\newfile.txt myfile.txt
                          exit /b
                          
                          :Change
                          set Text=%~1
                          if "%Text%"=="VarC=100" (
                          (echo VarC=200)>> c:\newfile.txt
                          ) else (
                          (echo %Text%)>> c:\newfile.txt
                          )
                          exit /b

                          Comment


                            #14
                            Hi,

                            special characters like the pipe "|" or the redirectors "<" and ">" are always a problem, also the bracktes "(" and ")" and the percent sign.

                            I'll think about a solution.

                            Boarders Paradise's script may have the same problems.

                            Regards, Nils.

                            Comment


                              #15
                              Nils,

                              Thanks for the reply and for pondering this. I have had problems with this before, but I thought there was some provision in the scripting language. So far, Boarder's Paradise's script seems to handle some special characters with my limited testing. If you find no solution, this script may be the better choice.

                              Comment

                              Working...
                              X