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
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.
Can anyone help?
Most probably, you messed around with the
PATH
variable. Perhaps you are overwriting it somewhere else in your script. Sincesort
is an external command, opposed to all the others in your command line likefor
,dir
,rd
, which arecmd
-internal commands, thePATH
variable is needed to find the command. IfPATH
is not defined, external commands are searched in the current working directory only. There is also aPATHEXT
variable that is needed to define standard file extensions for executables, like.com
,.exe
. So whensort
appears in command prompt or in a batch file, the system searches the current working directory and all directories specified by thePATH
variable for a file with the base namesort
and one of the extensions specified byPATHEXT
. The commandsort
is actually calledsort.exe
and is usually located inC:\Windows\System32
.A) How does Windows command interpreter search for commands?
Windows command interpreter searches for a COMMAND to execute which
cmd.exe
andfor a file matching the pattern
command.*
and having a file extension listed in local environment variablePATHEXT
PATH
.SORT and FIND and FINDSTR and ROBOCOPY and XCOPY and 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 namesort.exe
,find.exe
,findstr.exe
,robocopy.exe
,xcopy.exe
, ...Such console applications available by default on Windows are called external commands to distinguish them better from console applications not installed with Windows operating system.
B) How is the environment variable PATH defined?
There are 3 types of
PATH
variables:System
PATH
which is used for all accounts and stored in Windows registry under key:User
PATH
which is used only for current account and stored in Windows registry under key:Local
PATH
which is always a copy of the localPATH
of parent process which started the current process.Windows concatenates system and user
PATH
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.On starting a new process the entire currently active environment variables table of running process is copied for the new process by 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.C) What does the error message mean?
The error message
always means that
the file name of a
was specified for execution most likely without file extension and without (complete) path to the executable/script file and
Windows 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
.D) What are the possible reasons for this error message?
Typical reasons are:
1. The file name of the file to execute was specified wrong due to a typing mistake.
Check character by character the name of the command/executable.
2. The current directory is different to the directory containing the file to execute.
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.3. The executable or script to run is not installed at all.
Verify the existence of the executable to run. Some installation packages work only if other packages like Java, NPM, PHP, etc. were installed before.
4. The directory of the file to execute is not in
PATH
at all.Open in Windows Control Panel the System settings window, click on Advanced system settings on left side, click on button Environment Variables and look in both lists for
Path
and their values. By defaultPath
exists only in list of System variables.5. A running process/application was not restarted after modification of system or user
PATH
.A modification of system
PATH
or userPATH
with commandsetx
or via Control Panel – System – Advanced system settings was 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 afterPATH
modification. This is necessary as described in detail in chapter F) below.6. An executable in
%SystemRoot%\System32
is not found on 64 bit 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 inSystem32
and a few only inSysWOW64
.The system
PATH
contains by default as first folder path%SystemRoot%\System32
. But which one of the two 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 redirector to the directory%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
.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 conditionif exist %SystemRoot%\Sysnative
in a batch file is always false in both environments, butif 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 bitcmd.exe
in%SystemRoot%\SysWOW64
on 64 bit Windows which can be important to know depending on the task.See also the Microsoft documentations WOW64 Implementation Details and Registry Keys Affected by WOW64.
7. The LOCAL variable
PATH
was modified before on command line or in batch file.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 variablesPATH
andPATHEXT
.The last reason is responsible for external command SORT not being found on execution of the batch file which contains somewhere above
set path=...
.E) How to avoid this error message?
Best is coding a batch file for being independent on
PATH
andPATHEXT
and the order of directories inPATH
which means here using the command line:Any external command whose 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
andPATHEXT
and the batch file works always (as long as environment variableSystemRoot
is not also modified in the batch file which I have never seen).F) When is a system or user PATH change applied to processes?
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.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.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 localPATH
whose value is the same as the parent process had on startingcmd.exe
.Example:
Open a command prompt window, run
title Process1
and runset path
.Output is
PATH
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 again
PATH
andPATHEXT
, but withPATH
containing only one directory now.Run
start "Process2"
and run in new console window with window title Process2 the commandset path
.Output is
PATH
andPATHEXT
with same values as before in Process1.This demonstrates that on starting a new process the current environment variables of running process are copied and not what Windows itself has currently stored in Windows registry.
Run in Process2 the command
set PATH=
and nextset path
.Output is only
PATHEXT
because localPATH
does not exist anymore for Process2.This demonstrates that every process can modify its environment variables including complete deletion.
Switch to Process1 window, run the command
set PATH=%PATH%;%SystemRoot%
and nextset path
.Output is
PATH
with two directories andPATHEXT
.Run the command
start "Process3"
and in opened window with title Process3 the commandset path
.Output is
PATH
with two directories as defined also for Process1 andPATHEXT
.Run in Process3 the command
set PATH=%SystemRoot%\System32
.There are 3 command processes running with following values for local
PATH
when%SystemRoot%
expands toC:\Windows
:Process1:
PATH=C:\Windows\System32;C:\Windows
Process2:
PATH
does not exist at all.Process3:
PATH=C:\Windows\System32
So what happens now on opening Control Panel – System – Advanced System Settings – Environment Variables and adding to list of User variables the new environment variable
PATH
with valueC:\Temp
, or in case of there is already a userPATH
environment variable, editPATH
and append;C:\Temp
to the value?Well, as long as the dialog window with title Environment Variables showing the two lists is opened, nothing happens on modifying the variables, until button OK is clicked to takeover all changes into Windows registry and close the window.
Let's go back to the 3 running command processes and run in Process1, Process2 and Process3 the command
set path
. It can be seen:Process1:
PATH=C:\Windows\System32;C:\Windows
Process2:
PATH
does not exist at all.Process3:
PATH=C:\Windows\System32
Nothing changed on already running processes.
No process can modify the environment variables of a 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 directoryC:\Temp
now.Then close all 4 command processes and delete the added user
PATH
respectively remove;C:\Temp
from userPATH
if having appended this directory path before.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 Variables window with button OK?
The answer on those two questions was given by eryksun in his comment.
After writing the modifications on system and user variables into registry on clicking button OK of Environment Variables window, Windows sends the WM_SETTINGCHANGE message to all top-level windows to inform the running applications about changed system parameters.
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.Is there any possibility to modify a system or user variable with notification via
WM_SETTINGCHANGE
from within a command prompt window or batch file?It is possible to modify the registry value of an environment variable using
reg add
command. But this does not result in sendingWM_SETTINGCHANGE
message to all top-level windows. Such changes done withreg add
or withregedit
require a restart of Windows (or at least a log off and log on of current user) to be taken into account at all.But there is also the command
setx
which is designed for modifying a system or user variable and which also sends theWM_SETTINGCHANGE
message to all top-level windows after registry was updated according to specified arguments. Runsetx /?
in a command prompt window for details. But please take into account thatsetx
does not modify the local environment variable of running command process. This must be done with using commandset
used in addition tosetx
.G) How is environment variable PATHEXT handled by Windows?
The environment variable
PATHEXT
with the list of file extensions is handled by Windows different in comparison to environment variablePATH
.System
PATHEXT
and userPATHEXT
are NOT concatenated to localPATHEXT
.A user
PATHEXT
replaces the systemPATHEXT
for all processes running under environment of the account having a userPATHEXT
defined.There is defined only a system
PATHEXT
environment variable by default.H) Is it possible to disable file search in current directory?
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.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 variableNoDefaultCurrentDirectoryInExePath
with any value as written by eryksun in his comment below and explained by Microsoft's documentation about function NeedCurrentDirectoryForExePathA.See Removing the current working directory from the path for more details on usage of this environment variable.