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
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/
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])
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.
'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"
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.
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
- Set Increase by: to
- 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 is
5digits). 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
- In the Replace with: box, type
- 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!