看起来kotlinc.exe
不支持通配符模式作为参数。所以必须kotlinc hello1.kt hello2.kt
在Windows上使用。
Linux/Mac 上的 shell 解释器在*.kt
执行可执行文件之前将通配符模式扩展为具有匹配文件/文件夹名称的参数字符串列表。因此,Linux/Mac 上的 shell 解释器不使用 调用kotlinc
,*.kt
而是使用 调用可执行文件hello1.kt hello2.kt
。
Windows 命令处理器cmd.exe
在可执行文件的参数列表中不提供这样的通配符扩展。可执行文件本身必须支持带有通配符模式的参数,并自行搜索匹配的文件/文件夹。
Windows 批处理文件解决方案将遵循适用于所有文件名的代码,但带有感叹号的文件名除外:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Make the directory of the batch file the current directory.
pushd "%~dp0" || goto :EOF
if exist *.kt goto CreateFilesList
echo ERROR: There is no *.kt file in folder: "%~dp0"
echo/
pause
goto EndBatch
:CreateFilesList
set "FilesList="
for %%I in (*.kt) do set FilesList=!FilesList! "%%I"
rem The files list starts with a space character.
kotlinc.exe!FilesList!
if errorlevel 1 echo/& pause
:EndBatch
rem Restore the initial current directory.
popd
endlocal
一种较慢的解决方案也适用于.kt
文件名中包含一个或多个!
的文件名。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Make the directory of the batch file the current directory.
pushd "%~dp0" || goto :EOF
if exist *.kt goto CreateFilesList
echo ERROR: There is no *.kt file in folder: "%~dp0"
echo/
pause
goto EndBatch
:CreateFilesList
set "FilesList="
for %%I in (*.kt) do call :AddToList "%%I"
goto RunKotlinC
:AddToList
set FilesList=%FilesList% %1
goto :EOF
:RunKotlinC
rem The files list starts with a space character.
kotlinc.exe%FilesList%
if errorlevel 1 echo/& pause
:EndBatch
rem Restore the initial current directory.
popd
endlocal
请注意,文件名列表不是无限的。环境变量定义的最大长度为 8192 个字符,包括变量名称、等号、分配给环境变量的字符串值和终止空字节。添加到列表中的文件名没有路径。所以这个限制在这里应该没有问题,只要不.kt
应该用一次执行来编译数百个文件kotlinc.exe
。
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
popd /?
pushd /?
rem /?
set /?
setlocal /?
也可以看看: