var shell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
if( /wscript\.exe$/i.test( WSH.FullName ) ) {
shell.Run( [
"cscript //nologlo",
"\"" + WSH.ScriptFullName + "\""
].join(" ") );
WSH.Quit();
}
Object.extend = function(dest, src) {
for(var key in src) {
dest[key] = src[key];
}
return dest;
}
var $continue = {};
var $break = {};
var Enumerable = {
each : function(iterator) {
var index = 0;
try {
this._each( function(value) {
try {
iterator( value, index++ );
} catch(e) {
if( e != $continue ) throw e;
}
} );
} catch(e) {
if( e != $break ) throw e;
}
},
map : function(iterator) {
var result = [];
this.each( function(item, index) {
result.push( iterator(item, index) );
} );
return result;
},
findAll : function(iterator) {
var result = [];
this.each( function(item, index) {
if( iterator(item, index) ) result.push( item );
} );
return result;
},
find : function(iterator) {
var result = null;
this.each( function(item, index) {
var r = iterator( item, index );
if( r ) {
result = item;
throw $break;
}
} );
return result;
},
toArray : function() {
var result = [];
this.each(function(item) {
result.push( item );
} );
return result;
}
};
Object.extend( Array.prototype, Enumerable );
Array.prototype._each = function(iterator) {
for(var i = 0; i < this.length; i++) {
iterator( this[i] );
}
};
Object.extend( Enumerator.prototype, Enumerable );
Enumerator.prototype._each = function(iterator) {
for(this.moveFirst(); ! this.atEnd(); this.moveNext()) {
iterator( this.item() );
}
};
var console = {
echo : function(msg) {
if( msg == null ) msg = "";
if( msg instanceof Array ) {
msg.each( function(item) {
console.echo( item );
} );
return;
}
WSH.StdOut.WriteLine( String(msg) );
},
print : function(msg) {
if( msg == null ) msg = "";
if( msg instanceof Array ) {
msg.each( function(item) {
console.print( item );
} );
return;
}
WSH.StdOut.Write( String(msg) );
},
readLine : function(msg) {
if( msg ) console.print( [ msg, " > " ] );
return WSH.StdIn.ReadLine();
},
execute : function(cmd) {
var exec = shell.Exec( cmd );
while( exec.Status == 0 ) {
WSH.Sleep( 100 );
}
console.echo( exec.StdOut.ReadAll() );
}
};
Object.extend( this, console );
shell.CurrentDirectory = fso.GetFile( WSH.ScriptFullName ).ParentFolder.Path;
var targets = new Enumerator( fso.GetFolder( shell.CurrentDirectory ).Files ).findAll( function(fileInfo) {
// 拡張子でフィルタリング
return /\.netsh$/i.test( fileInfo.Name );
} );
while( true ) {
echo( [
"適用する設定を選択してください。(0 で終了)",
"----",
targets.map( function(item, index) {
return ( index + 1 ) + " : " + item.Name;
} )
] );
var n = parseInt( readLine( " " ) );
if( n == 0 ) break;
if( n < 1 || n > targets.length ) continue;
execute( [
"netsh", "exec", "\"" + targets[n - 1].Path + "\""
].join(" ") );
execute( "ipconfig /all" );
break;
};
ちょっと長いのだが、FSOのループ処理用にEnumeratorを拡張するためだけにprototype.jsのeach系メソッドやObject.extendの定義をしているので、単純ループに置き換えて短くすることもできる。
んで、なにやってるかっていうと、
セコメントをする