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

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.

Who is Participating?
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
AlexKhawAuthor Commented:
Sorry, in the title i have a typo error. it should be SEARCHING not aeraching. thank you
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 ./

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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>
AlexKhawAuthor Commented:
Hi IM,
What does this "setlocal enabledelayedexpansion" for? A syntex or a comment? Thank you.

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

Thank you
> 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..

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.

AlexKhawAuthor Commented:
Thanks a lot for your info and help IM!!
Have a nice day and see you around.
Yeah. :)
Cheers for the grade 'A'

AlexKhawAuthor Commented:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.