如何在批处理文件完成任务后关闭其cmd窗口?

huangapple go评论96阅读模式
英文:

How to get a batch file's cmd window to close after it's done its job?

问题

对不起,我试图重复发布,但我已经尝试了这里看到的大多数解决方案,但没有一个起作用。目前我使用VS Code的方式是通过一个批处理文件,它导航到我想要工作的目录,然后运行一个命令来启动它。原始文件如下:

@echo off
d:
cd c++
code .
pause

但是一旦VS Code启动,命令提示符仍然存在。我首先认为是我使用了 pause,所以我将其替换为 exit。但没有成功。所以我来到这里,看到有人建议在 code . 行末尾添加 start。然而,仍然没有起作用。在这一点上,我看到另一篇帖子说它可能会被“卡住”,所以我决定在每个点上加上 echo,看看是否是这种情况,想知道它会在哪里停止。此时,文件看起来像这样:

@echo on
d:
echo hello1
cd c++
echo hello2
start code .
echo hello3
exit

这不仅仍然没有关闭命令提示符终端,而且也没有输出任何内容?我确信这是一个小问题,但我对这个还不够了解,无法看到。感谢您的帮助。

英文:

I'm sorry to repeat-post but I've tried most solutions I've seen on here and none have worked. Currently the way I use VS Code is by a batch file that navigates to the directory I like to work out of and then runs a command to launch it. The original file looked like this:

@echo off
d:
cd c++
code .
pause

Once VS Code launches though, the cmd prompt lingers. I assumed first it was my use of pause, so I swapped that to exit. No dice. So I took to here, saw someone suggested I append start to the code . line. Still, nothing worked. At this point, I saw another post saying that it could be getting "stuck" so I decided to throw echo at each point to see if that were the case, wondering where it'd stop. At this point, the file looks like:

@echo on
d:
echo hello1
cd c++
echo hello2
start code .
echo hello3
exit

Not only did this still not close the cmd terminal, but it also isn't echoing anything at all? I'm sure it's something small but I'm just too new to this to see. Thank you for your help.

答案1

得分: 1

你可以使用 taskkill 命令来关闭命令窗口。
start 命令的第一个参数是要创建的窗口标题。
你可以在那里指定一个唯一的字符串,然后使用 /FI 参数调用 taskkill

start "vs_run_unique_title" code "d:\some_code_path"
taskkill /FI "WindowTitle eq vs_run_unique_title" /F /T
英文:

You could use a taskkill command to close the command window.
The first parameter of the start command is the title of the window to create.
You could specify a unique string there and call for taskkill with /FI parameter.

start "vs_run_unique_title" code "d:\some_code_path"
taskkill /FI "WindowTitle eq vs_run_unique_title" /F /T

答案2

得分: 0

使用 start 命令:

@echo off
...
start "" code .

您的 start 语法不正确。第一个参数指定标题。因此,您启动一个标题为 "code" 的 cmd。

英文:

Use start command:

@echo off
...
start "" code .

Your start syntax is incorrect. The first parameter specifies the title. So you start a cmd with title "code"

huangapple
  • 本文由 发表于 2020年1月6日 18:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定