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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3534346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:05:01  来源:igfitidea点击:

How to call a .bat file from any location in CMD on Windows

windowscommandprompt

提问by ByteArray123

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

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

File name: MyBatch

文件名:MyBatch

Path: C:\MyBatch.bat

路径:C:\MyBatch.bat

Open CMD: c:\Program Files> MyBatch

打开 CMD:c:\Program Files> MyBatch

How can I accomplish this?

我怎样才能做到这一点?

回答by Rawheiser

Set that location in your PATH environmental variable.

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

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

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

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

我在 C:\DRR\CMD 中保存了一个包含所有脚本的目录

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

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

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

回答by wfoster

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

你可以把它放在你的c:\windows\system32目录中,因为它总是在系统路径中。

回答by Scott zuz22 Blacklock

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

怎么样…… "%MyBatch%"?(双引号是有意的)

That should work!

那应该有效!

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

要更改您的变量,请使用 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.

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

回答by Russ

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

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

回答by Jagmag

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

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

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.

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

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

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

回答by Sababado

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

创建一个名为Batches(让我们说在你的 C 驱动器中)的文件夹。

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

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