Output several lines of C# code to .cs file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Alvin Seville
Posts: 1
Joined: 23 Aug 2020 02:36

Output several lines of C# code to .cs file

#1 Post by Alvin Seville » 23 Aug 2020 02:41

Hello everyone! I have this code:

Code: Select all

@echo off

set "normalColor=f"
set "errorColor=c"

color %normalColor%

set "fileNameInputMessage=Please enter non-empty program name (without extension):"
set /p "name=%fileNameInputMessage%"

if "%name%"=="" (
    call :ShowError "file name is empty"
    set /p "name=%fileNameInputMessage%"
    goto :while_file_is_not_specified_or_exist
if exist "%name%.cs" (
    call :ShowError "%name%.cs exists in the current folder"
    set /p "name=%fileNameInputMessage%"
    goto :while_file_is_not_specified_or_exist

set "languageNumberInputMessage=Please enter number corresponding to language (C# - 1, Visual Basic.NET - 2, F# - 3):"
set /p "number=%languageNumberInputMessage%"

set numberInputStatus=0

if "%number%"=="" set numberInputStatus=1
if "%number%" lss "1" set numberInputStatus=1
if "%number%" gtr "3" set numberInputStatus=1

if %numberInputStatus%==1 (
    call :ShowError "language with %number% is not supported now"
    set /p "number=%languageNumberInputMessage%"
    goto :while_language_is_not_specified

call :WriteTemplate %name% %number%

exit %errorlevel%

    color %errorColor%
    echo   error: %~1 [press any key to continue]
    pause > nul
    color %normalColor%
exit /b %errorlevel%

    if %~2 equ 1 (
        echo using System; >> "%~1.cs"
        echo.>> "%~1.cs"
        echo namespace FirstNamespace>> "%~1.cs"
        echo {>> "%~1.cs"
        echo   public class Program>> "%~1.cs"
        echo   {>> "%~1.cs"
        echo     public static void Main(string[] args)>> "%~1.cs"
        echo     {>> "%~1.cs"
        echo.      Console\.WriteLine("C# template.");>> "%~1.cs"
        echo     }>> "%~1.cs"
        echo   }>> "%~1.cs"
        echo }>> "%~1.cs"
    ) else (
        if %~2 equ 2 (
        ) else (
            if %~2 equ 3 (

exit /b %errorlevel%
You can view it on GitHub Gists also here https://gist.github.com/Alvin-Seville/1 ... 807e9f70ff.

Now my code doesn't work. Problem spans in WriteTemplate. I don't know how to fix this error.

Posts: 1859
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Output several lines of C# code to .cs file

#2 Post by penpen » 25 Aug 2020 10:09

There are two reasons why your function ":WriteTemplate" doesn't work:
1) You are using a compound statement (aka block), therefore you must escape closing parentheses ("^)") in order to prevent the command line interpreter from thinking that the block ends there. Note that you also have to escape all other special characters of the batch language ('&', '|', ... most with a preceding circumflex accent as used to escape the parentheses, except the percentage character, which you need to double: "%%").
2) You can't use an empty compound statement in batch because that is a syntax error.

You also might consider to use if-else blocks without unneccessary additional blocks and an block around all if commands to only redirect output only once:

Code: Select all

	if %~2 equ 1 (
        echo(using System;
        echo(namespace FirstNamespace
        echo(  public class Program
        echo(  {
        echo(    public static void Main(string[] args^)
        echo(    {
        echo(      Console\.WriteLine("C# template."^);
        echo(    }
        echo(  }
       ) else if %~2 equ 2 (
		rem: language 2
	) else if %~2 equ 3 (
		rem: language 3
) >> "%~1.cs"
I also like using opening parentheses after the echo command more, because it guards against some issues; see:


Post Reply