What is the reason for “X is not recognized as an internal or external command, operable program or batch...












4















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?










share|improve this question




















  • 7





    I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

    – aschipfl
    Jan 4 '17 at 1:15













  • Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

    – Hashim
    Jan 4 '17 at 1:58






  • 1





    It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

    – aschipfl
    Jan 4 '17 at 2:07






  • 1





    "sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

    – Raymond Chen
    Jan 4 '17 at 6:35






  • 1





    Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

    – aschipfl
    Jan 4 '17 at 9:48
















4















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?










share|improve this question




















  • 7





    I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

    – aschipfl
    Jan 4 '17 at 1:15













  • Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

    – Hashim
    Jan 4 '17 at 1:58






  • 1





    It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

    – aschipfl
    Jan 4 '17 at 2:07






  • 1





    "sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

    – Raymond Chen
    Jan 4 '17 at 6:35






  • 1





    Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

    – aschipfl
    Jan 4 '17 at 9:48














4












4








4


5






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?










share|improve this question
















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?







windows batch-file cmd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 at 6:03









Uwe Keim

27.5k32130212




27.5k32130212










asked Jan 4 '17 at 1:13









HashimHashim

6004929




6004929








  • 7





    I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

    – aschipfl
    Jan 4 '17 at 1:15













  • Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

    – Hashim
    Jan 4 '17 at 1:58






  • 1





    It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

    – aschipfl
    Jan 4 '17 at 2:07






  • 1





    "sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

    – Raymond Chen
    Jan 4 '17 at 6:35






  • 1





    Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

    – aschipfl
    Jan 4 '17 at 9:48














  • 7





    I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

    – aschipfl
    Jan 4 '17 at 1:15













  • Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

    – Hashim
    Jan 4 '17 at 1:58






  • 1





    It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

    – aschipfl
    Jan 4 '17 at 2:07






  • 1





    "sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

    – Raymond Chen
    Jan 4 '17 at 6:35






  • 1





    Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

    – aschipfl
    Jan 4 '17 at 9:48








7




7





I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

– aschipfl
Jan 4 '17 at 1:15







I guess your PATH variable is not set properly, or you are overwriting it elsewhere in the script, so the command interpreter does no longer know where to search sort.exe; the other commands are cmd-internal ones, so they are all found...

– aschipfl
Jan 4 '17 at 1:15















Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

– Hashim
Jan 4 '17 at 1:58





Damnit. I'd completely forgotten path was an environment variable. You're right, I defined a variable in the script called path. Can I ask how you knew it was PATH that was being overwritten? I'd never have made the connection from sort to the PATH environment variable.

– Hashim
Jan 4 '17 at 1:58




1




1





It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

– aschipfl
Jan 4 '17 at 2:07





It was the error message together with the fact that sort is the only external command in your command line that led me to that suspicion...

– aschipfl
Jan 4 '17 at 2:07




1




1





"sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

– Raymond Chen
Jan 4 '17 at 6:35





"sort is not recognized as an internal or external command..." - We know that it's not an internal command, but we expect it to be recognized as an external command. External commands are on the PATH. If it can't be found, it's because it's not on the PATH. So maybe you messed up the PATH.

– Raymond Chen
Jan 4 '17 at 6:35




1




1





Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

– aschipfl
Jan 4 '17 at 9:48





Possible duplicate of 'find' is not recognised as internal or external command, operable program or batch file

– aschipfl
Jan 4 '17 at 9:48












2 Answers
2






active

oldest

votes


















16














A) How does Windows command interpreter search for commands?



Windows command interpreter searches for a COMMAND to execute which




  1. is not an internal command of cmd.exe and

  2. is just specified with file name without file extension and without path


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




  1. first in current directory and

  2. next in all directories of local environment variable 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 name sort.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:





  1. System PATH which is used for all accounts and stored in Windows registry under key:



    HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment



  2. User PATH which is used only for current account and stored in Windows registry under key:



    HKEY_CURRENT_USEREnvironment


  3. Local PATH which is always a copy of the local PATH of parent process which started the current process.



Windows concatenates system and user PATH to local PATH 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




'...' 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 PATHEXT in current directory or any other directory in currently active environment variable PATH.





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 default Path 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 user PATH with command setx 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 after PATH modification. This is necessary as described in detail in chapter F) below.



6. 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 variables PATH and PATHEXT.



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 and PATHEXT and the order of directories in PATH which means here using the command line:



FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%System32sort.exe /r') do rd "%%d"


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 local PATH 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).





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 local PATH whose value is the same as the parent process had on starting cmd.exe.



Example:




  1. Open a command prompt window, run title Process1 and run set path.

    Output is PATH and PATHEXT as currently defined for current user account in the console window having now the window title Process1.


  2. Run set PATH=%SystemRoot%System32 and next once again set path.

    Output is again PATH and PATHEXT, but with PATH containing only one directory now.


  3. Run start "Process2" and run in new console window with window title Process2 the command set path.

    Output is PATH and PATHEXT 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.


  4. Run in Process2 the command set PATH= and next set path.

    Output is only PATHEXT because local PATH does not exist anymore for Process2.

    This demonstrates that every process can modify its environment variables including complete deletion.


  5. Switch to Process1 window, run the command set PATH=%PATH%;%SystemRoot% and next set path.

    Output is PATH with two directories and PATHEXT.


  6. Run the command start "Process3" and in opened window with title Process3 the command set path.

    Output is PATH with two directories as defined also for Process1 and PATHEXT.


  7. Run in Process3 the command set PATH=%SystemRoot%System32.



There are 3 command processes running with following values for local PATH when %SystemRoot% expands to C:Windows:



Process1: PATH=C:WindowsSystem32;C:Windows
Process2: PATH does not exist at all.
Process3: PATH=C:WindowsSystem32



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 value C:Temp, or in case of there is already a user PATH environment variable, edit PATH 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:WindowsSystem32;C:Windows
Process2: PATH does not exist at all.
Process3: PATH=C:WindowsSystem32



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 local PATH of fourth command process has appended the directory C:Temp now.



Then close all 4 command processes and delete the added user PATH respectively remove ;C:Temp from user PATH 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 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.



But there is also the command setx which is designed for modifying a system or user variable 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 local environment variable of running command process. This must be done with using command set used in addition to setx.





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 variable PATH.



System PATHEXT and user PATHEXT are NOT concatenated to local PATHEXT.



A user PATHEXT replaces the system PATHEXT for all processes running under environment of the account having a user PATHEXT 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 variable NoDefaultCurrentDirectoryInExePath with any value as written by eryksun in his comment below and explained by Microsoft in MSDN article about function NeedCurrentDirectoryForExePath.



See Removing the current working directory from the path for more details on usage of this environment variable.






share|improve this answer





















  • 3





    The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

    – eryksun
    Aug 15 '17 at 14:39






  • 4





    CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

    – eryksun
    Aug 15 '17 at 14:45






  • 4





    If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

    – eryksun
    Aug 15 '17 at 14:51











  • Many thanks to @eryksun for all this additional information for interested people like me.

    – Mofi
    Aug 16 '17 at 5:04



















5














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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41454769%2fwhat-is-the-reason-for-x-is-not-recognized-as-an-internal-or-external-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    16














    A) How does Windows command interpreter search for commands?



    Windows command interpreter searches for a COMMAND to execute which




    1. is not an internal command of cmd.exe and

    2. is just specified with file name without file extension and without path


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




    1. first in current directory and

    2. next in all directories of local environment variable 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 name sort.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:





    1. System PATH which is used for all accounts and stored in Windows registry under key:



      HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment



    2. User PATH which is used only for current account and stored in Windows registry under key:



      HKEY_CURRENT_USEREnvironment


    3. Local PATH which is always a copy of the local PATH of parent process which started the current process.



    Windows concatenates system and user PATH to local PATH 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




    '...' 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 PATHEXT in current directory or any other directory in currently active environment variable PATH.





    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 default Path 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 user PATH with command setx 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 after PATH modification. This is necessary as described in detail in chapter F) below.



    6. 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 variables PATH and PATHEXT.



    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 and PATHEXT and the order of directories in PATH which means here using the command line:



    FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%System32sort.exe /r') do rd "%%d"


    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 local PATH 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).





    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 local PATH whose value is the same as the parent process had on starting cmd.exe.



    Example:




    1. Open a command prompt window, run title Process1 and run set path.

      Output is PATH and PATHEXT as currently defined for current user account in the console window having now the window title Process1.


    2. Run set PATH=%SystemRoot%System32 and next once again set path.

      Output is again PATH and PATHEXT, but with PATH containing only one directory now.


    3. Run start "Process2" and run in new console window with window title Process2 the command set path.

      Output is PATH and PATHEXT 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.


    4. Run in Process2 the command set PATH= and next set path.

      Output is only PATHEXT because local PATH does not exist anymore for Process2.

      This demonstrates that every process can modify its environment variables including complete deletion.


    5. Switch to Process1 window, run the command set PATH=%PATH%;%SystemRoot% and next set path.

      Output is PATH with two directories and PATHEXT.


    6. Run the command start "Process3" and in opened window with title Process3 the command set path.

      Output is PATH with two directories as defined also for Process1 and PATHEXT.


    7. Run in Process3 the command set PATH=%SystemRoot%System32.



    There are 3 command processes running with following values for local PATH when %SystemRoot% expands to C:Windows:



    Process1: PATH=C:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 value C:Temp, or in case of there is already a user PATH environment variable, edit PATH 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:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 local PATH of fourth command process has appended the directory C:Temp now.



    Then close all 4 command processes and delete the added user PATH respectively remove ;C:Temp from user PATH 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 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.



    But there is also the command setx which is designed for modifying a system or user variable 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 local environment variable of running command process. This must be done with using command set used in addition to setx.





    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 variable PATH.



    System PATHEXT and user PATHEXT are NOT concatenated to local PATHEXT.



    A user PATHEXT replaces the system PATHEXT for all processes running under environment of the account having a user PATHEXT 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 variable NoDefaultCurrentDirectoryInExePath with any value as written by eryksun in his comment below and explained by Microsoft in MSDN article about function NeedCurrentDirectoryForExePath.



    See Removing the current working directory from the path for more details on usage of this environment variable.






    share|improve this answer





















    • 3





      The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

      – eryksun
      Aug 15 '17 at 14:39






    • 4





      CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

      – eryksun
      Aug 15 '17 at 14:45






    • 4





      If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

      – eryksun
      Aug 15 '17 at 14:51











    • Many thanks to @eryksun for all this additional information for interested people like me.

      – Mofi
      Aug 16 '17 at 5:04
















    16














    A) How does Windows command interpreter search for commands?



    Windows command interpreter searches for a COMMAND to execute which




    1. is not an internal command of cmd.exe and

    2. is just specified with file name without file extension and without path


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




    1. first in current directory and

    2. next in all directories of local environment variable 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 name sort.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:





    1. System PATH which is used for all accounts and stored in Windows registry under key:



      HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment



    2. User PATH which is used only for current account and stored in Windows registry under key:



      HKEY_CURRENT_USEREnvironment


    3. Local PATH which is always a copy of the local PATH of parent process which started the current process.



    Windows concatenates system and user PATH to local PATH 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




    '...' 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 PATHEXT in current directory or any other directory in currently active environment variable PATH.





    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 default Path 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 user PATH with command setx 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 after PATH modification. This is necessary as described in detail in chapter F) below.



    6. 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 variables PATH and PATHEXT.



    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 and PATHEXT and the order of directories in PATH which means here using the command line:



    FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%System32sort.exe /r') do rd "%%d"


    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 local PATH 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).





    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 local PATH whose value is the same as the parent process had on starting cmd.exe.



    Example:




    1. Open a command prompt window, run title Process1 and run set path.

      Output is PATH and PATHEXT as currently defined for current user account in the console window having now the window title Process1.


    2. Run set PATH=%SystemRoot%System32 and next once again set path.

      Output is again PATH and PATHEXT, but with PATH containing only one directory now.


    3. Run start "Process2" and run in new console window with window title Process2 the command set path.

      Output is PATH and PATHEXT 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.


    4. Run in Process2 the command set PATH= and next set path.

      Output is only PATHEXT because local PATH does not exist anymore for Process2.

      This demonstrates that every process can modify its environment variables including complete deletion.


    5. Switch to Process1 window, run the command set PATH=%PATH%;%SystemRoot% and next set path.

      Output is PATH with two directories and PATHEXT.


    6. Run the command start "Process3" and in opened window with title Process3 the command set path.

      Output is PATH with two directories as defined also for Process1 and PATHEXT.


    7. Run in Process3 the command set PATH=%SystemRoot%System32.



    There are 3 command processes running with following values for local PATH when %SystemRoot% expands to C:Windows:



    Process1: PATH=C:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 value C:Temp, or in case of there is already a user PATH environment variable, edit PATH 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:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 local PATH of fourth command process has appended the directory C:Temp now.



    Then close all 4 command processes and delete the added user PATH respectively remove ;C:Temp from user PATH 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 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.



    But there is also the command setx which is designed for modifying a system or user variable 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 local environment variable of running command process. This must be done with using command set used in addition to setx.





    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 variable PATH.



    System PATHEXT and user PATHEXT are NOT concatenated to local PATHEXT.



    A user PATHEXT replaces the system PATHEXT for all processes running under environment of the account having a user PATHEXT 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 variable NoDefaultCurrentDirectoryInExePath with any value as written by eryksun in his comment below and explained by Microsoft in MSDN article about function NeedCurrentDirectoryForExePath.



    See Removing the current working directory from the path for more details on usage of this environment variable.






    share|improve this answer





















    • 3





      The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

      – eryksun
      Aug 15 '17 at 14:39






    • 4





      CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

      – eryksun
      Aug 15 '17 at 14:45






    • 4





      If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

      – eryksun
      Aug 15 '17 at 14:51











    • Many thanks to @eryksun for all this additional information for interested people like me.

      – Mofi
      Aug 16 '17 at 5:04














    16












    16








    16







    A) How does Windows command interpreter search for commands?



    Windows command interpreter searches for a COMMAND to execute which




    1. is not an internal command of cmd.exe and

    2. is just specified with file name without file extension and without path


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




    1. first in current directory and

    2. next in all directories of local environment variable 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 name sort.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:





    1. System PATH which is used for all accounts and stored in Windows registry under key:



      HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment



    2. User PATH which is used only for current account and stored in Windows registry under key:



      HKEY_CURRENT_USEREnvironment


    3. Local PATH which is always a copy of the local PATH of parent process which started the current process.



    Windows concatenates system and user PATH to local PATH 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




    '...' 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 PATHEXT in current directory or any other directory in currently active environment variable PATH.





    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 default Path 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 user PATH with command setx 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 after PATH modification. This is necessary as described in detail in chapter F) below.



    6. 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 variables PATH and PATHEXT.



    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 and PATHEXT and the order of directories in PATH which means here using the command line:



    FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%System32sort.exe /r') do rd "%%d"


    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 local PATH 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).





    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 local PATH whose value is the same as the parent process had on starting cmd.exe.



    Example:




    1. Open a command prompt window, run title Process1 and run set path.

      Output is PATH and PATHEXT as currently defined for current user account in the console window having now the window title Process1.


    2. Run set PATH=%SystemRoot%System32 and next once again set path.

      Output is again PATH and PATHEXT, but with PATH containing only one directory now.


    3. Run start "Process2" and run in new console window with window title Process2 the command set path.

      Output is PATH and PATHEXT 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.


    4. Run in Process2 the command set PATH= and next set path.

      Output is only PATHEXT because local PATH does not exist anymore for Process2.

      This demonstrates that every process can modify its environment variables including complete deletion.


    5. Switch to Process1 window, run the command set PATH=%PATH%;%SystemRoot% and next set path.

      Output is PATH with two directories and PATHEXT.


    6. Run the command start "Process3" and in opened window with title Process3 the command set path.

      Output is PATH with two directories as defined also for Process1 and PATHEXT.


    7. Run in Process3 the command set PATH=%SystemRoot%System32.



    There are 3 command processes running with following values for local PATH when %SystemRoot% expands to C:Windows:



    Process1: PATH=C:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 value C:Temp, or in case of there is already a user PATH environment variable, edit PATH 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:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 local PATH of fourth command process has appended the directory C:Temp now.



    Then close all 4 command processes and delete the added user PATH respectively remove ;C:Temp from user PATH 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 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.



    But there is also the command setx which is designed for modifying a system or user variable 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 local environment variable of running command process. This must be done with using command set used in addition to setx.





    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 variable PATH.



    System PATHEXT and user PATHEXT are NOT concatenated to local PATHEXT.



    A user PATHEXT replaces the system PATHEXT for all processes running under environment of the account having a user PATHEXT 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 variable NoDefaultCurrentDirectoryInExePath with any value as written by eryksun in his comment below and explained by Microsoft in MSDN article about function NeedCurrentDirectoryForExePath.



    See Removing the current working directory from the path for more details on usage of this environment variable.






    share|improve this answer















    A) How does Windows command interpreter search for commands?



    Windows command interpreter searches for a COMMAND to execute which




    1. is not an internal command of cmd.exe and

    2. is just specified with file name without file extension and without path


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




    1. first in current directory and

    2. next in all directories of local environment variable 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 name sort.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:





    1. System PATH which is used for all accounts and stored in Windows registry under key:



      HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment



    2. User PATH which is used only for current account and stored in Windows registry under key:



      HKEY_CURRENT_USEREnvironment


    3. Local PATH which is always a copy of the local PATH of parent process which started the current process.



    Windows concatenates system and user PATH to local PATH 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




    '...' 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 PATHEXT in current directory or any other directory in currently active environment variable PATH.





    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 default Path 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 user PATH with command setx 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 after PATH modification. This is necessary as described in detail in chapter F) below.



    6. 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 variables PATH and PATHEXT.



    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 and PATHEXT and the order of directories in PATH which means here using the command line:



    FOR /f "delims=" %%d in ('dir /s /b /ad ^| %SystemRoot%System32sort.exe /r') do rd "%%d"


    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 local PATH 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).





    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 local PATH whose value is the same as the parent process had on starting cmd.exe.



    Example:




    1. Open a command prompt window, run title Process1 and run set path.

      Output is PATH and PATHEXT as currently defined for current user account in the console window having now the window title Process1.


    2. Run set PATH=%SystemRoot%System32 and next once again set path.

      Output is again PATH and PATHEXT, but with PATH containing only one directory now.


    3. Run start "Process2" and run in new console window with window title Process2 the command set path.

      Output is PATH and PATHEXT 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.


    4. Run in Process2 the command set PATH= and next set path.

      Output is only PATHEXT because local PATH does not exist anymore for Process2.

      This demonstrates that every process can modify its environment variables including complete deletion.


    5. Switch to Process1 window, run the command set PATH=%PATH%;%SystemRoot% and next set path.

      Output is PATH with two directories and PATHEXT.


    6. Run the command start "Process3" and in opened window with title Process3 the command set path.

      Output is PATH with two directories as defined also for Process1 and PATHEXT.


    7. Run in Process3 the command set PATH=%SystemRoot%System32.



    There are 3 command processes running with following values for local PATH when %SystemRoot% expands to C:Windows:



    Process1: PATH=C:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 value C:Temp, or in case of there is already a user PATH environment variable, edit PATH 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:WindowsSystem32;C:Windows
    Process2: PATH does not exist at all.
    Process3: PATH=C:WindowsSystem32



    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 local PATH of fourth command process has appended the directory C:Temp now.



    Then close all 4 command processes and delete the added user PATH respectively remove ;C:Temp from user PATH 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 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.



    But there is also the command setx which is designed for modifying a system or user variable 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 local environment variable of running command process. This must be done with using command set used in addition to setx.





    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 variable PATH.



    System PATHEXT and user PATHEXT are NOT concatenated to local PATHEXT.



    A user PATHEXT replaces the system PATHEXT for all processes running under environment of the account having a user PATHEXT 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 variable NoDefaultCurrentDirectoryInExePath with any value as written by eryksun in his comment below and explained by Microsoft in MSDN article about function NeedCurrentDirectoryForExePath.



    See Removing the current working directory from the path for more details on usage of this environment variable.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 28 at 6:01

























    answered Jan 4 '17 at 10:01









    MofiMofi

    28.5k83777




    28.5k83777








    • 3





      The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

      – eryksun
      Aug 15 '17 at 14:39






    • 4





      CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

      – eryksun
      Aug 15 '17 at 14:45






    • 4





      If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

      – eryksun
      Aug 15 '17 at 14:51











    • Many thanks to @eryksun for all this additional information for interested people like me.

      – Mofi
      Aug 16 '17 at 5:04














    • 3





      The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

      – eryksun
      Aug 15 '17 at 14:39






    • 4





      CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

      – eryksun
      Aug 15 '17 at 14:45






    • 4





      If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

      – eryksun
      Aug 15 '17 at 14:51











    • Many thanks to @eryksun for all this additional information for interested people like me.

      – Mofi
      Aug 16 '17 at 5:04








    3




    3





    The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

    – eryksun
    Aug 15 '17 at 14:39





    The environment variables stored in the registry are either REG_SZ or REG_EXPAND_SZ types that reference other %variables%. Because enumerating a registry key has no set order, Explorer reloads the environment in 4 passes: system REG_SZ, system REG_EXPAND_SZ, user REG_SZ, and user REG_EXPAND_SZ. The PATH value is almost always a REG_EXPAND_SZ type that's defined in terms of dynamic values and REG_SZ values. Also, the user's PATH gets appended to the system value. It's important to warn that naive use of setx.exe to modify PATH will flatten and expand this structure.

    – eryksun
    Aug 15 '17 at 14:39




    4




    4





    CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

    – eryksun
    Aug 15 '17 at 14:45





    CMD's default behavior is to first search in the current directory. We can have it skip this step by defining the environment variable NoDefaultCurrentDirectoryInExePath. Then for security we can add "." explicitly to PATH at the end, or at least after system directories. If we don't add it to PATH, then running a file in the current directory has to use an explicit relative path such as .program.exe.

    – eryksun
    Aug 15 '17 at 14:45




    4




    4





    If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

    – eryksun
    Aug 15 '17 at 14:51





    If for some reason you have an executable that's saved without an extension (.exe or any other), then you can run it in CMD by appending ";." to the PATHEXT environment variable.

    – eryksun
    Aug 15 '17 at 14:51













    Many thanks to @eryksun for all this additional information for interested people like me.

    – Mofi
    Aug 16 '17 at 5:04





    Many thanks to @eryksun for all this additional information for interested people like me.

    – Mofi
    Aug 16 '17 at 5:04













    5














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






    share|improve this answer




























      5














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






      share|improve this answer


























        5












        5








        5







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






        share|improve this answer













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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 '17 at 9:55









        aschipflaschipfl

        18.8k82856




        18.8k82856






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41454769%2fwhat-is-the-reason-for-x-is-not-recognized-as-an-internal-or-external-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python