windows “X 不是内部或外部命令,也不是可运行的程序或批处理文件”是什么原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41454769/
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
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
提问by Hashim
I have a one-line snippet that works perfectly in the command line, but fails and throws up errors when I run it as part of a batch script.
我有一个在命令行中完美运行的单行代码段,但是当我将它作为批处理脚本的一部分运行时失败并抛出错误。
The below commands behaves as expected, deleting all empty subfolders in the folder.
以下命令按预期运行,删除文件夹中的所有空子文件夹。
for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"
However, when put in a batch file like so...
但是,当放入这样的批处理文件时......
FOR /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"
...it throws the standard error:
...它抛出标准错误:
Sort is not recognised as an internal or external command
Sort 未被识别为内部或外部命令
I've been experimenting for the last hour or so with and without escaping the pipe, changing the order of the options, looking up the documentation of both dir
and sort
, etc., but I've still not been able to figure out what's going on here. The rest of the batch file, which is only a few lines, works fine, and this is the only line in it that fails.
在过去一个小时左右的时间里,我一直在尝试逃逸和不逃逸管道、更改选项的顺序、查找dir
和的文档sort
等,但我仍然无法弄清楚发生了什么这里。批处理文件的其余部分(只有几行)工作正常,这是其中唯一失败的行。
Can anyone help?
任何人都可以帮忙吗?
回答by Mofi
A) How does Windows command processor search for commands?
A) Windows 命令处理器如何搜索命令?
Windows command processor searches for a COMMANDto execute which
Windows 命令处理器搜索要执行的COMMAND
- is not an internal command of
cmd.exe
and - is just specified with file name without file extension and without path
- 不是内部命令
cmd.exe
和 - 只是用文件名指定,没有文件扩展名和路径
for a file matching the pattern command.*
andhaving a file extension listed in local environment variable PATHEXT
对于匹配模式command.*
并在本地环境变量中列出文件扩展名的文件PATHEXT
- first in current directory and
- next in all directories of local environment variable
PATH
.
- 首先在当前目录和
- next 在本地环境变量的所有目录中
PATH
。
SORTand FINDand FINDSTRand ROBOCOPYand XCOPYand many more commands are not internal commands of cmd.exe
. They are console applications installed with Windows located in directory %SystemRoot%\System32
having the file name sort.exe
, find.exe
, findstr.exe
, robocopy.exe
, xcopy.exe
, ...
SORT和FIND、FINDSTR、ROBOCOPY和XCOPY以及更多命令不是cmd.exe
. 它们是与 Windows 一起安装的控制台应用程序,位于%SystemRoot%\System32
文件名为sort.exe
, find.exe
, findstr.exe
, robocopy.exe
, xcopy.exe
, ...
Such console applications available by default on Windows are called external commandsto distinguish them better from console applications not installed with Windows operating system.
Windows 上默认可用的此类控制台应用程序称为外部命令,以更好地将它们与未安装在 Windows 操作系统中的控制台应用程序区分开来。
B) How is the environment variable PATH defined?
B) 环境变量 PATH 是如何定义的?
There are 3 types of PATH
variables:
有3种类型的PATH
变量:
System
PATH
which is used for all accounts and stored in Windows registry under key:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
User
PATH
which is used only for current account and stored in Windows registry under key:HKEY_CURRENT_USER\Environment
Local
PATH
which is always a copyof the localPATH
of parent process which started the current process.
系统
PATH
这是下键用于所有账户,并存储在Windows注册表中:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
用户
PATH
这下键仅用于经常账户,并存储在Windows注册表中:HKEY_CURRENT_USER\Environment
本地
PATH
始终是启动当前进程的父进程的本地副本PATH
。
Windows concatenates systemand userPATH
to localPATH
for the Windows Explorer instance used as Windows desktop with the shortcuts on desktop screen and the Windows start menu as visible interface for the user.
对于用作 Windows 桌面的 Windows Explorer 实例,Windows 将系统和用户连接PATH
到本地PATH
,桌面屏幕上的快捷方式和 Windows 开始菜单作为用户的可见界面。
On starting a new process the entire currently active environment variables table of running process is?copied for the new process by Windows.
在启动新进程时,Windows 会为新进程复制运行进程的整个当前活动环境变量表。
The parent process cannot modify the environment variables of any child process nor can a child process modify the environment variables of its parent process.
父进程不能修改任何子进程的环境变量,子进程也不能修改其父进程的环境变量。
This means once a process like cmd.exe
was started for execution of a batch file, the process has its?own set of environment variables which only the process itself can modify. No other process can modify the environment variables of an already running process.
这意味着一旦cmd.exe
启动了一个类似批处理文件的进程,该进程就有自己的一组环境变量,只有进程本身可以修改这些变量。没有其他进程可以修改已运行进程的环境变量。
C) What does the error message mean?
C) 错误信息是什么意思?
The error message
错误信息
'...' is not recognized as an internal or external command,
operable program or batch file.
'...' 不是内部或外部命令,也不是
可运行的程序或批处理文件。
always means that
总是意味着
the file name of a
- console application
- GUI application
- script (batch file, PowerShell script, Perl script, VBScript, JScript, ...)
was specified for execution most likely without file extension and without (complete) path to?the?executable/script file andWindows failed to find a file matching the pattern
FileName.*
with a file extension listed in currently active environment variablePATHEXT
in current directory or any other directory in currently active environment variablePATH
.
文件名
- 控制台应用程序
- 图形用户界面应用程序
- 脚本(批处理文件、PowerShell 脚本、Perl 脚本、VBScript、JScript 等)
被指定执行最有可能没有文件扩展名和没有(完整)路径?可执行/脚本文件和Windows 未能找到与模式匹配的
FileName.*
文件,文件扩展名列在当前PATHEXT
目录中的当前活动环境变量或当前活动环境变量中的任何其他目录中PATH
。
D) What are the possible reasons for this error message?
D) 此错误消息的可能原因是什么?
Typical reasons are:
典型的原因是:
1. The file name of the file to execute was specified wrong due to a typing mistake.
1. 由于键入错误,要执行的文件的文件名指定错误。
Check character by character the name of the command/executable.
逐字符检查命令/可执行文件的名称。
2. The current directory is different to the directory containing the file to execute.
2. 当前目录与包含要执行的文件的目录不同。
Run echo Current directory is: %CD%
on command line or add this line to the batch file above the command line which fails to see what the current directory is.
echo Current directory is: %CD%
在命令行上运行或将此行添加到无法查看当前目录是什么的命令行上方的批处理文件中。
3. The executable or script to run is not installed at all.
3. 根本没有安装要运行的可执行文件或脚本。
Verify the existence of the executable to run. Some installation packages work only if other packages like Java, NPM, PHP, etc. were installed before.
验证要运行的可执行文件是否存在。一些安装包只有在之前安装了其他包,如 Java、NPM、PHP 等时才有效。
4. The directory of the file to execute is not inPATH
at all.
4、要执行的文件所在目录根本不在PATH
。
Open in Windows Control Panelthe Systemsettings window, click on Advanced system settingson left side, click on button Environment Variablesand look in both lists for Path
and?their values. By default Path
exists only in list of System variables.
打开Windows的控制面板的系统设置窗口中,单击高级系统设置在左侧,点击按钮环境变量在这两个名单,并期待Path
和?他们的价值观。默认情况下Path
仅存在于系统变量列表中。
5. A running process/application was not restarted after modification of system or userPATH
.
5. 修改系统或用户后未重新启动正在运行的进程/应用程序PATH
。
A modification of systemPATH
or userPATH
with command setx
or via Control Panel – System – Advanced system settingswas made by the user or an installer, but an already running process/application like an opened command prompt or PowerShell window was not closed/exited and opened/restarted after PATH
modification. This is necessary as described in detail in chapter F)below.
用户或安装程序使用命令或通过控制面板 - 系统 - 高级系统设置对系统PATH
或用户PATH
进行了修改,但已运行的进程/应用程序(如打开的命令提示符或 PowerShell 窗口)未关闭/退出和打开/修改后重启。这是必要的,详见下文F)章。setx
PATH
6. An executable in %SystemRoot%\System32
is not found on 64-bit Windows.
6.%SystemRoot%\System32
在 64 位 Windows 上找不到可执行文件。
There is the directory %SystemRoot%\System32
with 64-bit executables and %SystemRoot%\SysWOW64
with 32-bit executables on 64-bit Windows. Most executables exist in both directories. But there are some executables existing only in System32
and a few only in SysWOW64
.
在 64 位 Windows 上%SystemRoot%\System32
有 64 位可执行文件和%SystemRoot%\SysWOW64
32 位可执行文件的目录。大多数可执行文件都存在于这两个目录中。但是有一些可执行文件System32
仅存在于SysWOW64
.
The systemPATH
contains by default as first folder path %SystemRoot%\System32
. But which one of the?two Windows system folders is searched for the executable specified without path or with the path %SystemRoot%\System32
depends on the execution environment. An application or script executed in 64-bit environment is really accessing %SystemRoot%\System32
while an application or script executed in?32-bit environment is redirected by the Windows file system redirectorto the directory %SystemRoot%\SysWOW64
.
该系统PATH
包含默认为第一个文件夹路径%SystemRoot%\System32
。但是不指定路径或指定路径的可执行文件在两个Windows系统文件夹中搜索哪一个%SystemRoot%\System32
取决于执行环境。在 64 位环境中执行的应用程序或脚本是真正访问,%SystemRoot%\System32
而在 32 位环境中执行的应用程序或脚本由 Windows文件系统重定向器重定向到目录%SystemRoot%\SysWOW64
。
An application or script running in 32-bit environment which wants to run a 64-bit executable in %SystemRoot%\System32
has to use full qualified file name of the executable with file path %SystemRoot%\Sysnative
.
在 32 位环境中运行的应用程序或脚本想要在其中运行 64 位可执行文件%SystemRoot%\System32
必须使用可执行文件的完整限定文件名和文件路径%SystemRoot%\Sysnative
。
Note:%SystemRoot%\Sysnative
is neither a directory nor any type of link. It is something very special existing only for x86 applications. It does not exist for amd64 applications. The condition if exist %SystemRoot%\Sysnative
in a batch file is always false in both environments, but if exist %SystemRoot%\Sysnative\cmd.exe
is true in 32-bit execution environment and false in 64-bit environment and also on 32-bit Windows. This condition can be used in batch scripts to find out if the batch file is processed by 32-bit cmd.exe
in %SystemRoot%\SysWOW64
on 64-bit Windows which can be important to know depending on the task.
注意:%SystemRoot%\Sysnative
既不是目录也不是任何类型的链接。这是一种非常特殊的东西,只存在于 x86 应用程序中。它对于 amd64 应用程序不存在。if exist %SystemRoot%\Sysnative
批处理文件中的条件在两种环境中始终为假,但if exist %SystemRoot%\Sysnative\cmd.exe
在 32 位执行环境中为真,在 64 位环境和 32 位 Windows 中为假。这种情况可以在批处理脚本中使用,以找出是否该批处理文件是由32位处理,cmd.exe
在%SystemRoot%\SysWOW64
64位的Windows可以是重要的根据任务就知道了。
See also the Microsoft documentations WOW64 Implementation Detailsand Registry Keys Affected by WOW64.
另请参阅 Microsoft 文档WOW64 实施详细信息和受 WOW64 影响的注册表项。
7.PATH
contains a reference to a not (yet) defined environment variable.
7.PATH
包含对尚未(尚未)定义的环境变量的引用。
It is possible to specify in PATH
a folder path using a reference to value of another environment variable like SystemRoot
. It is important that this environment variable is also defined in same set?of?environment variables or a set of environment variables processed first by Windows.
可以PATH
使用对另一个环境变量(如SystemRoot
. 重要的是,这个环境变量也被定义在同一个环境变量集或由 Windows 首先处理的一组环境变量中。
For example if %JAVA_HOME%\bin
is added to systemPATH
environment variable, there must be defined also a systemenvironment variable JAVA_HOME
with the base folder path to Java program files. It is not enough to have defined a userenvironment variable JAVA_HOME
or define the environment variable JAVA_HOME
later in the localenvironment of a batch file.
例如,如果%JAVA_HOME%\bin
添加到系统PATH
环境变量中,则还必须定义一个系统环境变量,JAVA_HOME
其中包含 Java 程序文件的基本文件夹路径。仅定义用户环境变量JAVA_HOME
或JAVA_HOME
稍后在批处理文件的本地环境中定义环境变量是不够的。
%JAVA_HOME%\bin
added to userPATH
is expanded by Windows to a full qualified folder path if the environment variable JAVA_HOME
is defined either as systemor as userenvironment variable, but?not?on JAVA_HOME
defined later in the localenvironment of a Windows command process.
%JAVA_HOME%\bin
如果环境变量被定义为系统或用户环境变量,但不是稍后在Windows 命令进程的本地环境中定义,则添加到用户PATH
被 Windows 扩展为完全限定的文件夹路径。JAVA_HOME
JAVA_HOME
Such a mistake can be seen easily by opening a new command promptwindow after making a modification on systemor userPATH
from Windows start menu and running set path
. The output PATH
should not contain anymore any %Variable%
environment variable value reference.
从 Windows 开始菜单对系统或用户进行修改并运行后,通过打开一个新的命令提示符窗口可以很容易地看到这样的错误。输出不应再包含任何环境变量值引用。PATH
set path
PATH
%Variable%
8. The LOCAL variablePATH
was modified before on command line or in batch file.
8. LOCAL 变量PATH
之前在命令行或批处理文件中被修改过。
Run set path
on command line or add this command to the batch file above the command line which fails to see the current values of the environment variables PATH
and PATHEXT
.
set path
在命令行上运行或将此命令添加到命令行上方的批处理文件中,该文件无法查看环境变量的当前值PATH
和PATHEXT
.
The last reason is responsible for external command SORTnot being found on execution of the batch file which contains somewhere above set path=...
.
最后一个原因是在执行包含上面某处的批处理文件时找不到外部命令SORTset path=...
。
E) How to avoid this error message?
E) 如何避免此错误消息?
Best is coding a batch file for being independent on PATH
and PATHEXT
and the order of directories in PATH
which means here using the command line:
最好是编码一个批处理文件为是独立于PATH
和PATHEXT
与目录中的顺序PATH
该装置在这里使用命令行:
FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%\System32\sort.exe /r') do rd "%%d"
Any external commandof which executable is stored in %SystemRoot%\System32
should be specified in a batch file with this path and with file extension .exe
. Then Windows command interpreter does not need to search for a file using localPATH
and PATHEXT
and the batch file works always (as long as environment variable SystemRoot
is not also modified in the batch file which I have never seen).
存储可执行文件的任何外部命令%SystemRoot%\System32
都应在具有此路径和文件扩展名的批处理文件中指定.exe
。然后 Windows 命令解释器不需要使用本地搜索文件PATH
,PATHEXT
并且批处理文件始终有效(只要SystemRoot
我从未见过的批处理文件中也没有修改环境变量)。
F) When is a system or user PATH change applied to processes?
F) 系统或用户路径更改何时应用于进程?
When a user opens a command prompt window via Windows start menu or from within a Windows Explorer window, the user starts cmd.exe
with implicit using option /K
to keep the console window open after finishing a command which is good for debugging a batch file.
当用户通过 Windows 开始菜单或从 Windows 资源管理器窗口中打开命令提示窗口时,用户cmd.exe
以隐式 using 选项启动,/K
以在完成有助于调试批处理文件的命令后保持控制台窗口打开。
When a batch file is doubled clicked in Windows Explorer, the user starts cmd.exe
for processing the batch file with implicit using option /C
to close the console window after finishing batch processing which is not good for debugging a batch file as error messages cannot be seen in this case.
当在 Windows 资源管理器中双击cmd.exe
批处理文件时,用户开始处理批处理文件,/C
在完成批处理后隐式使用选项关闭控制台窗口,这不利于调试批处理文件,因为在这种情况下无法看到错误消息.
In both cases Windows creates a copy of the environment variables of the application starting cmd.exe
which is usually Windows Explorer. Therefore the started command process has a local?PATH
of which value is the same as the parent process had on starting cmd.exe
.
在这两种情况下,Windows 都会创建启动应用程序的环境变量的副本,该应用程序cmd.exe
通常是 Windows 资源管理器。因此,启动的命令进程有一个本地的? PATH
其中的值与启动时父进程的值相同cmd.exe
。
Example:
例子:
Open a command prompt window, run
title Process1
and runset path
.
Output isPATH
andPATHEXT
as currently defined for current user account in the console window having now the window title Process1.Run
set PATH=%SystemRoot%\System32
and next once againset path
.
Output is againPATH
andPATHEXT
, but withPATH
containing only one directory now.Run
start "Process2"
and run in new console window with window title Process2the commandset path
.
Output isPATH
andPATHEXT
with same values as before in Process1.
This demonstrates that on starting a new process the currentenvironment variables of running process are copied and not what Windows itself has currently stored in Windows registry.Run in Process2the command
set PATH=
and nextset path
.
Output is onlyPATHEXT
because localPATH
does not exist anymore for Process2.
This demonstrates that every process can modify its environment variables including complete deletion.Switch to Process1window, run the command
set PATH=%PATH%;%SystemRoot%
and nextset?path
.
Output isPATH
with two directories andPATHEXT
.Run the command
start "Process3"
and in opened window with title Process3the commandset path
.
Output isPATH
with two directories as defined also for Process1andPATHEXT
.Run in Process3the command
set PATH=%SystemRoot%\System32
.
打开命令提示符窗口,运行
title Process1
并运行set path
。
输出PATH
和PATHEXT
当前在控制台窗口中为当前用户帐户定义的一样,现在具有窗口标题Process1。set PATH=%SystemRoot%\System32
再次运行和nextset path
。
输出又是PATH
andPATHEXT
,但PATH
现在只包含一个目录。start "Process2"
在窗口标题为Process2的新控制台窗口中运行并运行命令set path
。
输出PATH
和PATHEXT
之前在Process1 中具有相同的值。
这表明在启动新进程时,正在复制运行进程的当前环境变量,而不是 Windows 本身当前存储在 Windows 注册表中的内容。在Process2 中运行命令
set PATH=
和 nextset path
。
输出只是PATHEXT
因为本地PATH
不再存在Process2。
这表明每个进程都可以修改其环境变量,包括完全删除。切换到Process1窗口,运行命令
set PATH=%PATH%;%SystemRoot%
和 nextset?path
。
输出PATH
有两个目录和PATHEXT
.运行命令
start "Process3"
并在标题为Process3的打开窗口中运行命令set path
。
输出PATH
有两个目录,同样为Process1和PATHEXT
.在Process3 中运行命令
set PATH=%SystemRoot%\System32
。
There are 3 command processes running with following values for localPATH
when %SystemRoot%
expands to C:\Windows
:
当扩展为时,有 3 个命令进程使用以下本地值运行:PATH
%SystemRoot%
C:\Windows
Process1: PATH=C:\Windows\System32;C:\Windows
Process2: PATH
does not exist at all.
Process3: PATH=C:\Windows\System32
Process1: PATH=C:\Windows\System32;C:\Windows
Process2:PATH
根本不存在。
过程3:PATH=C:\Windows\System32
So what happens now on opening Control Panel – System – Advanced System Settings – Environment Variablesand adding to list of User variablesthe newenvironment variable PATH
with?value C:\Temp
, or in case of there is already a userPATH
environment variable, editPATH
and?append ;C:\Temp
to the value?
那么现在情况就打开控制面板-系统-高级系统设置-环境变量并添加到列表用户变量的新环境变量PATH
与价值?C:\Temp
,或在情况下已经有一个用户PATH
环境变量,编辑PATH
和追加?;C:\Temp
到价值?
Well, as long as the dialog window with title Environment Variablesshowing the two lists is opened, nothing happens on modifying the variables, until button OKis clicked to take over all changes into Windows registry and close the window.
好吧,只要打开显示两个列表的标题为环境变量的对话框窗口,修改变量时不会发生任何事情,直到单击“确定”按钮将所有更改接管到 Windows 注册表并关闭窗口。
Let's go back to the three running command processes and run in Process1, Process2and Process3the command set path
. It can be seen:
让我们回到三个正在运行的命令进程,并在Process1、Process2和Process3中运行命令set path
。可以被看见:
Process1: PATH=C:\Windows\System32;C:\Windows
Process2: PATH
does not exist at all.
Process3: PATH=C:\Windows\System32
Process1: PATH=C:\Windows\System32;C:\Windows
Process2:PATH
根本不存在。
过程3:PATH=C:\Windows\System32
Nothing changed on already running processes.
已经运行的进程没有任何变化。
No process can modify the environment variables of a different running process!
没有进程可以修改不同运行进程的环境变量!
Open from Windows start menu one more command prompt window and run in fourth command process the command set path
. It can be seen that localPATH
of fourth command process has appended the directory C:\Temp
now.
从 Windows 开始菜单再打开一个命令提示符窗口并在第四个命令中运行该命令set path
。可以看到第四个命令进程的local现在PATH
已经追加了目录C:\Temp
。
Then close all four command processes and delete the added userPATH
respectively remove ;C:\Temp
from userPATH
if having appended this directory path before.
然后关闭所有四个命令进程并删除添加的用户,如果之前已附加此目录路径,则PATH
分别;C:\Temp
从用户中删除PATH
。
How is this possible if no process can modify the environment variables of an already running process?
如果没有进程可以修改已运行进程的环境变量,这怎么可能?
How was the environment variables list of Windows Explorer instance running as Windows desktop modified on closing Environment Variableswindow with button OK?
在使用“确定”按钮关闭“环境变量”窗口时,作为 Windows 桌面运行的 Windows 资源管理器实例的环境变量列表如何修改?
The answer on those two questions was given by eryksunin his comment.
eryksun在他的评论中给出了这两个问题的答案。
After writing the modifications on systemand uservariables into registry on clicking button OKof?Environment Variableswindow, Windows sends the WM_SETTINGCHANGEmessage to all?top-level windows to inform the running applications about changed system parameters.
将系统和用户变量的修改写入注册表后单击“确定”按钮?Environment Variables窗口,Windows 向所有顶级窗口发送WM_SETTINGCHANGE消息,以通知正在运行的应用程序有关更改的系统参数。
It is up to the application if this event message is handled at all and how. Windows Explorer running as?Windows desktop reads the environment variables from registry and updates its environment variables list accordingly. Other applications like Total Commander handle this message also and update their lists of environment variables too. But cmd.exe
does not do that fortunately as this would be really problematic.
是否完全处理此事件消息以及如何处理取决于应用程序。以 Windows 桌面身份运行的 Windows 资源管理器从注册表中读取环境变量并相应地更新其环境变量列表。Total Commander 等其他应用程序也会处理此消息并更新它们的环境变量列表。但cmd.exe
幸运的是不会这样做,因为这真的很成问题。
Is there any possibility to modify a systemor uservariable with notification via WM_SETTINGCHANGE
from within a command prompt window or batch file?
是否有可能通过命令提示符窗口或批处理文件中的通知来修改系统或用户变量WM_SETTINGCHANGE
?
It is possible to modify the registry value of an environment variable using reg add
command. But this does not result in sending WM_SETTINGCHANGE
message to all top-level windows. Such changes done with reg add
or with regedit
require a restart of Windows (or at least a log off and log on of current user) to be taken into account at all.
可以使用reg add
命令修改环境变量的注册表值。但这不会导致WM_SETTINGCHANGE
向所有顶级窗口发送消息。使用reg add
或使用regedit
进行的此类更改需要重新启动 Windows(或至少注销并登录当前用户)才能完全考虑在内。
But there is also the command setx
which is designed for modifying a systemor uservariable and which also sends the WM_SETTINGCHANGE
message to all top-level windows after registry was updated according to specified arguments. Run setx /?
in a command prompt window for details. But please take into account that setx
does not modify the localenvironment variable of?running command process. This must be done with using command set
used in addition to setx
.
但是还有一个命令setx
,它是为修改系统或用户变量而设计的,并且WM_SETTINGCHANGE
在根据指定的参数更新注册表后,它还会将消息发送到所有顶级窗口。setx /?
在命令提示符窗口中运行以获取详细信息。但请注意setx
,不修改运行命令进程的本地环境变量。这必须通过set
使用除了setx
.
G) How is environment variable PATHEXT handled by Windows?
G) Windows 如何处理环境变量 PATHEXT?
The environment variable PATHEXT
with the list of file extensions is handled by Windows different in comparison to environment variable PATH
.
与环境变量相比,PATHEXT
带有文件扩展名列表的环境变量由 Windows 处理PATH
。
SystemPATHEXT
and userPATHEXT
are NOTconcatenated to localPATHEXT
.
系统PATHEXT
和用户PATHEXT
都不要串接到本地PATHEXT
。
A userPATHEXT
replacesthe systemPATHEXT
for all processes running under environment of the account having defined a userPATHEXT
.
甲用户PATHEXT
替换的系统PATHEXT
下的帐户的环境中运行的所有过程具有确定一个用户PATHEXT
。
There is defined only a systemPATHEXT
environment variable by default.
默认情况下只定义了一个系统PATHEXT
环境变量。
H) Is it possible to disable file search in current directory?
H) 是否可以禁用当前目录中的文件搜索?
Windows command processor searches by default in current directory if file name of a script file or executable is specified on command line or in a batch file without any path which means without a backslash \
(or a forward slash /
thanks to auto-correction) in argument string.
如果在命令行或批处理文件中指定了脚本文件或可执行文件的文件名而没有任何路径,这意味着参数字符串中没有反斜杠\
(或/
由于自动更正而产生的正斜杠),则Windows 命令处理器默认在当前目录中搜索.
But on Windows Vista and later Windows client versions and on Windows Server 2003 and later Windows server versions it is indeed possible to disable searching for a script/executable in current directory specified without at least relative path .\
by defining the environment variable NoDefaultCurrentDirectoryInExePath
with any value as written by eryksunin his comment below and?explained by Microsoft's documentation about function NeedCurrentDirectoryForExePathA.
但是在 Windows Vista 和更高版本的 Windows 客户端版本以及在 Windows Server 2003 和更高版本的 Windows 服务器版本上,确实可以.\
通过定义NoDefaultCurrentDirectoryInExePath
具有写入的任何值的环境变量来禁用在指定的当前目录中搜索脚本/可执行文件而没有至少相对路径由eryksun在他下面的评论中和? 由微软关于函数NeedCurrentDirectoryForExePathA的文档解释。
See Removing the current working directory from the pathfor more details on usage of this environment variable.
有关使用此环境变量的更多详细信息,请参阅从路径中删除当前工作目录。
回答by aschipfl
Most probably, you messed around with the PATH
variable. Perhaps you are overwriting it somewhere else in your script. Since sort
is an external command, opposed to all the others in your command line like for
, dir
, rd
, which are cmd
-internal commands, the PATH
variable is needed to find the command. If PATH
is not defined, external commands are searched in the current working directory only. There is also a PATHEXT
variable that is needed to define standard file extensions for executables, like .com
, .exe
. So when sort
appears in command prompt or in a batch file, the system searches the current working directory and all directories specified by the PATH
variable for a file with the base name sort
and one of the extensions specified by PATHEXT
. The command sort
is actually called sort.exe
and is usually located in C:\Windows\System32
.
最有可能的是,您弄乱了PATH
变量。也许您正在脚本中的其他地方覆盖它。由于sort
是一个外部命令,像您的命令行与所有的人for
,dir
,rd
,这是cmd
-internal命令时,PATH
需要变量来查找命令。如果PATH
未定义,则仅在当前工作目录中搜索外部命令。还有一个PATHEXT
变量需要定义可执行文件的标准文件扩展名,例如.com
, .exe
. 所以当sort
出现在命令提示符或批处理文件中时,系统会在当前工作目录和PATH
变量指定的所有目录中搜索具有基本名称的文件sort
和指定的扩展名之一PATHEXT
。该命令sort
实际上被调用sort.exe
并且通常位于C:\Windows\System32
.