Loading

Tuesday, August 25, 2009

Windows Command Line Find and replace - Made easy with FART.exe

Here is a great little application that does a find and replace on a particular file, file type or file contents, then replaces it with a string of your choice. It can look in sub directories as well.
The small app is called FART, yes that’s right FART - Find And Replace Text!

Usage: FART [options] [--] [,...] [find_string] [replace_string]
Options
  • -h, –help Show this help message (ignores other options)
  • -q, –quiet Suppress output to stdio / stderr
  • -V, –verbose Show more information
  • -r, –recursive Process sub-folders recursively
  • -c, –count Only show filenames, match counts and totals
  • -i, –ignore-case Case insensitive text comparison
  • -v, –invert Print lines NOT containing the find string
  • -n, –line-number Print line number before each line (1-based)
  • -w, –word Match whole word (uses C syntax, like grep)
  • -f, –filename Find (and replace) filename instead of contents
  • -B, –binary Also search (and replace) in binary files (CAUTION)
  • -C, –c-style Allow C-style extended characters (\xFF\t\n\r\\ etc.)
  • –cvs Skip cvs dirs; execute “cvs edit” before changing files
  • –svn Skip svn dirs
  • –remove Remove all occurences of the find_string
  • -a, –adapt Adapt the case of replace_string to found string
  • -b, –backup Make a backup of each changed file
  • -p, –preview Do not change the files but print the changes
Example 1 (replace text, preview only)
fart -c -r -i -p *.txt original_text new_text
This will look for all .txt files in a sub directory, locate the original_text string within the .txt file and change it to new_text. The -p switch means it won't actually change anything because this is a preview, showing you how many strings it found within each .txt file.

Example 2 (replace text)
fart -c -r -i *.txt original_text new_text
Same as above except it will do the actual replacement.

Example 3 (remove text)
fart -r -i --remove *.txt "remove this text"
Rather than replacing one term for another this will remove the specified term.

NOTE: Use quotes around text if it contains spaces, tabs, etc.

Download FART from SourceForge.

Keywords:

windows fart.exe
fart find and replace
notepad find/replace text command line
fart "command line" download
fart.exe find and replace
find and replace fart
replace file command line windows
"find and replace" fart examples
detailed manual fart find replace
find and replace command line executable

23 comments:

  1. great tool

    any idea of how i can go about replacing a text string that contains a colon?

    c:\windows\fart.exe --c-style "S:\misc_videos\Skate\*.wvx" G:\_Media_Center\ <Ref href=G:\_Media_Center\

    Thanks

    ReplyDelete
    Replies
    1. Try to use an online "escaping tool" like this:
      http://www.htmlescape.net/stringescape_tool.html

      Delete
  2. Mark, try using the backslash (\) as an escape character immediately before the colon. And (or?) put both text strings in quotes. Example: c:\windows\fart.exe --c-style "S\:\misc_videos\Skate\*.wvx" "G\:\_Media_Center\"

    ReplyDelete
  3. I had code I needed to replace in Windows that looked like this:

    div id ..
    /div>

    And needed to remove both lines. Obviously I can't do a find-replace-all for /div> in an HTML file because everything would break. Needed to figure out how to deal with new lines. After a lot of head banging I figured out how to go.

    - Find and replace "div id .. \r\n"
    This is essentially the same as pressing backspace from behind the /div>.

    Now the text is:
    div id .. /div> \r\n"
    Now you can remove "div id .. /div>"

    Hope that helps.

    ReplyDelete
  4. This tool works fine on my test file that is 58 KB large, but it doesn't work on the file I want to process, which is 36 MB large.

    ReplyDelete
  5. dellfalconer, I routinely use it on files that are several GB in size. In fact, I'm watching it run on a file that's about 12GB right now. I use it daily on several files so I know it works just fine.

    While you probably aren't running out of disk space I wanted to point out that fart.exe will create a temporary file, _fart.~, in the same directory as the file you are operating on. This temp file will grow to about the same size as the original file, therefore you have to make sure to have at least as much free space on your volume as the file on which you are finding and removing text.

    ReplyDelete
  6. I have plenty of disk space. What I tried to convert was %% to \x0D\x0A. I had to enter %% as %%%% to get it to work on the small file, but it didn't convert anything on the large file. Anyway I solved my problem another way. My file transfer now outputs 0D0A directly, so I have no need to convert any more.

    ReplyDelete
  7. is there a way to replace text within two static pieces of text kinda like
    oldstring= "C:\Users\randomtext\Documents"
    newstring= "C:\Users\newtext\Documents"
    please any help would be amazing.

    ReplyDelete
    Replies
    1. Just use any substring you find convenient

      c:\path\>fart -C "\\randomtext\\" "\\newtext\\"

      Delete
  8. Just trying a simple example and can't get it to work - Please Help ?

    fart test.txt oldstring newstring

    => returns "Replaced 0 occurence(s) in 0 file(s)?

    ReplyDelete
  9. Is there any way in FART put many strings for finnd and replace in one line command?

    ReplyDelete
  10. Hi
    I am struggling with fart.exe to make modification to lines that includes quotation marks.

    Task:
    All commas embraced by quotation marks must be replaced with semicolon embraced by quotation marks.
    Like this:
    Before: ","
    After : ";"
    However standalone commas must not be touched.

    Example:
    Before: "Resistor","SMD RES 0,1W 0R"
    After : "Resistor";"SMD RES 0,1W 0R"

    Is this possible?

    Have tried with:
    C:\Fart.exe -c -r -i Test.txt "," ";"
    but it is not working. It also replaces the comma not embraced by quotation marks:
    "Resistor";"SMD RES 0;1W 0R"

    ReplyDelete
    Replies
    1. C:\Fart.exe -c -r -i Test.txt "\",\"" "\";\""

      Delete
  11. Try it with double quotes - fart.exe -c -r -i Test.txt "","" "";""

    ReplyDelete
    Replies
    1. Hi

      Already tried that with no luck.
      Also tried with triple quotes.
      Any other ideas?

      Delete
  12. No - already tried that.
    Double quotes doesn't work either.
    Neither do triple quotes.

    ReplyDelete
  13. This program has trouble with files that have commas in their names. IE this will not work: fart.exe "some file (with, commas).txt" [TEXT] [NEWTEXT]

    However you can do this and it will work: for /f "tokens=* delims= " %a in ('dir /b /a-d "some*.txt"') do fart.exe "*.txt" [TEXT] [NEWTEXT]

    ReplyDelete
  14. Thanks for the info Sudopinion!

    ReplyDelete
  15. In my file the accepted pattern for " is ,",. Whenever there is a " and its followed by some letter or preceded by some letter or any other symbol other than , I need to correct it. Any suggestions on how I can do it with Fart.exe is greatly appreciated. Thanks!

    ReplyDelete
  16. To remove " from a file you have to add a \ before the

    ReplyDelete
  17. Is there any idea to find the multiple words and replace the words with it's respective changes in same file using single fart command?

    FILE NAME:test.txt
    CONTENTS: This is a Pencil.It is used to write a content.

    I want to change this "Pencil to Eraser" and "write to erase" using single fart command.

    ReplyDelete
    Replies
    1. You could replace the phrase, "Pencil.It is used to write" with "Eraser.It is used to erase." Note: your example didn't have a space between Pencil and the period so this example doesn't either.
      Like this: fart.exe test.txt "Pencil.It is used to write" "Eraser.It is used to erase"

      Delete