Change Console Title with vbscript

2019-03-01 08:39发布

问题:

is there a way to change the cmd title? I wrote a vbs program. But the dos title is bad.

The name ist c:\windows\system32\cscript.exe I try it with:

title the_name and title ="name"

But both doesn't works.

Thanks for help.

回答1:

Unfortunately you cannot do that from within the script using any of the WSH objects.

The only way to do it is to launch the script via an intermediary (a .bat using the TITLE command or another script using a %comspec% argument).



回答2:

@AlexK The link you point to actually shows how you CAN change the title of the command window so I'm not sure why that doesn't work as a solution to Matthias's problem.

@Matthias - since you are already using cscript you have a couple of options:

  1. You can create a parent script that opens multiple windows with unique titles like so:

  var x = new ActiveXObject("WScript.shell");

    for (var i=0; i < 5; i++) {
        x.run('cmd title your title ' + i + '| cscript.exe "params"');
    }
  

or just make the parent script execute one child script but prompt for the title before it runs the cscript command like so:


  var x = new ActiveXObject("WScript.shell");
      x.run('cmd title your title ' + 
            WScript.StdIn.ReadLine() + 
            '| cscript.exe "script path and params"');
  
  1. You can also use the "start" command from an existing command prompt, and just change the value of the title each time you run the script
c:\>start "your title" cscript script_path.vbs
  1. Or you could use the windows api to change the title programmatically using a custom activex object. This is definitely a little more in-depth of a solution but you can create an activex object using C# that can make calls to the windows api, and execute the C# function from vbscript using COM. You'll want to use the findWindow and SetWindowText api calls to change the title of the CMD window.

Check out the IEUnit project, specifically the Win32Dom activex object. It's a good project to start from because it solves the "how to create a c# activex object" and "how to call the win32 api" questions you might have for this option. And it already has the findWindow portion finsihed for you.

http://code.google.com/p/ieunit/source/browse/#svn%2Ftrunk%2Ftool%2FWin32Dom



回答3:

Do you need to change it in the code or do you just want it to look a little nicer? You could possibly do it by creating a shortcut to your script and then changing the name of the shortcut (in the properties of the shortcut, change to the General tab and change the name there).



标签: vbscript wsh