When analyzing plain-text log files such as the Web server logs or the Sfc, or DISM log files, you may sometimes prefer the most recent items appearing at the top. However, most of the log files that Windows generates would have the oldest entries listed first — entries listed in chronological order.
You may be wondering how to reverse the lines in a log file (or any plain-text file, for that matter) so that the newest items appear at the top. You can reverse the lines in a text file using several methods, of which some of them are covered in this article.
Reverse a text file in Windows
Note: As always, before proceeding, be sure to backup the original text file before manipulating it — e.g., using find/replace, or running a script or macro against a text file.
Reverse a Text File Using Different Ways in Windows
Method 1: Using online portals (free)
There are several free online services to reverse the order of the lines in a text file for you. Here is a couple of useful sites:
Sort Text Lines - Text Mechanic: https://textmechanic.co/Sort-Text-Lines.html Big File Tool - Sort Lines: https://textmechanic.co/Big-File-Tool-Sort-Lines.html Reverse Lines: https://www.miniwebtool.com/reverse-lines/
Method 2: Using PowerShell
To reverse the order of the lines in a text file using PowerShell, use this command-line syntax from a PowerShell window:
$x = Get-Content -Path "C:\Users\ramesh\Desktop\dism.log"; Set-Content -Path "C:\Users\ramesh\Desktop\dism.log" -Value ($x[($x.Length-1)..0])
Method 3: Using VBScript
Make a VBScript file from the following contents. To do so, open Notepad, copy the following lines into it and save the file with .vbs extension — e.g. reverse_text_file.vbs
'Script to reverse the lines in a text file. Takes a text file name as the argument. If Wscript.arguments.count = 0 Then wscript.quit Dim sFileName sFileName = Wscript.arguments(0) Const ForReading = 1 Const ForWriting = 2 Const TriStateTrue = -1 Dim arrLines() i = 0 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(sFileName, ForReading, TriStateTrue) Do Until objFile.AtEndOfStream ReDim Preserve arrLines(i) arrLines(i) = objFile.ReadLine i = i + 1 Loop objFile.Close Set objFile = objFSO.OpenTextFile(sFileName, ForWriting, TriStateTrue) For i = Ubound(arrLines) To LBound(arrLines) Step -1 objFile.WriteLine arrLines(i) Next objFile.Close Wscript.echo "Done"
To run the VBScript file, use the following command-line syntax:
cscript //nologo reverse_text_file.vbs "d:\logs\somefile.txt"
or
wscript reverse_text_file.vbs "d:\logs\somefile.txt"
Alternately, drag the text file on the VBScript file to reverse the text file contents.
You may also place a VBScript shortcut to the Send To folder for easier access. In the Send To folder of your user account, create a shortcut to the VBScript with wscript.exe prefix — e.g. wscript.exe d:\reverse-text-file.vbs
and customize its icon and shortcut caption.
Method 4: Using Notepad++
To reverse the lines in a text file using Notepad++, first, you’ll need to add serial number prefix for each line. Then reverse the lines (lexicographically descending), and then finally remove the serial numbers. Follow these steps:
- Open the text file using Notepad++
- Select all the contents via Edit → Select All
- From the Edit menu, select Column Editor
- Select the Number to Insert radio button.
- Set Initial number: to
1
- Set Increase by: to
1
- Enable Leading zeros, and click OK. This adds the line number or the serial number before each line.
- From the Edit menu, click Line Operations → Sort Lines Lexicographically Descending.
The lines in the text file are now reversed. The total number of lines in this example is17612
(i.e.5
digits). Now, you’ll need to remove the first five characters (Sl. no) in each line. - Go to the 1st line in the text file, and press Ctrl + H to launch the Find & Replace dialog.
- In the Find what: text box, type
^.{5}(.*)$
- In the Replace with: box, type
$1
- Set the Search Mode to Regular expression
- Click Replace All. That’s it. The first five characters are removed.
- Save the file and close Notepad++.
This reverses the lines in your text file!
One small request: If you liked this post, please share this?
One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:- Pin it!
- Share it to your favorite blog + Facebook, Reddit
- Tweet it!
Thank you! Thank you! Thank you!
The reverse-text-file.vbs script was exactly what I needed for my project!