如何从批处理文件中获取窗口标题文本

cas*_*yle 6 windows batch-file title

如何获取当前窗口的值title,设置如下:

TITLE Here Are The New Contents
Run Code Online (Sandbox Code Playgroud)

图片

Wae*_*lay 8

在 cmd.exe(通常的命令行提示符)中:

设置窗口标题:

title "Your New Title"
Run Code Online (Sandbox Code Playgroud)

获取窗口标题:我没有找到任何有用的东西来做这样的事情,但是如果你有一些 C# 或 Visual Basic 的知识,你可以开发一个小程序,它会在打开的窗口中查找你的命令行并返回标题你。(使用父进程的PID(你的cmd.exe))

在 Powershell 中:(这里很容易)

设置窗口标题:

[system.console]::title = "Your New Title"
Run Code Online (Sandbox Code Playgroud)

获取窗口标题:

$myTitleVar = [system.console]::title
Run Code Online (Sandbox Code Playgroud)

或者你可以直接:

echo [system.console]::title
Run Code Online (Sandbox Code Playgroud)

  • `echo [system.console]::title` 只是为我输出 `[system.console]::title` (2认同)
  • @a_horse_with_no_name:无需使用 `echo` - 只需按原样提交 `[system.console]::title`(PowerShell _implicitly_ 输出 [到显示器])。如果你确实使用了 `echo`(它是 `Write-Output` 的别名,很少需要显式使用它),你必须将参数括在 `(...)` 中: `echo ([system.console]: :title)` - 在命令参数中,标记初始的 `[` 不被评估为 _expression_ 并被视为字符串文字;请参阅 [about_Parsing](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing)。 (2认同)

Ato*_*all 4

没有内置任何内容,但您可以从tasklist命令中检索它。

tasklist /fi "imagename eq cmd.exe" /fo list /v

  • 您可以运行`wmic process getparentprocessid,name|find "WMIC"`,它返回cmd.exe执行实例的父PID。然后,您可以解析字符串(可能是 for 循环)以提取 PID 并针对任务列表运行 `tasklist /fi "pid eq <PID>" /fo list /v | 找到“窗口标题:` (4认同)
  • 如果正在运行多个 cmd.exe,如何获取正确的实例? (3认同)