batch to reverse my output file

I had this question after viewing batch findtext match 2 criteria.
simple question can i reverse the output so the 1st line is last

My output file looks like
02/01/18 10:11:55 ( 0) Fire Alarm      3          DS-5382 XXXXXXXXXX
02/01/18 10:12:38 ( 0) Fire Alarm      2          DS-3682 XXXXXXXXXX
02/01/18 10:12:38 ( 0) Fire Alarm      4          DSA-3234 XXXXXXXXXX
02/01/18 10:14:16 ( 0) Panic Alarm     2          DS-3382 WUBBLU, DO

i would like it to look like
02/01/18 10:14:16 ( 0) Panic Alarm     2          DS-3382 WUBBLU, DO
02/01/18 10:12:38 ( 0) Fire Alarm      4          DSA-3234 XXXXXXXXXX
02/01/18 10:12:38 ( 0) Fire Alarm      2          DS-3682 XXXXXXXXXX
02/01/18 10:11:55 ( 0) Fire Alarm      3          DS-5382 XXXXXXXXXX
LVL 1
csePixelatedAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
You can do that same basic thing in DOS commands if what you want is just to reverse the order of the lines.  Something like:

(for /f "tokens=1* delims=]" %%A in ('find /v "" /n^<input.txt^|sort /r') do echo %%B)>output.txt

Open in new window

Reads from input.txt, reverses the order of the lines, and writes to output.txt.


»bp
0
 
Bill PrewCommented:
You can try sorting it, using the /R option for reverse order.  Assuming that's what you are after.  But keep in mind that will be a simple sort, of the date as you have it and time, so when the month changes from 12 to 01 in the middle of the data (if it does) then it could break down.

If you really need a chronological sort (or reverse chronological) then your records would need to start with something like YYYYMMDDhhmmss or YYYY/MM/DD hh:mm:ss, where a text sort of that data would result in the proper ordering.

See SORT /? for info on the sort command.


»bp
0
 
NVITConnect With a Mentor Commented:
If you want it to work without sorting, i.e. keep the true reversed order, you can use this vbscript method:
This is especially faster with big text files.

Save the code to a filename Backwards.vbs

' Read a Text File from the Bottom Up

Dim arrFileLines()
fname = WScript.Arguments.Item(0)
i = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(fname, 1)

Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop

objFile.Close

For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    Wscript.Echo arrFileLines(l)
Next

Open in new window


Run it in a CMD window with an original filename big.txt. Output results to big_reversed.txt
cscript //nologo Backwards.vbs big.txt>big_reversed.txt

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.