Following the original idea from Aacini. I've not tested it under all the posible scenarios, but seems to work.
EDITED - Code updated to detect Ctrl+C key press while editing the line. Call to the subroutine returns error level to signal it.
@if (@this == @isBatch) @then
@echo off
setlocal enableextensions
call :prefilledSet/P data "Edit this:" "This is the data that must be edited"
if not errorlevel 1 (
echo(Data retrieved: [%data%]
) else (
echo(
echo( ---- Input has been canceled
)
endlocal
exit /B
:prefilledSet/P variable "Prompt" "Prefill value"
setlocal enableextensions disabledelayedexpansion
set "line="
<nul set /p "v=%~2"
for /f "delims=" %%a in ('cscript //nologo //e:jscript "%~f0" "%~3"'
) do if not defined line ( set "line=%%a" & set "exitCode=0"
) else if "%%a"=="ERROR" ( set "line=" & set "exitCode=1" )
endlocal & set "%~1=%line%" & exit /b %exitCode%
@end
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
try {
WScript.StdOut.WriteLine( WScript.StdIn.ReadLine() );
} catch (e) {
WScript.StdOut.WriteLine('ERROR\r\nERROR'); WScript.Quit(1)
};
EDITED - Just to add another option. But sorry, not directly batch code. If you have the option to compile a simple tool (tested with mingw gcc compiler), this c code
#define _WIN32_WINNT 0x0500
#include "windows.h"
int main(int argc, char **argv){
HWND hWnd = NULL;
int i;
if (argc < 2) return 1 ;
hWnd = GetConsoleWindow();
for (i=0;i<strlen(argv[1]);i++){
PostMessage(hWnd, WM_CHAR, argv[1][i], 0);
};
return 0;
}
when compiled will generate a command line tool that will send the text given as first argument to the current console. So, it can be used as (ex. compiled as typetext.exe)
typetext.exe "this is the text to edit" & set /p "var=edit this text:"
As the text is directly sent to the console in where the tool is running, there is no interference with other window being active while this executes.