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
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
It also has some other handy options, such as masking the typed input[*], limiting the input length, allowing and disallowing entry of characters, etc.
The most recent binaries are available here:
https://github.com/Bill-Stewart/editenv/releases
[*] The input masking feature is not secure and is provided for convenience only.
Another possibility is to use the editv32.exe or edit64.exe program (http://www.westmesatech.com/editv.html), which makes this very simple:
set NAME=Gill Bates
editv32 -p "Enter your name: " NAME
editv32.exe/editv64.exe are unrestricted copyrighted freeware.
Another alternative is ReadLine.exe:
@echo off
set NAME=First Last
for /f "delims=" %%n in ('ReadLine -p "Enter name: " -- %NAME%') do set NAME=%%n
echo You entered: %NAME%
You can get it from http://www.westmesatech.com/misctools.html. Source code is included.
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
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.