- Как запросить и получить пользовательский ввод в файле. bat и использовать его для запуска определенной программы?
- 8 ответов:
- User Input
- Windows 2000 and later versions
- Windows NT 4 and later versions
- MS-DOS
- Batch Tools
- KiXtart
- PowerShell
- PowerShell on-the-fly
- VBScript
- Take input a batch file and output that input to another batch file
- 2 Answers 2
Как запросить и получить пользовательский ввод в файле. bat и использовать его для запуска определенной программы?
Это то, что у меня есть до сих пор
В любом случае, если пользователь вводит y или n, он всегда работает в режиме-dev.
Как заставить его работать в режиме dev, если ответ-да, и в обычном режиме, если ответ-нет. Кроме того, как я могу заставить его спросить снова, если входные данные не Y, N, y или n?
8 ответов:
Если входные данные, скажем, N , ваши строки IF оцениваются следующим образом:
То есть вы сравниваете N с «y» , затем «n» и т. д. в том числе «N» . Вы никогда не получите совпадение, если пользователь каким-то образом не решит ввести «N» или «y» (то есть любой из четырех символов, но заключенный в двойные кавычки).
Так что вам нужно либо удалить » со всего y , n , Y и N или поместите их вокруг %INPUT% в ваших условных утверждениях. Я бы рекомендовал последнее, потому что таким образом вы бы экранировали по крайней мере некоторые символы, которые имеют особое значение в пакетных сценариях (если пользователю удалось их ввести). Итак, вот что вы должны получить:
Кстати, вы можете уменьшить число условий, применив переключатель /I к оператору IF , например:
Переключатель /I делает сравнение нечувствительным к регистру, и поэтому вам не нужны отдельные проверки для строк с разными регистрами.
Один другая проблема заключается в том, что после выполнения команды режима разработки нет возможности перепрыгнуть через другую команду, и поэтому, если пользователь соглашается запустить Java в режиме разработки, он получит ее запуск как в режиме разработки, так и в режиме без разработки. Поэтому, возможно, вам нужно добавить что-то вроде этого в свой скрипт:
Наконец, чтобы решить проблему обработки неверных входных данных, вы можете просто добавить другую (безусловную) команду goto сразу после условных операторов, просто перед меткой yes , а именно goto Ask , чтобы вернуться к началу вашего скрипта, где отображается приглашение и запрашивается ввод, или вы могли бы также добавить другую команду ECHO перед прыжком, объясняя, что ввод был неправильным, что-то вроде этого:
Примечание: некоторые из упомянутых здесь вопросов также были рассмотрены @xmjx в своем ответе, который я полностью признаю.
User Input
Sometimes we need some user interaction in our batch files.
We may need to know to which directory a file is to be copied, for example.
Or which drive needs to be formatted.
There are many ways to achieve this user interaction.
The most basic form of user interaction, of course, is the PAUSE command, which halts the batch file until the user presses «any key» (apart from Ctrl, Alt, Shift, CapsLock, NumLock or ScrollLock).
Maybe not really sophisticated, but it works and is always available, in all DOS, Windows and OS/2 versions.
How you can manage to retrieve user input depends on the OS version used, and whether you want to allow «third party» tools or use «native code» only.
Windows 2000 and later versions
As of Windows 2000, user input can be obtained quite easily by using SET /P
This command will display an optional promptString where the user can type in a string and press Enter. The typed string will then be stored in the specified environment variable variable.
Warning: | Working with a stored string containing ampersands and doublequotes may cause your batch file to fail or even to execute «unexpected» code (code insertion). Do not use SET /P in batch files running with elevated privileges! |
Windows NT 4 and later versions
In NT we don’t need temporary files and we can skip a few lines by using TYPE CON and FOR /F :
It is still just as un-intuitive as the first COPY CON example, though.
Update: | Replace TYPE CON by MORE in the above NT batch file and you can save yourself pressing Enter once — you’ll need to press F6, Enter only instead of Enter, F6, Enter, though the latter will still work. [Tip posted by «Frank» on alt.msdos.batch.nt] |
Tom Lavedas’ New and Improved Data Input Routine! shows a way to use a graphical box to ask for user input in Windows 95
Duke Communications International, Inc.’s tip #0323: How can I get a batch file to prompt me for parameters? shows a way to get a graphical box asking for user input in Windows NT
My own DialogBoxes (executables, written in C#) can be used to display messages and ask for input in Windows XP and later versions.
Walter Zackery posted two interesting solutions to obtain user input in NT, on the alt.msdos.batch.nt news group.
One solution uses the FORMAT command, which will fail since it tries to format a diskette in drive A: at 160KB.
If you have a 5¼» drive as your A: drive don’t use this one.
The second solution uses the LABEL command.
It will actually change drive C:’s volume label and then restore it again to its old value.
The volume label in NT is restricted to 32 characters, and so is the input string when using this LABEL trick for user input.
Besides that the batch file limits the string to 2 words (1 space) only.
I adapted the original postings so that the batch files no longer need to use temporary files. You can view the original postings at my Solutions found at alt.msdos.batch page.
Clay Calvert posted another interesting solution to obtain Yes or No input in most DOS versions by using DEL’s /P switch (prompt for confirmation).
Another great solution by Eric Phelps uses a temporary HTA file to obscure a password while it is being typed.
MS-DOS
In the MS-DOS 3 days, a simple Yes/No question could be answered by changing to a temporary directory where two temporary batch files were located, Y.BAT and N.BAT.
Guess what happened if a user typed a completely different answer . . .
Since MS-DOS 6 we have CHOICE.COM ( CHOICE.EXE in later versions), a much more versatile and reliable way to solve one character answers like Yes/No.
The CHOICE command was discontinued in Windows NT 4, only to be reintroduced again in Windows 7 (and maybe Vista?).
If you work with one of these «challenged» Windows versions, you may want to try my Poor Man’s Choice instead, or use DEBUG to create REPLY.COM (16-bit, won’t work in a 64-bit OS), as published in Microsoft Knowledge Base article Q77457: Accepting Keyboard Input in Batch Files.
There is another way to receive user input: COPY CON
copies the user input on the command line to the file filename.
To stop entering user input, the user has to type Ctrl+Z (or F6), followed by the Enter key.
Many PC users and batch file authors (including myself), find this approach «less intuitive», to say the least. One would expect that pressing the enter key is enough, and once you find out it isn’t, the previous line of input cannot be removed anymore.
The following trick uses ANSI to perform some key translation: the Enter key is translated to the F6 key followed by the Enter key. Thus only one line of input can be entered, and pressing the Enter key sends the input to the temporary file USERINP.TMP.
Note: | The ← character is the Esc character, or ASCII character 27 (or 1B Hexadecimal). It is a representation of the Esc key. This Esc character is not to be confused with escape characters! |
How to create the Esc character depends on the editor you use:
• In EDIT, use Ctrl+P followed by Alt+27 (press Alt , keep it pressed, and then type 27 on the numeric key pad).
• In Notepad or Wordpad, copy and paste the Escape character from some reference file you can create using EDIT.
• In Norton Commander or File Commander’s editor, use Ctrl+Q followed by Alt+27 on the numeric key pad.
• In E and EPM (OS/2) just type Alt+27 on the numeric key pad.
• In UltraEdit, press Ctrl+I followed by the Esc key.
You may have to change the codepage to 437 in some editors.
The previous example is only a bare minimum. The following example not only asks for user input, but stores it in an environment variable USRINPUT as well:
The previous batch file should work in every DOS version, assuming ANSI.SYS (or one of its replacements, like ANSI.COM ) is loaded.
With a few minor adjustments (replace .BAT with .CMD everywhere) it can be used in OS/2 as well. Use READLINE instead, however, in OS/2’s DOS sessions.
The following batch file checks if ANSI.SYS is loaded. If so, it will tell the user to press the Enter key only. If not, it will tell the user to press F6 first, followed by the Enter key.
However, to check if ANSI.SYS is loaded, this batch file needs MS-DOS 6 or later.
Batch Tools
There are many batch utilities on the web that help ask for user input.
I will list some of them here:
- ASK
Part of the «DOS Utilities Collection» by Jem Berkes. - WBAT
Dialog boxes for DOS batch (menus, buttons, input fields, checkboxes, radio buttons, list selection) by Horst Schaeffer. - InputBox.exe is a batch tool I wrote in C# to popup an InputBox, like VBScript, with an extra option (width).
MessageBox.exe is a batch tool I wrote in C# to display messages in a MessageBox, like VBScript, with more options.
KiXtart
A non-batch solution for Windows 95/98/NT/2000 users is the GetS( ) function in KiXtart:
OS/2 users may want to take a look at UserInPM, a utility written in VX-Rexx, displaying a small PM window asking for user input.
It creates a temporary batch file to set an environment variable to the typed input.
An example batch file, with the resulting PM window:
You will need the VX-Rexx runtime library VROBJ.DLL to run UserInPM.
PowerShell
For a login dialog, you can also use the following PowerShell code:
Save it as login.ps1, and use it to return the user name and password to a batch file:
My LoginDialog.ps1 is based on the code above, with some error handling added:
With LoginDialog.ps1, catching a login error in the batch file is easy:
My GetHDDStatusGUI.ps1 combines several GUI techniques:
PowerShell on-the-fly
It is possible to write batch files using PowerShell without requiring a (temporary) PowerShell script.
This is where PowerShell’s -Command switch comes in handy.
However, don’t expect it to make your code readable, and maintenance will be a real nightmare.
The messagebox below is a fairly simple example:
Notes: | 1. | The code above is wrapped to allow a quick overview, but should be entered as a single line in a batch file. |
2. | Use single quotes for strings inside the PowerShell code, and backquotes for the batch file’s FOR command. | |
3. | Do not use backquotes for PowerShell literals! If you have to use PowerShell literals, enter them as [char] as shown in the following code: |
( ‘Are you sure you want to continue? <0>Really, really sure?’ -f [char]0x0D ) instead of ‘Are you sure you want to continue? `n Really, really sure?’
OK, even simpler, a plain popup message:
One more example, using the code of our previous PowerShell login dialog example:
Notes: | 1. | The code above is wrapped to allow a quick overview, but should be entered as a single line in a batch file. |
2. | Credentials are stored in the environment variables Login.Name and Login.Passwd |
VBScript
Advanced dialog boxes, output and input, including (masked) password prompts, can also be created using VBScript and Internet Explorer, but I would not recommend creating them on the fly.
See the VBScript Scripting Techniques section for some of these advanced user message windows.
Take input a batch file and output that input to another batch file
Sorry for the messy title.
I want to efficiently take input from another batch file/window (example: a choice command inside a batch file) and send it to the other batch file. Essentially it would «type» it in the other window. Now, I can’t just have the first batch file open a new one, because if I did that I would just open another java instance
The code for the first batch file:
The code for the second batch file:
Something like echo stop > task start.bat
Does anyone have any ideas on how I can achieve this? I’m willing to use anything other than batch for the first file (since the second one requires it to be a batch script) but I don’t want to download anything.
2 Answers 2
I am assuming that your java server is a console process that accepts redirected stdin input. I’m also assuming your server process will terminate if reading from a file and it reaches the end of the input file. I’m also assuming you do not want to modify your java code.
You cannot use a pipe to communicate between two windows. But you can use a file.
Your master batch script can write commands to an input file that are read by the server. But the server cannot read the commands directly. A sender batch script is needed to relay the commands to the server.
The master script launches the server by STARTing a send script with input redirected to the input file, and the send scripts output is piped to your server process.
The send script is a loop that repeatedly uses FOR /F to read the input file via FINDSTR. FINDSTR reads stdin (which has already been redirected to the input file). The reason this works is because FINDSTR does not reset the file pointer each time it is called. If there are no new commands, then it returns nothing. But if a line has been added to the input file, then FINDSTR will pick it up.
Each command that is added to the input file consists of two parts that are delimited by a colon.
- A command for the send batch script
- GO tells the send script it is a normal command for the server
- EXIT tells the send script that it should shut down
- The command for the server
I’ve opted to combine the master script and sender script into a single script. The script launches the sender by calling itself with a :send argument. If the first argument is :send, then it branches to the sender code.
I don’t have your java server code. So for demonstration purposes I have used cmd.exe as my «server». Obviously the commands in my :menu are geared for cmd.exe. You can substitute your java program and change the commands appropriately.
Note that the server window will automatically steal focus when it is STARTed. You must click on the parent window to regain focus so you can enter commands.
Note that the :send routine is continuously polling stdin, so it will peg a processor (or processor core) at nearly 100%. You could add a PING delay in the loop so that it does not hog CPU resources, but then the server will not be as responsive.
If your server process does not automatically terminate when it reaches the end of redirected input, then you don’t need a sender batch process. You can simply launch your server within your master script using
Or if it currently terminates upon reaching end of file, but you are willing to modify the server code, then you could still do the above. You could modify your java code to continuously poll for input, only reading a line if not at end of file. The process would only terminate when it receives some type of exit command.
If your server is not a console process, then you will need something like AutoIt to send commands to your server window. Or you could roll your own solution with VBScript or JScript and the SendKeys method.
Even if your server process is a console process, you still might want to use AutoIt or SendKeys, as it is more straight forward.