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