Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: Batch File-Find/Replace

  1. #1
    Professional User Skippybox's Avatar
    Join Date
    Jul 2008
    Location
    Detroit, MI USA
    Posts
    2,030
    Version
    IrfanView 4.25
    OS
    32-bit Win XP Home SP2
    CPU Cores
    1

    Question 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. #2
    Moderator Sam_Zen's Avatar
    Join Date
    May 2007
    Location
    NL
    Posts
    1,897
    Version
    IrfanView 4.27
    OS
    Win XP Home SP1
    CPU Cores
    1

    Default

    I don't know much about batch details, but maybe this tutorial by a friend of mine could help :
    http://vtatila.kapsi.fi/batch_tutorial.html
    0.6180339887
    Rest In Peace, Sam!

  3. #3
    Professional User Skippybox's Avatar
    Join Date
    Jul 2008
    Location
    Detroit, MI USA
    Posts
    2,030
    Version
    IrfanView 4.25
    OS
    32-bit Win XP Home SP2
    CPU Cores
    1

    Default

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

  4. #4
    Multiple User boarder's paradise's Avatar
    Join Date
    Sep 2008
    Posts
    290
    Version
    IrfanView 4.20

    Default

    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 ?

  5. #5
    Multiple User boarder's paradise's Avatar
    Join Date
    Sep 2008
    Posts
    290
    Version
    IrfanView 4.20

    Default

    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 at 05:56 AM.

  6. #6
    Professional User Skippybox's Avatar
    Join Date
    Jul 2008
    Location
    Detroit, MI USA
    Posts
    2,030
    Version
    IrfanView 4.25
    OS
    32-bit Win XP Home SP2
    CPU Cores
    1

    Default

    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.

  7. #7
    Multiple User boarder's paradise's Avatar
    Join Date
    Sep 2008
    Posts
    290
    Version
    IrfanView 4.20

    Default

    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.

  8. #8
    Professional User Skippybox's Avatar
    Join Date
    Jul 2008
    Location
    Detroit, MI USA
    Posts
    2,030
    Version
    IrfanView 4.25
    OS
    32-bit Win XP Home SP2
    CPU Cores
    1

    Default

    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!

  9. #9
    Multiple User boarder's paradise's Avatar
    Join Date
    Sep 2008
    Posts
    290
    Version
    IrfanView 4.20

    Default

    hehe, a moment of zen here

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

    Quote 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 at 02:58 AM.

  10. #10
    Moderator
    Join Date
    Aug 2007
    Location
    Germany
    Posts
    845
    Version
    IrfanView 4.38
    OS
    64-bit Win 7 Ultimate/Enterprise SP1
    CPU Cores
    8

    Default

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •