Site icon Winhelponline

How to Flip or Reverse a Text File Using Different Ways in Windows

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.

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:
Big File Tool - Sort Lines:
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
Set objFile = objFSO.OpenTextFile(sFileName, ForWriting, TriStateTrue)
For i = Ubound(arrLines) To LBound(arrLines) Step -1
   objFile.WriteLine arrLines(i)
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.

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:

  1. Open the text file using Notepad++
  2. Select all the contents via Edit → Select All
  3. From the Edit menu, select Column Editor
  4. Select the Number to Insert radio button.
  5. Set Initial number: to 1
  6. Set Increase by: to 1
  7. Enable Leading zeros, and click OK. This adds the line number or the serial number before each line.
  8. From the Edit menu, click Line OperationsSort Lines Lexicographically Descending.

    The lines in the text file are now reversed. The total number of lines in this example is 17612 (i.e. 5 digits). Now, you’ll need to remove the first five characters (Sl. no) in each line.
  9. Go to the 1st line in the text file, and press Ctrl + H to launch the Find & Replace dialog.
  10. In the Find what: text box, type ^.{5}(.*)$
  11. In the Replace with: box, type $1
  12. Set the Search Mode to Regular expression
  13. Click Replace All. That’s it. The first five characters are removed.
  14. Save the file and close Notepad++.

This reverses the lines in your text file!

Exit mobile version