我建议首先阅读 Stack Overflow 问题的答案:
- “START”在哪里搜索可执行文件? https://stackoverflow.com/questions/27382626/
- “...”不被识别为内部或外部命令、可运行程序或批处理文件的原因是什么? https://stackoverflow.com/questions/41454769/
非常感谢 eryksun https://stackoverflow.com/users/205580/eryksun因为如果没有这个答案就不存在他的评论 https://stackoverflow.com/questions/41454769/what-is-the-reason-for-is-not-recognized-as-an-internal-or-external-comman#comment78348685_41461002关于上面引用的答案。
接下来我建议阅读 Microsoft Developer Network (MSDN) 文章:
- 命名文件、路径和命名空间 https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx
- NeedCurrentDirectoryForExePath 函数 https://msdn.microsoft.com/en-us/library/windows/desktop/ms684269.aspx
可以回答这个问题:是的,桌面应用程序和批处理文件可以在
- Windows Vista 和所有更高版本的 Windows 客户端版本以及
- Windows Server 2003 和所有更高版本的 Windows Server。
带有名称的环境变量NoDefaultCurrentDirectoryInExePath
必须定义为任意值,以防止在未显式使用的情况下执行存储在当前目录中的脚本(.bat、.cmd、.vbs...)或应用程序(.com、.exe).\
根据 Unix/Linux 上的要求。
环境变量NoDefaultCurrentDirectoryInExePath
可以定义为system变量以关闭在当前目录中搜索该计算机上所有帐户的脚本或应用程序。但这肯定不是一个好主意,因为它肯定会导致许多应用程序(包括安装程序和卸载程序)不再正常工作。
环境变量NoDefaultCurrentDirectoryInExePath
可以定义为user变量以关闭在当前目录中搜索使用此帐户的进程的脚本或应用程序。但这肯定也不是一个好主意。
但设置环境变量是有意义的NoDefaultCurrentDirectoryInExePath
as local在某些用例中,变量可以关闭在当前目录中搜索脚本或应用程序而无需显式使用.\
在具有内核功能的 Windows 版本上NeedCurrentDirectoryForExePath
which cmd.exe
在搜索不包含反斜杠的脚本文件或应用程序之前调用\
(或正斜杠/
) 在文件名字符串中。
Example:
@echo off
pushd "%TEMP%"
set "NoDefaultCurrentDirectoryInExePath=0"
echo @echo %%0 executed successfully.>Test1.bat
echo Calling Test1.bat ...
call Test1.bat
echo Calling .\Test1.bat ...
call .\Test1.bat
echo Starting Test1.bat ...
start /wait Test1.bat ^& timeout 5
set "NoDefaultCurrentDirectoryInExePath="
echo Calling again Test1.bat ...
call Test1.bat
del Test1.bat
popd
pause
从命令提示符窗口中执行的此批处理文件会产生当前控制台窗口的输出:
Calling Test1.bat ...
'Test1.bat' is not recognized as an internal or external command,
operable program or batch file.
Calling .\Test1.bat ...
.\Test1.bat executed successfully.
Starting Test1.bat ...
Calling again Test1.bat ...
Test1.bat executed successfully.
Press any key to continue . . .
在执行该批处理文件期间,会打开第二个控制台窗口并显示输出:
"%TEMP%\Test1.bat" executed successfully.
第二个控制台窗口将在 5 秒后自动关闭。
环境变量NoDefaultCurrentDirectoryInExePath
是用值定义的0
将临时文件的目录设置为当前目录并将当前目录路径推送到堆栈后。变量值并不重要,因为评估的只是环境变量的存在而不是它的值。
接下来是另一个具有名称的批处理文件Test1.bat
在临时文件目录中创建,该临时文件通常对当前用户没有写保护,因为这会导致很多麻烦。
第一种调用方法Test1.bat
由于环境变量而没有任何路径失败NoDefaultCurrentDirectoryInExePath
是在本地环境中定义的。
第二次调用Test1.bat
带相对路径.\
尽管存在环境变量,但仍成功。
命令START忽略NoDefaultCurrentDirectoryInExePath
如该批处理文件所证明。
然后是环境变量NoDefaultCurrentDirectoryInExePath
被删除以恢复原始 Windows 行为。
第二种调用方式Test1.bat
没有任何路径现在就成功了。
最后创建的Test1.bat
被删除,初始当前目录恢复为当前目录。
当然不可能阻止命令的执行DIR这不是脚本文件或可执行文件。这是一个内部命令cmd.exe
– Windows 命令处理器 – 分别powershell.exe
– Windows PowerShell。