Last Updated on Thursday, 19 July 2012
Prefixing lines with their respective line number in a text file using a Microsoft Windows Operating System... I've just googled this as I couldn't remember how I did this last time and a number of people showing off their MS-DOS batch skills have proposed stupid extensive solutions when all you need is one command-line.
Working with programming languages, I often need to write the accompanying documentation. Within the documentation, I may want to refer to a line of code within a text file. I also find myself copying amounts of code into the same document and then needing lines prefixed so that I can explain the code.
Change contents of "original_file.txt"
- The first line of my code
- The second line of my code
- The third line of my code
- 1: The first line of my code
- 2: The second line of my code
- 3: The third line of my code
- Open a MS-DOS Command Prompt (Start > Run... > Type "COMMAND" > Enter)
- [Optional] Browse to the folder that contains the file (or use full path in path reference). This example assumes the file is in C drive (C:\).
- Create an empty text file (Type "ECHO > TEMP.TXT")
- Type "FC /N /LB 300 c:\original_file.txt c:\temp.txt > c:\results_file.txt"
- The resulting file will be "c:\results_file.txt", open it in your favorite text editor... ta-daa...
- FC is a file comparison tool in MS-DOS
- /N tells the comparison tool to prefix each line with its respective number
- /LB 300 tells it to buffer 300 lines (default will show first 100 lines - change this to the number of lines or greater in your file)
- original_file.txt is your original file (obviously change the name to your file)
- temp.txt is just a dummy file which should be empty for clarity
- results_file.txt is the resulting file, it will be created in the current working directory.
Above and Beyond: Add to your Context Menu
My absolute laziness drives me to write the instructions on adding this to the context menu (right-click menu) when you right-click on a text file:
- Open the system registry (Start > Run... > Type "REGEDIT" > OK)
- Navigate to My Computer\HKEY_CLASSES_ROOT\txtfile\shell
- Right-click on "shell" and create a new key with the action name (eg. "Prefix_with_line_numbers")
- Right-click on this new key and add a key to this one called "command"
- Under the "command" key, double-click the "(Default)" string in the right-pane and type the following c:\windows\system32\cmd.exe /c "ECHO > c:\temp.txt | FC /N /LB 1000 %L c:\temp.txt > c:\results_file.txt | start c:\results_file.txt"
- Right-clicking on a text file now and selecting this option will generate a c:\results_file.txt with the line numbers prefixed and open it; the 1000 is the number of lines but you can increase/decrease this to the length of the files you usually do this to (put a higher number than the maximum length [=number of lines] of the files you will be using this function on).
- -- yields
- Comparing files H:\temp.txt and C:\TEMP.TXT
- ***** H:\temp.txt
- 1: This is line 1
- 2: This is line 2
- 3: This is line 3
- ***** C:\TEMP.TXT
- 1: ECHO is on.
- You could change c:\results_file.txt to %TEMP%\results_file.txt to send the file to your temporary directory instead of using the root c drive.
- If you do not have c:\windows folder than find where "cmd.exe" exists and chage the path in the above instructions accordingly.
- The use of some environment variables (eg. %ComSpec% or %windir%) in the command may cause an "Access Denied" error on some networks. Use the hardcoded path.
Visitors of the World
- Wed 25-Mar-15
- Tue 24-Mar-15
- Mon 23-Mar-15
- Sat 21-Mar-15
- Mon 23-Mar-15