1

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.

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:

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...

enter image description here

Compo
  • 36,585
  • 5
  • 27
  • 39
ー PupSoZeyDe ー
  • 1,082
  • 3
  • 14
  • 33
  • 1
    Open a [command prompt](https://www.howtogeek.com/235101/), run `help` and look on the output list of [Windows commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) with brief description. Run the command __START__ (or any other command) with `/?` as argument to get output the help for the command. Try that out with `start /?`. What you need is the command __TITLE__. The very short help of this command can be read on running `title /?`. `title %~n0` in a batch file changes to title of the console window to name of the batch file. – Mofi May 21 '21 at 16:18
  • 1
    Well, the usage of just `title %~n0` is not secure. The batch file could do malicious things on somebody renames the batch file and adds an ampersand like `Development & Test!.cmd`. For using the batch file name in a secure manner as window title without using `"` around `%~n0` shown also in window title it is necessary to use the following lines: 1. `setlocal EnableExtensions DisableDelayedExpansion`, 2. `set "BatchName=%~n0"`, 3. `setlocal EnableDelayedExpansion`, 4. title `!BatchName!`, 5. `endlocal` and once more `endlocal`. – Mofi May 21 '21 at 16:27
  • hi, thanks @Mofi. I tried the literal "title %~n0", but it also didn't work unfortunately. idk why really.. I attached the screenshot, please take a look. – ー PupSoZeyDe ー May 21 '21 at 16:28
  • 1
    Well, you run the Linux shell interpreter `bash` for executing a command line specified after `-c` to execute `figlet` with passing `frpc_wake.cmd`. Why is the batch file `frpc_wake.cmd` not run directly? What is `figlet` at all? Why do you use on Windows the Linux shell interpreter `bash`? `bash` cannot interpret a Windows batch file and `figlet` starts most likely `C:\Windows\System32\cmd.exe` to process the batch file. So the window title changes to `frpc_wake` by command __TITLE__, but is next replaced quickly by the started full name of `cmd.exe` used to process `croncli.cmd`. – Mofi May 21 '21 at 16:40
  • 1
    Why is the batch file `croncli.cmd` not called with command __CALL__ from within `frpc_wake.cmd`? See my answer on [How to call a batch file that is one level up from the current directory?](https://stackoverflow.com/a/24725044/3074564) You are using definitely too many script interpreters for whatever you want to do. The console window title is not fixed once set by command __TITLE__. It can be changed by other programs as well and that happens here due to the series of script interpreters started one after the other. – Mofi May 21 '21 at 16:43
  • @Mofi the bash commands are from Windows Subsystem Linux Bash. it's a legit feature. figlet command is for bash/linux. Also, the croncli.cmd file is not a batch file, it's not hand written by me, it's a node.js binary built for Windows. Either way, the two comamnds are not relevant here, what the problems is only the one line "title frcp_wake" and the fact it's apparently not working on the window. thanks. – ー PupSoZeyDe ー May 22 '21 at 00:04

1 Answers1

2

So, as I got an idea what was wrong thanks to @Mofi, here are the workaround ways to solve the problem I tried.

-- The cause:

The cause of this problem is the "someprogram.cmd --option whatever" lines on the batch file. On the other cmd program file, there should be some "title" lines that overrides the previously defined title line.

-- 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

  1. modify the child cmd program file(in this case node.js generated file) manually

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.

(default)

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

(to e.g.)

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).

This will define Windows cmd title from node.js. This is untested myself yet, but should work.

if (process.platform == 'win32') {
  process.title = title;
} else {
  process.stdout.write('\x1b]2;' + title + '\x1b\x5c');
}
ー PupSoZeyDe ー
  • 1,082
  • 3
  • 14
  • 33