Javascript 根据文件的跟踪编号重命名目录下的音乐文件
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 ] ); } } DirectoryUtility = { _fso : WScript.CreateObject ( "Scripting.FileSystemObject" ), // function getCurrentDirectory: returns currentDirectory path // with a trailing backslash. getCurrentDirectory : function ( ) { var scriptFullName = WScript.ScriptFullName; var scriptName = WScript.ScriptName; return scriptFullName.substr ( 0, scriptFullName.lastIndexOf ( scriptName ) ); }, getFiles : function ( path, re ) { var folder = this._fso.GetFolder ( path ); var files = folder.Files; var filteredFiles = new Array ( ); for ( var e = new Enumerator ( files ); ! e.atEnd ( ); e.moveNext () ) { var file = e.item ( ); if ( re.test ( file.Name ) ) { filteredFiles.add ( file ); } } return filteredFiles; } } var du = DirectoryUtility; var path = du.getCurrentDirectory ( ); var args = WScript.Arguments; var pattern; if ( args.Length > 0 ) { pattern = "\\." + args ( 0 ) + "$"; } else { WScript.Quit ( ); } var re = new RegExp ( pattern ); var files = du.getFiles ( path, re ); var printName = function ( fileName ) { WScript.Echo ( fileName ); } var rename = function ( file ) { WScript.Echo ( "Specify a track number for \"" + file.Name + "\": " ); var line = WScript.StdIn.ReadLine ( ); var track = new Number ( line ); if ( track < 10 ) { newName = "0" + track; } else { newName = track.toString ( ); } newName += " " + file.Name; file.Name = newName; } files.iterate ( rename ); /* 用法 */ cscript .js extension