Error on batch compiling Kotlin source code in Windows

167 Views Asked by At

In Microsoft Windows 7 I have two Kotlin source codes in C:\new directory:

  • hello1.kt
  • hello2.kt

I can compile them separately in command line which its current working directory is C:\, for example >kotlinc hello1.kt. But when I try to do batch compile, I get an error:

C:\new>kotlinc *.kt
error: source file or directory not found: *.kt

Notes:

  • Result of kotlinc .\*.kt was the same.
  • > dir *.kt lists .kt files correctly, so the problem could not be in about * wildcard.
  • $ kotlinc *.kt works in Linux without error.

I want to know what causes the problem? Is there a way for doing batch compile in Microsoft Windows?

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like kotlinc.exe does not support wildcard patterns as an argument. So there must be used kotlinc hello1.kt hello2.kt on Windows.

The shell interpreters on Linux/Mac expand a wildcard pattern like *.kt to a list of argument strings with matching file/folder names before executing the executable. So the shell interpreters on Linux/Mac do not call kotlinc with *.kt, but call the executable with hello1.kt hello2.kt.

The Windows command processor cmd.exe does not offer such a wildcard expansion in arguments list of an executable. The executable itself must support an argument with a wildcard pattern and search itself for matching files/folders.

A Windows batch file solution would be following code working for all file names except file names with an exclamation mark:

@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

A slower solution working also for .kt file names with one or more ! in file name.

@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

Please note that the list of file names is not unlimited. The maximum length for an environment variable definition is 8192 characters which includes the variable name, the equal sign, the string value assigned to the environment variable and the terminating null byte. The file names added to the list are without path. So this limitation should be no problem here as long as not several hundreds of .kt files should be compiled with one execution of kotlinc.exe.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • popd /?
  • pushd /?
  • rem /?
  • set /?
  • setlocal /?

See also: