Add batch file to PATH

为君一笑 提交于 2021-02-07 14:14:20

问题


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?


回答1:


As @SLaks said in his comment, this will work.

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.

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 

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.


Edit to add:

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

That will change the current working directory to what ever the current working directory was before the last time that pushd was run.

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:.

Now, the %~dp0 is a bit weirder. It's a modified variable.

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.

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.

The d means "drive letter only". So %~d0 would be C: (assuming we're on the C: drive).

The p means "path only". So %~p0 would be \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\

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.



来源:https://stackoverflow.com/questions/28574800/add-batch-file-to-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!