Im making a .bat game, and currently when putting in a command the code is
set /p command=
What i want to know is if you can somehow have
It is possible to mix a batch file with something else, for example c#. As .net is installed on almost all windows pc nowadays, that should not be a big problem.
In the example below there is a 3 second delay where the user can enter some input. If nothing is entered, the program continues, but %result%
will be empty.
/* 2>NUL
@echo off
REM cls
set WinDirNet=%WinDir%\Microsoft.NET\Framework
IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
%csc% /nologo /out:"%~0.exe" %0
echo enter some text:
set result=
for /F "tokens=*" %%a in ('"%~0.exe"') do set result=%%a
echo you have entered:%result%
del "%~0.exe"
goto :eof
*/
using System;
using System.Text;
using System.IO;
using System.Threading;
class Program
{
static void Main (string[] args)
{
byte[] buffer=new byte[80];
using (Stream s = Console.OpenStandardInput ()) {
ManualResetEvent e=new ManualResetEvent(false);
s.BeginRead (buffer, 0, buffer.Length, x => e.Set(), null);
e.WaitOne (3000);
}
Console.WriteLine (Encoding.UTF8.GetString (buffer));
}
}
This way you can program your batch file, and use c# for everything what is not possible in batch files. Note there are several improvements possible to this code.
See How to add a Timeout to Console.ReadLine()? for improvements of the c# code.
(Source of embedded c# code)