Frage

So basically I have a batch file that requires alot of user input. I was wondering if it was possible to have any filler data already present when the question is asked, and if the user needs to change something, they can go edit that data. For example

enter image description here And then the user enter their first and last name. enter image description here

But is it possible to start with a default name that the user can go back and edit if they need?

This probably isn't necessary, But this is the code I use for the user input.

Set /p "Author=Please enter your name: "

And I understand for the Author it wouldn't make much sense to have preset data, but there are other instances where it would be useful for me to do this. Is it possible?

War es hilfreich?

Lösung 2

The method below have the inconvenience that the screen must be cleared before the user enter the data, but I am working trying to solve this point:

EDIT: I fixed the detail of the first version

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

@echo off
rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "First Last"
rem Read the variable
echo -----------------------------------------------------------
set /P "Author=Please enter your name: "
echo Author=%Author%
goto :EOF

@end

WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));

For further details, see this post.

Andere Tipps

nearly impossible to edit a preset value with pure batch, but you can easily give a default value (works, because set /p is not touching the variable, if input is empty)

set "author=First Last"
set /p "author=Enter name or press [ENTER] for default [%author%]: "
echo %author%

You can set the var first and then prompt the user only if it's not defined like so:

set Author=First Last
if not defined Author set /p "Author=Please enter your name: "

You can also do this backwards where you can define a value if the user didn't define it, like so:

set /p "Author=Please enter your name: "
if not defined Author set Author=First Last

There Is another way yet to achieve this. It uses vbs script to get input and assign it to a variable, -The script can be created within and called from .bat files.

I developed this method as an alternative to accepting user input from set /p in order to validate input and prevent setting of variables with spaces or special characters. *Validation methods omitted as does not relate to the question

Best method is to have the script generator as a secondary .bat file that you can call, as it allows for a lot of versatility regarding the information the vbs input box conveys, as well as being able to be used in any circumstance within one or more .bat files you want to be able to control input defaults with.

In your main program, when Preparing to get input-

REM Define title:

Set inputtitle=The title you want the input box to have

REM Declare variable to be assigned:

Set variableforinput=VariableName

REM Define the default Variable Value:

Set defaultinput=VariableName's Desired Default Value

REM getting the Input:

CALL "inputscript.bat" %inputtitle% %variableforinput% %defaultinput%

inputscript.bat:

@echo off

SET inputtitle=%~1
SET variableforinput=%~2
SET defaultinput=%~3
SET %variableforinput%=
SET input=

:main

REM exit and cleanup once variable is successfully defined

IF DEFINED input GOTO return

REM this creates then starts the VBS input box, storing input to a text file.

(
ECHO Dim objFSO 'File System Object
ECHO Set objFSO = CreateObject("Scripting.FileSystemObject"^)
ECHO Dim objTS 'Text Stream Object
ECHO Const ForWriting = 2
ECHO Set objTS = objFSO.OpenTextFile("%userprofile%\getinput.txt", ForWriting, 
True^)
ECHO objTS.Write(InputBox("Please enter %variableforinput% to continue.","%inputtitle%","%defaultinput%",0,0^)^)
ECHO objTS.Close(^)
ECHO Set bjFSO = Nothing 'Destroy the object.
ECHO Set objTS = Nothing 'Destroy the object.
) >GetInput.vbs

START GetInput.vbs

REM a pause that allows time for the input to be entered and stored is required.

cls
ECHO (N)ext
CHOICE /T 20 /C nz /N /D n >nul
IF ERRORLEVEL ==2 GOTO main
IF ERRORLEVEL ==1 GOTO loadinput

:loadinput

IF NOT EXIST "%userprofile%\getinput.txt" GOTO invInput

<%userprofile%\getinput.txt (
SET /P input=
)

IF NOT DEFINED input GOTO invInput

REM opportunity for other validation of input before returning to main.

GOTO main

:invInput

SET input=

IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)

REM ends the vbs script ready for the next attempt to provide input

taskkill /pid WScript.exe /T >nul

Timeout 1 >nul

GOTO main

REM assigns the input value to the variable name being set in Your Main program.

:return

SET %variableforinput%=%input%

SET input=

IF EXIST "%userprofile%\getinput.txt" (
DEL /Q "%userprofile%\getinput.txt"
)

IF EXIST "GetInput.vbs" (
DEL /Q "GetInput.vbs"
)

GOTO :EOF

I wrote an open-source Windows console program called editenv that replaces my older editv32/editv64/readline.exe utilities:

https://github.com/Bill-Stewart/editenv

Basically, editenv lets you interactively edit the value of an environment variable. One of my common use cases is to edit the Path environment variable for the current process:

editenv Path

editenv also provides the following convenience features:

  • --maskinput allows you to hide the typed input (note that this feature does not provide any encryption or security)
  • --allowedchars and --disallowchars allow you to specify which characters are allowed for input
  • --minlength and --maxlength let you choose a minimum and maximum length of the input string
  • --timeout lets you specify a timeout after which input is entered automatically

The most recent binaries are available here:

https://github.com/Bill-Stewart/editenv/releases

askingFile.cmd < response.txt

Take the input to the batch from the indicated file, one line per answer

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top