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

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

What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?

windowsbatch-filecmd

提问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 dirand 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

  1. is not an internal command of cmd.exeand
  2. is just specified with file name without file extension and without path
  1. 不是内部命令cmd.exe
  2. 只是用文件名指定,没有文件扩展名和路径

for a file matching the pattern command.*andhaving a file extension listed in local environment variable PATHEXT

对于匹配模式command.*在本地环境变量中列出文件扩展名的文件PATHEXT

  1. first in current directory and
  2. next in all directories of local environment variable PATH.
  1. 首先在当前目录
  2. 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%\System32having the file name sort.exe, find.exe, findstr.exe, robocopy.exe, xcopy.exe, ...

SORTFINDFINDSTRROBOCOPYXCOPY以及更多命令不是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 PATHvariables:

有3种类型的PATH变量:

  1. SystemPATHwhich is used for all accounts and stored in Windows registry under key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    
  2. UserPATHwhich is used only for current account and stored in Windows registry under key:

    HKEY_CURRENT_USER\Environment
    
  3. LocalPATHwhich is always a copyof the local PATHof parent process which started the current process.

  1. 系统PATH这是下键用于所有账户,并存储在Windows注册表中:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    
  2. 用户PATH这下键仅用于经常账户,并存储在Windows注册表中:

    HKEY_CURRENT_USER\Environment
    
  3. 本地PATH始终是启动当前进程的父进程的本地副本PATH

Windows concatenates systemand userPATHto localPATHfor 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.exewas 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

总是意味着

  1. 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 and

  2. Windows failed to find a file matching the pattern FileName.*with a file extension listed in currently active environment variable PATHEXTin current directory or any other directory in currently active environment variable PATH.

  1. 文件名

    • 控制台应用程序
    • 图形用户界面应用程序
    • 脚本(批处理文件、PowerShell 脚本、Perl 脚本、VBScript、JScript 等)


    被指定执行最有可能没有文件扩展名和没有(完整)路径?可执行/脚本文件

  2. 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 inPATHat 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 Pathand?their values. By default Pathexists 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 systemPATHor userPATHwith command setxor 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 PATHmodification. This is necessary as described in detail in chapter F)below.

用户或安装程序使用命令或通过控制面板 - 系统 - 高级系统设置系统PATH用户PATH进行了修改,但已运行的进程/应用程序(如打开的命令提示符或 PowerShell 窗口)未关闭/退出和打开/修改后重启。这是必要的,详见下文F)章。setxPATH

6. An executable in %SystemRoot%\System32is not found on 64-bit Windows.

6.%SystemRoot%\System32在 64 位 Windows 上找不到可执行文件。

There is the directory %SystemRoot%\System32with 64-bit executables and %SystemRoot%\SysWOW64with 32-bit executables on 64-bit Windows. Most executables exist in both directories. But there are some executables existing only in System32and a few only in SysWOW64.

在 64 位 Windows 上%SystemRoot%\System32有 64 位可执行文件和%SystemRoot%\SysWOW6432 位可执行文件的目录。大多数可执行文件都存在于这两个目录中。但是有一些可执行文件System32仅存在于SysWOW64.

The systemPATHcontains 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%\System32depends on the execution environment. An application or script executed in 64-bit environment is really accessing %SystemRoot%\System32while 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%\System32has to use full qualified file name of the executable with file path %SystemRoot%\Sysnative.

在 32 位环境中运行的应用程序或脚本想要在其中运行 64 位可执行文件%SystemRoot%\System32必须使用可执行文件的完整限定文件名和文件路径%SystemRoot%\Sysnative

Note:%SystemRoot%\Sysnativeis 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%\Sysnativein a batch file is always false in both environments, but if exist %SystemRoot%\Sysnative\cmd.exeis 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.exein %SystemRoot%\SysWOW64on 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%\SysWOW6464位的Windows可以是重要的根据任务就知道了。

See also the Microsoft documentations WOW64 Implementation Detailsand Registry Keys Affected by WOW64.

另请参阅 Microsoft 文档WOW64 实施详细信息受 WOW64 影响的注册表项

7.PATHcontains a reference to a not (yet) defined environment variable.

7.PATH包含对尚未(尚未)定义的环境变量的引用。

It is possible to specify in PATHa 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%\binis added to systemPATHenvironment variable, there must be defined also a systemenvironment variable JAVA_HOMEwith the base folder path to Java program files. It is not enough to have defined a userenvironment variable JAVA_HOMEor define the environment variable JAVA_HOMElater in the localenvironment of a batch file.

例如,如果%JAVA_HOME%\bin添加到系统PATH环境变量中,则还必须定义一个系统环境变量,JAVA_HOME其中包含 Java 程序文件的基本文件夹路径。仅定义用户环境变量JAVA_HOMEJAVA_HOME稍后在批处理文件的本地环境中定义环境变量是不够的。

%JAVA_HOME%\binadded to userPATHis expanded by Windows to a full qualified folder path if the environment variable JAVA_HOMEis defined either as systemor as userenvironment variable, but?not?on JAVA_HOMEdefined later in the localenvironment of a Windows command process.

%JAVA_HOME%\bin如果环境变量被定义为系统用户环境变量,但不是稍后在Windows 命令进程的本地环境中定义,则添加到用户PATH被 Windows 扩展为完全限定的文件夹路径。JAVA_HOMEJAVA_HOME

Such a mistake can be seen easily by opening a new command promptwindow after making a modification on systemor userPATHfrom Windows start menu and running set path. The output PATHshould not contain anymore any %Variable%environment variable value reference.

从 Windows 开始菜单对系统用户进行修改并运行后,通过打开一个新的命令提示符窗口可以很容易地看到这样的错误。输出不应再包含任何环境变量值引用。PATHset pathPATH%Variable%

8. The LOCAL variablePATHwas modified before on command line or in batch file.

8. LOCAL 变量PATH之前在命令行或批处理文件中被修改过。

Run set pathon 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 PATHand PATHEXT.

set path在命令行上运行或将此命令添加到命令行上方的批处理文件中,该文件无法查看环境变量的当前值PATHPATHEXT.

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 PATHand PATHEXTand the order of directories in PATHwhich means here using the command line:

最好是编码一个批处理文件为是独立于PATHPATHEXT与目录中的顺序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%\System32should 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 localPATHand PATHEXTand the batch file works always (as long as environment variable SystemRootis not also modified in the batch file which I have never seen).

存储可执行文件的任何外部命令%SystemRoot%\System32都应在具有此路径和文件扩展名的批处理文件中指定.exe。然后 Windows 命令解释器不需要使用本地搜索文件PATHPATHEXT并且批处理文件始终有效(只要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.exewith implicit using option /Kto 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.exefor processing the batch file with implicit using option /Cto 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.exewhich is usually Windows Explorer. Therefore the started command process has a local?PATHof which value is the same as the parent process had on starting cmd.exe.

在这两种情况下,Windows 都会创建启动应用程序的环境变量的副本,该应用程序cmd.exe通常是 Windows 资源管理器。因此,启动的命令进程有一个本地的? PATH其中的值与启动时父进程的值相同cmd.exe

Example:

例子:

  1. Open a command prompt window, run title Process1and run set path.
    Output is PATHand PATHEXTas currently defined for current user account in the console window having now the window title Process1.

  2. Run set PATH=%SystemRoot%\System32and next once again set path.
    Output is again PATHand PATHEXT, but with PATHcontaining only one directory now.

  3. Run start "Process2"and run in new console window with window title Process2the command set path.
    Output is PATHand PATHEXTwith 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.

  4. Run in Process2the command set PATH=and next set path.
    Output is only PATHEXTbecause localPATHdoes not exist anymore for Process2.
    This demonstrates that every process can modify its environment variables including complete deletion.

  5. Switch to Process1window, run the command set PATH=%PATH%;%SystemRoot%and next set?path.
    Output is PATHwith two directories and PATHEXT.

  6. Run the command start "Process3"and in opened window with title Process3the command set path.
    Output is PATHwith two directories as defined also for Process1and PATHEXT.

  7. Run in Process3the command set PATH=%SystemRoot%\System32.

  1. 打开命令提示符窗口,运行title Process1并运行set path
    输出PATHPATHEXT当前在控制台窗口中为当前用户帐户定义的一样,现在具有窗口标题Process1

  2. set PATH=%SystemRoot%\System32再次运行和next set path
    输出又是PATHand PATHEXT,但PATH现在只包含一个目录。

  3. start "Process2"在窗口标题为Process2的新控制台窗口中运行并运行命令set path
    输出PATHPATHEXT之前在Process1 中具有相同的值。
    这表明在启动新进程时,正在复制运行进程的当前环境变量,而不是 Windows 本身当前存储在 Windows 注册表中的内容。

  4. Process2 中运行命令set PATH=和 next set path
    输出只是PATHEXT因为本地PATH不再存在Process2
    这表明每个进程都可以修改其环境变量,包括完全删除。

  5. 切换到Process1窗口,运行命令set PATH=%PATH%;%SystemRoot%和 next set?path
    输出PATH有两个目录和PATHEXT.

  6. 运行命令start "Process3"并在标题为Process3的打开窗口中运行命令set path
    输出PATH有两个目录,同样为Process1PATHEXT.

  7. Process3 中运行命令set PATH=%SystemRoot%\System32

There are 3 command processes running with following values for localPATHwhen %SystemRoot%expands to C:\Windows:

当扩展为时,有 3 个命令进程使用以下本地值运行:PATH%SystemRoot%C:\Windows

Process1: PATH=C:\Windows\System32;C:\Windows
Process2: PATHdoes not exist at all.
Process3: PATH=C:\Windows\System32

Process1: PATH=C:\Windows\System32;C:\Windows
Process2:PATH根本不存在。
过程3PATH=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 PATHwith?value C:\Temp, or in case of there is already a userPATHenvironment variable, editPATHand?append ;C:\Tempto 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:

让我们回到三个正在运行的命令进程,并在Process1Process2Process3中运行命令set path。可以被看见:

Process1: PATH=C:\Windows\System32;C:\Windows
Process2: PATHdoes not exist at all.
Process3: PATH=C:\Windows\System32

Process1: PATH=C:\Windows\System32;C:\Windows
Process2:PATH根本不存在。
过程3PATH=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 localPATHof fourth command process has appended the directory C:\Tempnow.

从 Windows 开始菜单再打开一个命令提示符窗口并在第四个命令中运行该命令set path。可以看到第四个命令进程的local现在PATH已经追加了目录C:\Temp

Then close all four command processes and delete the added userPATHrespectively remove ;C:\Tempfrom userPATHif 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.exedoes 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_SETTINGCHANGEfrom within a command prompt window or batch file?

是否有可能通过命令提示符窗口或批处理文件中的通知来修改系统用户变量WM_SETTINGCHANGE

It is possible to modify the registry value of an environment variable using reg addcommand. But this does not result in sending WM_SETTINGCHANGEmessage to all top-level windows. Such changes done with reg addor with regeditrequire 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 setxwhich is designed for modifying a systemor uservariable and which also sends the WM_SETTINGCHANGEmessage 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 setxdoes not modify the localenvironment variable of?running command process. This must be done with using command setused 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 PATHEXTwith the list of file extensions is handled by Windows different in comparison to environment variable PATH.

与环境变量相比,PATHEXT带有文件扩展名列表的环境变量由 Windows 处理PATH

SystemPATHEXTand userPATHEXTare NOTconcatenated to localPATHEXT.

系统PATHEXT用户PATHEXT不要串接到本地PATHEXT

A userPATHEXTreplacesthe systemPATHEXTfor all processes running under environment of the account having defined a userPATHEXT.

用户PATHEXT替换系统PATHEXT下的帐户的环境中运行的所有过程具有确定一个用户PATHEXT

There is defined only a systemPATHEXTenvironment 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 NoDefaultCurrentDirectoryInExePathwith 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 PATHvariable. Perhaps you are overwriting it somewhere else in your script. Since sortis an external command, opposed to all the others in your command line like for, dir, rd, which are cmd-internal commands, the PATHvariable is needed to find the command. If PATHis not defined, external commands are searched in the current working directory only. There is also a PATHEXTvariable that is needed to define standard file extensions for executables, like .com, .exe. So when sortappears in command prompt or in a batch file, the system searches the current working directory and all directories specified by the PATHvariable for a file with the base name sortand one of the extensions specified by PATHEXT. The command sortis actually called sort.exeand is usually located in C:\Windows\System32.

最有可能的是,您弄乱了PATH变量。也许您正在脚本中的其他地方覆盖它。由于sort是一个外部命令,像您的命令行与所有的人fordirrd,这是cmd-internal命令时,PATH需要变量来查找命令。如果PATH未定义,则仅在当前工作目录中搜索外部命令。还有一个PATHEXT变量需要定义可执行文件的标准文件扩展名,例如.com, .exe. 所以当sort出现在命令提示符或批处理文件中时,系统会在当前工作目录和PATH变量指定的所有目录中搜索具有基本名称的文件sort和指定的扩展名之一PATHEXT。该命令sort实际上被调用sort.exe并且通常位于C:\Windows\System32.