16

Is there a way to present a default value in a set /p statement in a Windows batch script?

For example, something like this:

set /p MyVar=My Default Value
echo $MyVar$

If the user presses Enter without typing anything else, then `MyVar gets that default value.

Thanks.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Conrad S.
  • 774
  • 4
  • 10
  • 19
  • This question has been answered several times before, like [this](https://stackoverflow.com/questions/23549048/is-there-anyway-to-have-preset-data-for-user-input-in-a-batch-file/23551029#23551029), or [this](https://stackoverflow.com/questions/29857183/how-can-i-run-cmd-from-batch-file-and-add-text-to-line-without-executing/29887523#29887523), or [this](https://stackoverflow.com/questions/40595252/add-a-pre-value-to-set-p-userinp/40597703#40597703), ... – Aacini Feb 07 '18 at 14:47
  • @Aacini Yeah might be... Interestingly I literally _always_ find those duplicates in top positions of any search result. :) But thanks to your 1st link I found what I was really looking for, i.e. not just setting a default value but also preseting that default value to the user. – Andreas Mar 15 '22 at 06:10

3 Answers3

25

This:

SET /P "MyVar=" || SET "MyVar=My Default Value"

(Read more).


You could also use this:

SET /P "MyVar="
IF NOT DEFINED MyVar SET "MyVar=My Default Value"
FatalBulletHit
  • 762
  • 6
  • 22
  • 7
    You could also preset the value because `set` won't overwrite it if the user presses enter without typing, e.g. `set "MyVar=Default Value" & set /p "MyVar=Enter value: "`. – Eryk Sun Feb 07 '18 at 03:12
  • @eryksun If the user presses enter without typing `SET "MyVar=My Default Value"` is processed (tested on Windows 10). – FatalBulletHit Feb 07 '18 at 03:16
  • @eryksun It's late... fixed it, thanks. How does it come it's more reliable, tho? – FatalBulletHit Feb 07 '18 at 03:22
  • It's a potential parsing problem with mismatched quoting if the user enters a string with double quotes. – Eryk Sun Feb 07 '18 at 03:26
8

Here is one method.

It will set %1 as myvar if defined, then skip the prompt and echo myvar else it will store Default Value as the default, but if someone types in another value, it will overwrite the default. Not you can also use %2 %3 etc.

@echo off
set "myvar=%1"
if "%myvar%"=="" set /p "myvar=Enter Value: " || set "myvar=Default Value"
echo %myvar%

to see results, save the batch and run the following from cmdline:

batchfile.cmd Default

Which will skip prompt and just echo:

Default

batchfile.cmd

Which will prompt Enter Value: just hit enter, which will then echo:

Default Value

Lastly

batchfile.cmd

and enter a value, which wil lecho the entered value.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
5

The solutions proposed above did not work for me or they worked in a very clunky way.

This is (an obvious) simple solution to the problem I applied:

SET Docdir=D:\Documents\
SET /p Docdir="Target directory (Press Enter to keep <%Docdir%>): "

It results in the following prompt:

Target directory (Press Enter to use <D:\Documents\>):

User then see what's the default value and can accept it with Enter key

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41