Rik Schaaf
Rik Schaaf

Reputation: 1183

Put editable text in command prompt with bat file

I have a bat script that asks for user input with the next line

SET /P returnString=Enter string: %=%

Now I want default input visible on the command line, like:

Enter string: defaultstring

To clarify: I don't want a default value if no input is given, I want the default value visible and editable on the command line, so in the case described above the defaultstring could be replaced for a different text.

Is this possible with SET in a batch file? If not, how could I accomplish this?

Upvotes: 2

Views: 1538

Answers (1)

David Ruhmann
David Ruhmann

Reputation: 11367

Here is a Batch + JScript hybrid script that prompts with a default value.

@if (@CodeSection == @Batch) @then

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: The first line in the script is...
:: in Batch, a valid IF command that does nothing.
:: in JScript, a conditional compilation IF statement that is false.
::             So the following section is omitted until the next "[at]end".
:: Note: the "[at]then" is required for Batch to prevent a syntax error.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Section

@echo off
setlocal
set "Input="
set "Default=Hello World"
title MyUniqueTitle
CScript //E:JScript //Nologo "%~f0" "MyUniqueTitle" "%Default%"
set /p "Input=> Prompt: "
if defined Input set "Input=%Input:"=%"
echo(%Input%
endlocal
exit /b 0

:: End of Batch
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@end
////////////////////////////////////////////////////////////////////////////////
// JScript Section

try
{
    var WshShell = WScript.CreateObject('WScript.Shell');
    var Title = WScript.Arguments.Item(0);
    var Message = WScript.Arguments.Item(1);
    WshShell.AppActivate(Title);
    WshShell.SendKeys(Message);
    WScript.Quit(0);
}
catch(e)
{
    WScript.Echo(e);
    WScript.Quit(1);
}
WScript.Quit(2);

Upvotes: 3

Related Questions