简体   繁体   English

无论如何要在批处理文件中为用户输入预设数据?

[英]Is there anyway to have preset data for user input in a batch file?

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例如

在此处输入图片说明And then the user enter their first and last name.然后用户输入他们的名字和姓氏。在此处输入图片说明

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?是否可以?

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 /p不接触变量,如果输入为空)

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

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 .有关更多详细信息,请参阅此帖子

You can set the var first and then prompt the user only if it's not defined like so:您可以先设置 var,然后仅当它没有像这样定义时才提示用户:

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

Another possibility is to use the editv32.exe or edit64.exe program ( http://www.westmesatech.com/editv.html ), which makes this very simple:另一种可能性是使用 editv32.exe 或 edit64.exe 程序( http://www.westmesatech.com/editv.html ),这使得这非常简单:

set NAME=Gill Bates
editv32 -p "Enter your name: " NAME

editv32.exe/editv64.exe are unrestricted copyrighted freeware. editv32.exe/editv64.exe 是不受限制的受版权保护的免费软件。

Another alternative is ReadLine.exe:另一种选择是 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 .您可以从http://www.westmesatech.com/misctools.html获取它。 Source code is included.包含源代码。

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.它使用 vbs 脚本来获取输入并将其分配给一个变量, - 该脚本可以在 .bat 文件中创建和调用。

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.我开发了这种方法作为从 set /p 接受用户输入的替代方法,以验证输入并防止设置带有空格或特殊字符的变量。 *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.最好的方法是将脚本生成器作为您可以调用的辅助 .bat 文件,因为它允许在 vbs 输入框传达的信息方面具有很多多功能性,并且能够在一个或多个环境中的任何情况下使用。您希望能够控制输入默认值的更多 .bat 文件。

In your main program, when Preparing to get input-在您的主程序中,当准备获取输入时-

REM Define title: REM 定义标题:

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

REM Declare variable to be assigned: REM 声明要赋值的变量:

Set variableforinput=VariableName

REM Define the default Variable Value: REM 定义默认变量值:

Set defaultinput=VariableName's Desired Default Value

REM getting the Input: REM 获取输入:

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

inputscript.bat:输入脚本.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成功定义变量后,REM 退出并清理

IF DEFINED input GOTO return

REM this creates then starts the VBS input box, storing input to a text file. REM 这将创建然后启动 VBS 输入框,将输入存储到文本文件中。

(
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. REM 需要暂停,以便有时间输入和存储输入。

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. REM 机会在返回 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 REM 结束 vbs 脚本准备下一次尝试提供输入

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. REM 将输入值分配给在您的主程序中设置的变量名称。

: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:我编写了一个名为editenv的开源 Windows 控制台程序,它替换了我旧的editv32 / editv64 / readline.exe实用程序:

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

Basically, editenv lets you interactively edit the value of an environment variable.基本上, editenv允许您以交互方式编辑环境变量的值。 One of my common use cases is to edit the Path environment variable for the current process:我的一个常见用例是编辑当前进程的Path环境变量:

editenv Path

editenv also provides the following convenience features: editenv还提供以下便利功能:

  • --maskinput allows you to hide the typed input (note that this feature does not provide any encryption or security) --maskinput允许您隐藏键入的输入(请注意,此功能不提供任何加密或安全性)
  • --allowedchars and --disallowchars allow you to specify which characters are allowed for input --allowedchars--disallowchars允许您指定允许输入的字符
  • --minlength and --maxlength let you choose a minimum and maximum length of the input string --minlength--maxlength允许您选择输入字符串的最小和最大长度
  • --timeout lets you specify a timeout after which input is entered automatically --timeout允许您指定超时时间,在此之后自动输入输入

The most recent binaries are available here:最新的二进制文件可在此处获得:

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

askingFile.cmd < response.txt

从指定的文件中获取批处理的输入,每个答案一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM