将批处理文件添加到PATH [英] Add batch file to PATH
问题描述
我正在尝试通过将包含它的目录添加到PATH来全局运行.bat文件.显然,这适用于exe文件,但是有没有办法以这种方式运行.bat文件?
I'm trying to run a .bat file globally by adding the directory which contains it to PATH. This obviously works for exe files but is there a way to run .bat files this way?
推荐答案
正如@SLaks在他的评论中所说,这将起作用.
As @SLaks said in his comment, this will work.
根据其余注释,您需要指定完整的文件名.如果有 program.exe
和 program.bat
,则需要输入 program.bat
,而不仅仅是输入 program
命令提示符.
Based on the rest of your comments, you need to specify the full file name. If there's program.exe
and program.bat
, you need to enter program.bat
and not just program
at the command prompt.
在命令提示符下输入 program
时,外壳程序将首先尝试执行 program.com
,然后执行 program.exe
,然后 program.bat
.确切的顺序保存在 PATHEXT
环境变量中:
When you enter program
at a command prompt, the shell will first try to execute program.com
, then program.exe
, then program.bat
. The exact order is saved in the PATHEXT
environment variable:
C:\Windows>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
我记得,原始的MS-DOS没有此变量.它先搜索.COM,.EXE,然后搜索.BAT,以便自MS-DOS 3.3(并且我相信自IBM DOS 1.0起)以来就一直保留该行为,但是我认为它是硬编码的. PATHEXT
变量是Windows NT IIRC引入的.
Original MS-DOS didn't have this variable, as I recall. It searched .COM, .EXE, and then .BAT so the behavior has preserved since MS-DOS 3.3 (and I believe since IBM DOS 1.0), but I believe it was hard-coded. The PATHEXT
variable was introduced with Windows NT, IIRC.
编辑以添加:
啊,好的.听起来您的批处理文件也需要更改为它自己的目录,以便当前工作目录位于该文件所在的位置.最简单的方法是在批处理文件的开头(在您的 @echo关闭
之后)放置:
Ah, alright. It sounds like your batch file also needs to change to it's own directory so that the current working directory is wherever it's at. The easy way to do that is at the beginning of the batch file (after your @echo off
) put:
pushd %~dp0
这会将当前工作目录更改为批处理文件所在的位置.然后在最后一行:
This will change the current working directory to wherever the batch file is. And then at the very last line:
popd
这会将当前工作目录更改为上次运行 pushd
之前的当前工作目录.
That will change the current working directory to what ever the current working directory was before the last time that pushd
was run.
这两个命令 pushd
和 popd
就像高级更改目录命令.如果您位于 C:\
并键入 pushd C:\ Program Files \
,则将切换到该目录.然后,如果您键入 popd
,您将返回到C:所在的位置.您可以多次执行此操作,每次将另一个目录推"到目录历史记录的堆栈"上.然后 popd
删除堆栈的顶部,并带您回到堆栈顶部.可以将其视为浏览器上的后退"按钮.您甚至可以同时更改驱动器. pushd D:\
将更改为D:驱动器,并将目录设置为D:的根目录.
The two commands, pushd
and popd
, are like advanced change directory commands. If you're at C:\
and you type pushd C:\Program Files\
, you'll change to that directory. Then if you type popd
, you'll go back where you just were at C:. You can do it multiple times, each time "pushing" another directory onto the "stack" of your directory history. popd
then removes the top of the stack, and takes you back one. Think of it like the back button on your browser. You can even change the drive at the same time. pushd D:\
will change to the D: drive and set the directory to the root of D:.
现在,%〜dp0
有点奇怪.这是一个修改后的变量.
Now, the %~dp0
is a bit weirder. It's a modified variable.
您可能知道批处理文件的参数已分配给特殊变量.%1
是第一个参数,然后%2
是第二个参数,%3
是第三个参数,依此类推,直到%9
.%0
是第零个参数.那是实际批处理文件本身的名称.如果我们从 C:\ Folder \
目录中运行 program.bat
,则%0
可能是 program.bat
You might know that the arguments for a batch file get assigned to special variables. %1
is the first argument, then %2
is the second, %3
the third, and so forth up to %9
. %0
is the zeroth argument. That's the name of the actual batch file itself. If we run program.bat
from the C:\Folder\
directory, %0
is probably program.bat
.
tidle(〜
)删除参数周围的双引号.因此,%〜0
是批处理文件的文件名,不带引号,如果文件名或文件夹名中有空格,则可能会出现.
The tidle (~
) removes double quotes around the argument. So %~0
is the file name of the batch file without any quotation marks, which can appear if you have spaces in your file or folder names.
d
的意思是仅驱动器号".因此%〜d0
将是 C:
(假设我们在C:驱动器上).
The d
means "drive letter only". So %~d0
would be C:
(assuming we're on the C: drive).
p
的意思是仅路径".因此%〜p0
将是 \ Folder \
.
The p
means "path only". So %~p0
would be \Folder\
.
我们都想要,所以 dp
表示仅驱动器和路径".因此%〜dp0
扩展为 C:\ Folder \
.
We want both, so dp
means "drive and path only". So %~dp0
expands to C:\Folder\
.
因此,批处理文件的第一行现在是:
So, the first line of the batch file is now:
pushd C:\Folder\
但是它是动态的!因此,如果将其移动到 D:\ AnotherFolder \
,它仍然可以工作,而无需对其进行编辑.您可以在 下找到cmd.exe可以理解的变量修改的完整列表.> for
命令.
But it's dynamic! So if you move it to D:\AnotherFolder\
, it will still work without needing to edit it. You can find a full list of the variable modifications that cmd.exe understands under the for
command.
这篇关于将批处理文件添加到PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!