以编程方式或从Windows中的命令行显示文件属性对话框

时间:2014-05-21 10:06:54

标签: windows winapi

资源管理器,右键单击单个文件,属性...

显示文件属性对话框。

我希望有一个直接的方法可以从命令行执行此操作,这解释了我完全没有研究如何使用win api进行此操作:)

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。我从提供的链接中汇总了这个脚本。可能有更直接的方式 - 这有一种类似弗兰肯斯坦的光环,但它有效。缺点是有一个hack来保持对话框打开(睡眠脚本),可以将其更改为两种不同的方法。该脚本将执行任何动词,并列出动词。

用法:将脚本保存为shareGUI.js,使用cscript shareGUI.js <args>

进行调用

示例:

cscript shareGUI.js C:\path\to\file.txt将列出动词

cscript shareGUI.js <verb> C:\path\to\file.txt将执行给定的动词

cscript shareGUI.js "Properties" C:\path\to\file.txt将显示属性对话框

以粗体显示通常的免责声明。

var args = WScript.Arguments;
var usage="Usage: cscript shareGUI.js [verb] <fullpath>";
useage+="\n Function: Executes given verb on given file";
useage+="\n           or lists available verbs";
useage+="\n Notes: Ampersands in property names are remove in listing";
useage+="\n        Ampersands are not required when specifying verb";
useage+="\n Arguments: ";
useage+="\n   verb (optional): Name of verb to be executed, case sensative";
useage+="\n                    if not provided, verbs are listed";
useage+="\n   fullpath: FULLPATH Name of file or directory";
//
if ((args.Unnamed.length == 0) || (args.Named.Exists("?"))) {
  QuitMsg(usage,0);
}
if (args.Unnamed.length > 2) {
  QuitMsg(usage,0);
}
//
var tgt_;
var verb_;
if (args.Unnamed.length == 1) {
  tgt_=args.Unnamed.Item(0);
  verb_ = "";
}else if (args.Unnamed.length == 2) {
  verb_ = args.Unnamed.Item(0);
  tgt_=args.Unnamed.Item(1);
}
//
var shellApp = new ActiveXObject("Shell.Application");
QuitIfNull(shellApp,"System Error:Could not get Shell.Application ActiveXObject",2);
//
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
QuitIfNull(objFSO,"System Error:Could not get Scripting.FileSystemObject ActiveXObject",2);
//
var WSHshell = new ActiveXObject("Wscript.Shell");
QuitIfNull(WSHshell,"System Error:Could not get Wscript.Shell ActiveXObject",2);
//
var doList=verb_=="";
//
var isDir=objFSO.FolderExists(tgt_);
if (isDir) {
  DoForFolder();
}else{
  DoForFile();
}
WScript.Quit(0);// EXIT OK
// functions...
// redundant code prequel
function DoForFile(){
  var objFile = objFSO.GetFile(tgt_);
  QuitIfNull(objFile,"Error:Path \""+tgt_+"\" Not Found",3);
  //
  var folder = shellApp.NameSpace(objFSO.GetParentFolderName(objFile));
  QuitIfNull(folder,"System Error:Could not get parent folder name of \""+tgt_+"\"",3);
  //
  var folderItem=folderItemFromFolder(tgt_,folder);
  QuitIfNull(folderItem,"System Error:Could not folder item from parent folder of \""+tgt_+"\"",3);
  //
  if (doList) {
    listItemVerbs(folderItem);
    return;
  }
  var folderItemVerb=folderItemVerbFromFileItem(verb_,folderItem);
  QuitIfNull(folderItemVerb,"System Error:Could not folder item verb from folder item \""+tgt_+"\"",3);
  //
  runTheVerb(folderItemVerb);
}
// redundant code redux
function DoForFolder(){
  var folder = shellApp.NameSpace(tgt_);
  QuitIfNull(folder,"System Error:Could not folder of \""+tgt_+"\"",2);
  //
  var folderItem=folder.Self;
  QuitIfNull(folderItem,"System Error:Could not folder item of \""+tgt_+"\"",2);
  //
  if (doList) {
    listItemVerbs(folderItem);
    return;
  }
  var folderItemVerb=folderItemVerbFromFileItem(verb_,folderItem);
  QuitIfNull(folderItemVerb,"System Error:Could not folder item verb from folder item \""+tgt_+"\"",3);
  //
  runTheVerb(folderItemVerb);
}
// HACK!! this part sucks, have to remain open else dialog dies
function runTheVerb(verb){
  verb.DoIt();
  WSH.Sleep(5000);
  var focus_way=false;
  if (focus_way) {
    while (WSHshell.AppActivate("Properties")) {WSH.Sleep(2000)};
  }else{ // forever way
    WSH.Sleep(60*60*1000);// sleep for 60 minutes !
  }
}
// match path and return folder item
function folderItemFromFolder(path,folder){
  var folderItems = new Enumerator(folder.Items());
  for (; ! folderItems.atEnd(); folderItems.moveNext()) {
    var folderItem  = folderItems.item();
    if (folderItem.Path==tgt_) {
      //WScript.Echo("tgt_:"+folderItem.Path);
      //WScript.Quit(0);
      return folderItem
    }
  }
  return null;
}
// match verb name
function folderItemVerbFromFileItem(verb,folderItem){
  verb=verb.replace('&','');
  var folderItemVerbs = new Enumerator(folderItem.Verbs());
  for (; ! folderItemVerbs.atEnd(); folderItemVerbs.moveNext()) {
    var folderItemVerb  = folderItemVerbs.item();
    //WScript.echo("Verb:"+folderItemVerb );
    var fix_n=folderItemVerb.Name.replace('&','');
    if (fix_n == verb) {
      return folderItemVerb;
    }
  }
  return null;
}
// echo the verbs, ignore blanks, remove ampersands
function listItemVerbs(folderItem){
  var folderItemVerbs = new Enumerator(folderItem.Verbs());
  for (; ! folderItemVerbs.atEnd(); folderItemVerbs.moveNext()) {
    var folderItemVerb  = folderItemVerbs.item();
    var fix_n=folderItemVerb.Name.replace('&','');
    if (fix_n!="") {
      WScript.echo(""+fix_n );
    }
  }
}
function QuitIfNull(who,why,how){
  if (who==null) {
    QuitMsg(why,how);
  }
}
function QuitMsg(why,how){
  WScript.Echo(why);
  WScript.Quit(how);
}