Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

Well, it's not exactly what you asked for but I think it's better. This will create a browseforfolder dialog and let your user pick what folder to use. In this example r is the return variable from the function. It will return an errorlevel of 0 if a folder is chosen and 1 if the cancel button is hit.

@Echo off
setlocal
Call :BrowseFolder "Enter path to folder" "C:\scripts\" r
echo %r%
echo %errorlevel%
pause
Goto :EOF


:BrowseFolder <Title> <DefaultStartPath> <Return>
setlocal
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo set sh=wscript.CreateObject("Shell.Application") 
>>%vbs% echo set f=sh.BrowseForFolder(0,%1,0,%2) 
>>%vbs% echo if typename(f)="Nothing" Then 
>>%vbs% echo   wscript.echo "Dialog Cancelled" 
>>%vbs% echo   wscript.Quit(1) 
>>%vbs% echo end if 
>>%vbs% echo set fs=f.Items():set fi=fs.Item() 
>>%vbs% echo p=fi.Path:wscript.echo p
for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do set result=%%a
if exist %vbs% del /f /q %vbs%
if "%result%" EQU "Dialog Cancelled" (set a=1) else set a=0
endlocal & set %3=%result% & exit /b %a%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top