返回顶部

收藏

Javascript 恢复windows系统中tasklist.exe的内容并排序

更多
Array.prototype.add = function ( item ) {
    this [ this.length ] = item;
}

Array.prototype.iterate = function ( callback ) {
    for ( var i = 0; i < this.length; i++ ) {
        callback ( this [ i ] );
    }
}

String.prototype.trimRight = function ( ) {
    return this.replace ( /^\s*/, "" );
}

String.prototype.trimLeft = function ( ) {
    return this.replace ( /\s*$/, "" );
}

String.prototype.trim = function ( ) {
    return this.trimRight().trimLeft();
}

String.prototype.padRight = function ( n ) {
    var retVal = this;
    for ( var i = this.length; i < n; i++ ) {
        retVal += " ";
    }
    return retVal;
}

String.prototype.padLeft = function ( n ) {
    var padding = "";
    for ( var i = this.length; i < n; i++ ) {
        padding += " "; 
    }
    return padding + this;
}

String.prototype.center = function ( n ) {
    if ( n < this.length ) {
        throw "Error in String.prototype.center: the string cannot be "
            + "longer than the span over which its centered.";
    }
    debugger;
    var leftPadding = ( n - this.length ) >> 1;
    //var rightPadding = n - leftPadding - this.length;
    return this.padLeft(leftPadding + this.length).padRight(n);
}

String.prototype.compareTo = function ( s ) {
    if ( s == null ) return -1;

    var s1 = this.trim().toUpperCase();
    var s2 = s.trim().toUpperCase();
    if ( s1 == s2 ) {
        return 0;
    }

    for ( var i = 0; i < Math.min ( s1.length, s2.length ); i++ ) {
        var n1 = s1.charCodeAt ( i );
        var n2 = s2.charCodeAt ( i );
        if ( n1 < n2 ) {
            return -1;
        } else if ( n1 > n2 ) {
            return 1;
        }
    }

    return ( s1.length > s2.length ) ? ( -1 ) : ( 1 );
}

var tasks = new Array ( );

tasks.parseLine = function ( line ) {
    return {
        ImageName : line.substr(0, 25).trim(),
        PID : new Number ( line.substr(26, 6 ).trim() )
    };
}

tasks.addLine = function ( line ) {
    this.add ( this.parseLine ( line ) );
}

tasks.sortItems = function ( ) {
    this.sort ( 
        function ( item1, item2 ) {
            return item1.ImageName.compareTo ( item2.ImageName );
        }
    );
}

var oShell = WScript.CreateObject("WScript.Shell");
var oExec = oShell.Exec ( "tasklist.exe" );
var oStream = oExec.StdOut;
while ( ! oStream.AtEndOfStream ) {
    var currentLine = oStream.ReadLine();
    if ( currentLine.search ( /\=/ ) == 0 
        || currentLine.search ( /Image\sName/ ) == 0 ) {
        continue;
    }
    tasks.addLine ( currentLine );
}
oStream = null;

tasks.sortItems ( );

WScript.Echo ( "Image Name".center ( 25 ) + "PID".center ( 6 ) );

tasks.iterate ( 
    function ( item ) {
        if ( item.ImageName != null && item.ImageName.length > 0
            && item.PID != 0 ) {
            WScript.Echo ( item.ImageName.padRight(25) + item.PID.toString().padLeft(6) );          
        }
    }
)

标签:Javascript,恢复,tasklist.exe

收藏

0人收藏

支持

0

反对

0

发表评论