Wednesday, July 28, 2010

Reversing the Order of Lines in a File


//
// reversor.js
//
// Reverses order of lines in a file
//
// Execute on Windows as:
// cscript reversor.js input.txt > output.txt
// ------------------------------------------------

var args = WScript.Arguments;
if(args.length != 1)
WScript.Quit();

// don't use square brackets below...
// args is not an array/dictionary
var in_file = args(0);

var f;
var lines = new Array();
var fso = new ActiveXObject("Scripting.FileSystemObject");

var i = 0;
var fin = fso.OpenTextFile(in_file);

while(!fin.AtEndOfStream)
lines[i++] = fin.ReadLine();
for(i = lines.length - 1; i >= 0; --i)
WScript.StdOut.WriteLine(lines[i]);

fin.Close();

No comments:

About Me

My photo
C/C++ Programmer doing CAD on Windows. Some web development experience. Bangalorean.

Blog Archive

Labels