如何在 Windows 上的 CMD 中从任何位置调用 .bat 文件

发布于 2024-09-15 07:36:03 字数 144 浏览 4 评论 0原文

我有一个批处理文件,我想从任何目录在 CMD 中执行该文件。像这样的:

文件名:MyBatch

路径:C:\MyBatch.bat

打开 CMD: c:\程序文件> MyBatch

我怎样才能做到这一点?

I have a Batch file which I want to execute in CMD from any directory. Something like this:

File name: MyBatch

Path: C:\MyBatch.bat

Open CMD:
c:\Program Files> MyBatch

How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪眸﹌ 2024-09-22 07:36:03

在 PATH 环境变量中设置该位置。

我不会把它放在根目录或系统目录中。

我将所有脚本保存在 C:\DRR\CMD 中

,并在 MyComputer GUI 中设置它或在命令脚本中运行:

set PATH=%PATH%;C:\DRR\CMD

Set that location in your PATH environmental variable.

I wouldn't put it the root or the system directory.

I keep a directory with all my scripts in C:\DRR\CMD

and either set it in the MyComputer GUI or run at the command script:

set PATH=%PATH%;C:\DRR\CMD
胡大本事 2024-09-22 07:36:03

您可以将其放在 c:\windows\system32 目录中,因为它始终位于系统路径中。

You could just put it in your c:\windows\system32 directory, as its always in the system path.

笑饮青盏花 2024-09-22 07:36:03

怎么样...
“%MyBatch%”? (双引号是有意的)

那应该可以!

要更改变量,请使用 set MyBatch="Path\Whatever.bat"

要向用户询问字符串,请使用 set /p MyBatch="Question? "
-- 或者,您可以使用 BAT 到 EXE 转换器在可执行文件中运行批处理。

How about...
"%MyBatch%"? (the double qoutes are intended)

That should work!


to change your Variable, use set MyBatch="Path\Whatever.bat"

and to ask the user for a String, use set /p MyBatch="Question? "
-- or, you can use a BAT-to-EXE converter to run the batch in a Executable.

街道布景 2024-09-22 07:36:03

您需要设置 PATH 环境变量以包含批处理文件的路径

You would need to set the PATH environment variable to include the path to your batch file

橙味迷妹 2024-09-22 07:36:03

如果您使用的是 Windows,则需要设置 PATH 环境变量。

bat 文件所在的路径应附加到 PATH 变量中。
在您的示例中附加“C:\;”在 Path 环境变量的值中。

然后您可以从命令行的任何位置执行 MyBatch.bat。

If you are talking Windows, then the PATH Environment variable is what you need to set.

The path where your bat file is placed should be appended to the PATH variable.
In your example append "C:\;" in the value for Path environment variable.

Then you can execute MyBatch.bat from anywhere on the command line.

拔了角的鹿 2024-09-22 07:36:03

创建一个名为 Batches 的文件夹(假设在您的 C 驱动器中)。

将 C:\Batches 附加到 path 环境变量中,然后您可以从任何地方运行该目录中的批处理文件。

Create a folder called Batches (lets say in your C drive).

Append C:\Batches in your path environment variable and you then can run batch files in that directory from anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文