This project has moved. For the latest updates, please go here.

Command line

For command line parsing the following popular library was used:
http://commandline.codeplex.com/

Usage

fnr.exe --cl --find "Text To Find" --replace "Text To Replace" --caseSensitive --dir "Directory Path" --fileMask "*.txt" --includeSubDirectories

Flags

--cl Required to run on command line.
--find Required. Text to find.
--replace Replacement text. If not specified only find operation is performed.
--useRegEx Use regular expressions in 'find' parameter.
--useEscapeChars Use escape chars like '\n' in 'find' and 'replace' parameters.
--caseSensitive Case Sensitive.
--dir Required. Directory path.
--includeSubDirectories Include files in SubDirectories.
--fileMask Required. File mask or multiple file masks separated by comma.
--excludeFileMask File masks to exclude. If multiple - separate by comma
--help Dispaly help screen.
--showEncoding Displays detected encoding for each file with matches.
--silent Supress the command window output.
--logFile Path to log file where to save command output.
--skipBinaryFileDetection Ignore detection of binary files.
--alwaysUseEncoding Skip encoding detection and always use specified encoding.
--defaultEncodingIfNotDetected If encoding is not detected in some very rare cases, use this one.
--includeFilesWithoutMatches Include files without matches in results.
--setErrorLevelIfAnyFileErrors Return ErrorLevel 2 if any files have read/write errors.



Error Levels

ErrorLevels are:
0 - No errors
1 - Major error like directory is invalid
2 - Read/Write error for any processed file

By default only 0 and 1 are returned by the command. To enable ErrorLevel 2, use new flag --setErrorLevelIfAnyFileErrors

Regular Expressions

.NET RegEx engine is used for regular expressions. It is described here:
http://msdn.microsoft.com/en-us/library/hs600312(v=vs.110).aspx

"In the .NET Framework, regular expression patterns are defined by a special syntax or language, which is compatible with Perl 5 regular expressions and adds some additional features such as right-to-left matching."

Here is a link to Regular Expression Language - Quick Reference in msdn:
http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Encoding detection

fnr.exe uses two approaches two detect file encoding:

  • First KlerkSoft Bom Detector which is streamlined version of code from here:

http://www.architectshack.com/TextFileEncodingDetector.ashx

  • If Bom Detection fails it uses Microsoft's MLang library using approach described here:

http://www.codeproject.com/Articles/17201/Detect-Encoding-for-In-and-Outgoing-Text

In some very rare cases the detection may fail or may report a false positive. The reason for it failing is that detecting correct file encoding 100% of the time is impossible. MLang uses heuristics to go through file content and give its best guess on encoding. Usually its guess is correct 99%+ of the time. The only sure way to load a file with correct encoding is to know it upfront and pass it in.

If you have a case where encoding detection fails for a specific file, you will get error message "Could not detect file encoding." for that file.

To get around this error there are several approaches:

  1. You can open the problematic file in Notepad and save it using Unicode encoding. This will add a BOM header to the file so it will be easily detected by KlerkSoft Bom Detector
  2. Use --defaultEncodingIfNotDetected or --alwaysUseEncoding to bypass automatic detection. See details for these flags in the table above.


For flags --defaultEncodingIfNotDetected and --alwaysUseEncoding you can use any encoding listed in the following web page. Just scroll down to example results and look for values in column 'identifier and name':

http://msdn.microsoft.com/en-us/library/system.text.encoding.getencodings(v=vs.110).aspx

Special characters

Escape new line and quote characters using \n and \". If there is actual text "\n" in find or replace text, you can use [NewLine] for new lines instead of "\n" (starting v 1.7)

Using accented characters in BAT Files

By default when you run DOS commands, the values in arguments are limited to code page 437. This code page is the character set of the original IBM PC (personal computer), or MS-DOS. It is pretty old and doesn't support a lot of accented chars like "ó" and many others.

To pass chars that are not supported by code page 437 in find/replace text or other arguments, please use information in the following links:

How to write batch file that has accented chars:
ur:http://stackoverflow.com/questions/1427796/batch-file-encoding/1427817

View UTF-16 or other encodings in DOS prompt
http://stackoverflow.com/questions/10764920/utf-16-on-cmd-exe

One more tip is to make sure you save your BAT file in UTF-8 or other format that displays the chars correctly. Don't use ASCII.

Last edited Nov 13 at 9:09 PM by zzzprojects, version 42

Comments

AgentFriday Mar 5, 2016 at 11:49 PM 
The options (flags) are excessively long, you have to specify --cl --dir and --filemask every time (there is no reasonable default, like * in current directory?), and the --dir must be a FULL PATH?!?!

Simply unusable as a command-line tool, in my opinion.

mrT33 Dec 2, 2015 at 11:33 AM 
Hello, great little tool, thank you :)
I would just like to know if it is possible to do multiple finds and replaces by using command line? And if so how would I do this. Thanks again.

AWEInCA Jul 31, 2015 at 7:16 PM 
Awesome tool. Use it all the time. Thank you. It would be nice to have an option to include replacing text in the file name as well as the contents?

ericpopivker Mar 4, 2014 at 12:06 PM 
@bugmenot2 please try latest version - 1.5.1 BETA. You shouldn't have to click enter to end process and fnr.exe --cl --help now works. If you still encounter an issue please file it in Issues section.

bugmenot2 Feb 17, 2014 at 7:45 AM 
echo "" | fnr.exe <Command line parameters> doesn't work!

bugmenot2 Feb 9, 2014 at 1:52 PM 
Great tool but unfortunately the command line have bug! Process doesn't end. If i type:
fnr.exe --cl --help --test
it will always stay in task manager until you manually kill it!
And it requires a keypress to end the job! :(

bugmenot2 Feb 9, 2014 at 1:46 PM 
At the end of any execution in command line mode it requires a keypress to end process!!!
I have 1.5 version on WIn 7 x64.
Any workaround ?!

bugga360 Aug 17, 2013 at 5:05 AM 
Is there a way to use a relative path for --dir in command line? I have created a batch file and run it on a usb stick (for files within the usb stick). If my drive letter changes, the directory path breaks. Thanks!!!

ericpopivker Feb 12, 2013 at 12:28 PM 
Silent switch added in 1.2 --silent

munsingh Jul 23, 2012 at 12:45 PM 
Does it have a silent switch such that it does not print anything on the console when running in the command line mode? If not can you add it?

munsingh Jul 23, 2012 at 12:31 PM 
This issue exists with the 1.2 beta as well. The workaround is:

echo "" | fnr.exe <Command line parameters>

ericpopivker Mar 9, 2012 at 4:34 PM 
this issue has been fixed as of v1.0

tmwes Feb 9, 2012 at 1:59 PM 
This little program is exactly what I was looking for. One issue; when using it within a batch file, the batch file now requires a keypress after the fnr.exe is done running before it moves on to the next step. Any command line options to remove the pause?