Batch file for aeraching a string in a file and remark that particular string

Posted on 2005-04-10
Last Modified: 2010-08-05
Hi experts out there,
I am trying to write a batch file that can direct check a txt file for a particular string (for e.g. set Happy=2), then remark that partucular string (to make it look like this :: set Happy=2) in that file. Can this be done? I just need the part that perform serach and remmark the string will do.

Thank you very much.

Question by:AlexKhaw

    Author Comment

    Sorry, in the title i have a typo error. it should be SEARCHING not aeraching. thank you
    LVL 3

    Expert Comment

    I would advise you to use Perl for this.
    Perl has clean way of searching a string in a file.
    If your string is say "mystring", and file name is "myfile.txt", you could do this in this way.

    $mystring = "mystring";
    $filename = "myfile.txt";
    open FILEHANDLE, $filename;
      if( m/$mystring/ ) #tries to match $mystring in each line i.e. $_;
          print "got it";

    You can refer to Perl programming manuals available online for more details. This is just an example. You can gather more ideas elsewhere. Perl can be run as a batch file if you do this
    1.add  #!/usr/bin/perl to the top of perl file.
    2.Give this file executable permission by chmod +x
    3. Run this file as ./

    LVL 1

    Expert Comment

    Try this if you working on windows

    for /F %%t in (<yourfile>) do (if %%t==Happy=2 (echo set %%t >> temp.txt) else (echo %%t >> temp.txt))
    del <yourfile>
    copy temp.txt <yourfile>
    LVL 25

    Accepted Solution

    In Andrey's code, you may want to some needed quotes in:

    for /F %%t in (<yourfile>) do (if "%%t"=="Happy=2" (echo set %%t >> temp.txt) else (echo %%t >> temp.txt))

    To turn it all into a complete batch file however, where the user can input the string to remark out, then try this:

    @echo off
    title String Remarker
    setlocal enabledelayedexpansion
    set /p string="Enter the string to remark: "
    if /i "%string%"=="" goto :get_string
    set /p file="Enter the filename: "
    if not exist "%file%" (
       echo Cannot find file.
       goto :get_file
    find /i "%string%" "%file%" >NUL
    if %errorlevel% neq 0 (
       echo Cannot find String in file.
       goto :eof
    set /a count=0
    echo Remarking string "%string%" . . .
    del /f temp.txt >nul
    for /f "delims=" %%L in (%file%) do (
       if /i "%%L"=="%String%" (
          echo :: %%L>>temp.txt
          set /a count+=1
       ) else (
          echo %%L>>temp.txt
    del "%file%" /f >nul
    ren "temp.txt" "%file%" >nul
    echo Complete.
    echo %count% strings were remarked in file %file%.
    echo Press any key to exit . . .

    I hope that helps :)
    >> IM

    Author Comment

    Hi IM,
    What does this "setlocal enabledelayedexpansion" for? A syntex or a comment? Thank you.


    Author Comment

    Any website that you guys recommend to enchance Batch file skill? I am so impress about yours skill. :-)

    Thank you
    LVL 25

    Expert Comment

    > What does this "setlocal enabledelayedexpansion" for?
    To understand this, you need to have a grip on "scopes". I needed to make this declaration, for the "count" variables' sake.
    An example:

    If I was to do the following without the "setlocal enabledelayedexpansion" command:

    set count=0
    for /f "delims=" %%L in (file.txt) do call :count
    goto :eof

    set /a count+=1
    exit /b

    Then, you'd expect this to count all of the lines in the file, however, it won't, because of the scope. You see, the "count" variable after the :count declaration is not being used as the same variable as the "count" variable before the FOR statement.. However, if you were to add the "setlocal enabledelayedexpansion" at the start of the file, it would work fine...
    It's a really tedious item; but it would tell it to basically use the same variable no matter where in the scope it is..

    Do you see what I mean?  :o)

    For a more professional explanation, open the Command Prompt, and type: setlocal /?

    I've got to pop out... but I'll post some more info once I get back..

    LVL 25

    Expert Comment

    Okay, I'm back..

    As for resources on learning MS-DOS Batch Coding; well, I personally got a few books out on MS-DOS from the library when I was just 12 years old... that's what got me started in the Programming world :)
    I have then just played about in DOS; if you do the following at the command line: Type: Help  then press enter, this will list all of the available commands.. to learn how to use each command, just type: commandName /?
    So, for example, to learn how to use the FOR statement, at the command prompt, you would type:

      FOR /?

    However, I appreciate that not everyone has access to the library, so I've looked up some DOS tutorials for you:

    This site gives a lot of good information as well as explains most of the commands in msdos.  Its not really a tutorial.

    About the same as above but in a little bit different format.

    You might also want to take a look at this,

    Them, if you have any problems, or need further assistance with MS-DOS Programming, you can ask for help, with anything DOS related, in the MS-DOS TA - where you get people like SteveGTR, who is by far the best MS-DOS programmer on the site, always keen to help you :)  The MS-DOS TA:

    Best of luck with it.


    Author Comment

    Thanks a lot for your info and help IM!!
    Have a nice day and see you around.
    LVL 25

    Expert Comment

    Yeah. :)
    Cheers for the grade 'A'


    Author Comment



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now