用JS给XP的右键加上“打开文件位置”

  • 发表于
  • 周边

在WIN7中有了这个功能,并且感觉很好用,但在XP中没有这样的功能,以前都是用BAT实现的,今天扔个JS实现的:

function alert( aa )
{
WScript.Echo(aa);
}
var ajscontent = 
"function ShowLinkFileGTarget( linkfile ) "+
"{"+
" var WshShell = WScript.CreateObject('WScript.Shell');"+
" var oShellLink = WshShell.CreateShortcut( linkfile );"+
" WshShell.Run('explorer.exe /n ,/select ,' +oShellLink.TargetPath, 1);"+
" }"+
" var objArgs = WScript.Arguments;"+
"if( objArgs.length>0 )"+
" ShowLinkFileGTarget( objArgs(0) );";

var fso = new ActiveXObject("Scripting.FileSystemObject");
var WshShell = WScript.CreateObject("WScript.Shell");
var programDir = "c:\\program files";

if( !fso.FolderExists( programDir ) )
fso.CreateFolder( programDir );

var dstDir = fso.BuildPath( programDir, "locatelinkfile");

if( !fso.FolderExists( dstDir ) )
fso.CreateFolder( dstDir );

var dstFile = fso.BuildPath( dstDir, "a.js" );

var textStream = fso.OpenTextFile( dstFile, 2, true );
textStream.WriteLine( ajscontent ); //end create js file

//write the register
var regValue = 'wscript.exe "'+ dstFile + '" "%1" ';
WshShell.RegWrite ("HKCR\\lnkfile\\shell\\打开文件位置\\command\\", regValue );

由于使用了注册表写入操作,所以一些防御软件会提示恶意程序,这里就不讲了,代码清楚!