简体   繁体   English

Windows 批处理 cmd 文件“标题”命令不起作用

[英]Windows batch cmd file "title" command not working

I wanted to change cmd window title bar's title that is ran from batch file, but apparently the title command is not working on batch (.cmd) files.我想更改从批处理文件运行的 cmd window 标题栏的标题,但显然标题命令不适用于批处理(.cmd)文件。

As I googled and read stackoverflow, I found there are three possible commands that might solve the problem, but none of them worked so far for me:当我用谷歌搜索并阅读 stackoverflow 时,我发现有三个可能的命令可以解决这个问题,但到目前为止,它们都没有对我有用:

start "<title>" # => this starts a new shell window, not a solution
system "title \"Window title\""; # => "system" not found 
title cmdtitle # => this works on a manually opened cmd window (win-R -> cmd) but does not work on batch files

So, what's wrong with this?那么,这有什么问题呢?

(edit) (编辑)

As shown below, "title [title]" does not work for some reason...如下图,“title[title]”由于某种原因不起作用……

在此处输入图像描述

So, as I got an idea what was wrong thanks to @Mofi, here are the workaround ways to solve the problem I tried.因此,由于@Mofi,我知道出了什么问题,这里是解决我尝试过的问题的解决方法。

-- The cause: ——原因:

The cause of this problem is the "someprogram.cmd --option whatever" lines on the batch file.此问题的原因是批处理文件上的“someprogram.cmd --option 不管”行。 On the other cmd program file, there should be some "title" lines that overrides the previously defined title line.在另一个 cmd 程序文件上,应该有一些“标题”行覆盖先前定义的标题行。

-- The solve ——解决办法

While I'm noob to cmd script and feel frustrating about this title behavior(the title can be easily overridden and apparently it's kind of a customary coding - like this case nodejs - ), actually there's no way around but need to take some extra efforts to this issue, here are the workaround ways虽然我对 cmd 脚本不熟悉,并且对这个标题行为感到沮丧(标题很容易被覆盖,而且显然它是一种习惯编码 - 就像这种情况下 nodejs - ),但实际上没有办法,但需要采取一些额外的努力对于这个问题,这里有解决方法

  1. modify the child cmd program file(in this case node.js generated file) manually手动修改子 cmd 程序文件(在本例中为 node.js 生成的文件)

In this case, the node.js cmd program inside was like this below, and this specific line defines the "title" and causing override it so modify the line.在这种情况下,node.js cmd 程序内部如下所示,该特定行定义了“标题”并导致覆盖它,因此修改该行。

(default) (默认)

endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%"  "%dp0%\node_modules\node-cron-cli\croncli" %*

(to eg) (例如)

endLocal & goto #_undefined_# 2>NUL || title croncli.cmd & "%_prog%"  "%dp0%\node_modules\node-cron-cli\croncli" %*
  1. override the title within the last-child program覆盖最后一个子程序中的标题

From the premise where the title will be the last-defined one whenever, the most effective way should be defining "title" on the very last of programs, namely, the main node.js program (in this case).在任何时候标题都是最后一个定义的前提下,最有效的方法应该是在最后一个程序上定义“标题”,即主 node.js 程序(在这种情况下)。

This will define Windows cmd title from node.js.这将从 node.js 定义 Windows cmd 标题。 This is untested myself yet, but should work.这还没有经过我自己的测试,但应该可以工作。

if (process.platform == 'win32') {
  process.title = title;
} else {
  process.stdout.write('\x1b]2;' + title + '\x1b\x5c');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM