Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch - ask for user input with default value

In a windows batch file I want to ask the user for an input, I want to show the user a default value which is the folder where the bat file reside. So when running the batch file the batch check the current folder and set the default variable to it then user can accept the suggested value by clicking on enter or enter a different value. i tried this code but it doesn't work, the UserInputPath is not set.

    set default=ABCD
    set /p UserInputPath=%default%
    echo %UserInputPath%
like image 404
Epligam Avatar asked Jun 06 '26 15:06

Epligam


1 Answers

You asked for user can accept the suggested value by clicking on enter or enter a different value.

Take advantage of the behavior of set /p: if the input is empty (just ENTER), the variable keeps unchanged. So you can simply set a default value:

set "UserInputPath=ABCD"
set /p "UserInputPath=Enter path or just ENTER for default [%UserInputPath%] : "
echo %UserInputPath%
like image 91
Stephan Avatar answered Jun 08 '26 14:06

Stephan