如何在 Windows 上的 CMD 中从任何位置调用 .bat 文件
我有一个批处理文件,我想从任何目录在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 PATH 环境变量中设置该位置。
我不会把它放在根目录或系统目录中。
我将所有脚本保存在 C:\DRR\CMD 中
,并在 MyComputer GUI 中设置它或在命令脚本中运行:
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:
您可以将其放在
c:\windows\system32
目录中,因为它始终位于系统路径中。You could just put it in your
c:\windows\system32
directory, as its always in the system path.怎么样...
“%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.
您需要设置 PATH 环境变量以包含批处理文件的路径
You would need to set the PATH environment variable to include the path to your batch file
如果您使用的是 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.
创建一个名为
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.