![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 23:42 |
47,812 topics / 349,917 posts / today 0 new / 48,274 members |
| DOS学习入门 & 精彩文章 (教学室) » [Help] How to write the statement to capture the string variable entered by the screen in batch processing |
| Printable Version 2,014 / 6 |
| Floor1 hzy | Posted 2003-07-23 00:00 |
| 高级用户 Posts 135 Credits 578 | |
|
I want to write a batch like this:
Input "string" through the screen, then assign this %string% variable to the following command, how to write it? What tools are needed? for %a in (c: d: e: f: g: h do Dir /B /S %a\%string% |
|
| Floor2 Roy | Posted 2003-07-23 00:00 |
| 管理员 Posts 1,633 Credits 4,869 | |
|
:: Get user input - includes error check
@echo off echo e102'set %%1='> %temp%.\t1.dat for %%? in (w q) do echo %%?>> %temp%.\t1.dat :input echo. echo Type input: FC con nul /lb1 /n |FIND "1:" > %temp%.\t2.bat :: Error checking begins here FIND "|" %temp%.\t2.bat > nul if not errorlevel=1 goto error FIND "=" %temp%.\t2.bat > nul if not errorlevel=1 goto error FIND " nul if not errorlevel=1 goto error FIND ">" %temp%.\t2.bat > nul if not errorlevel=1 goto error :: Error checking end DEBUG %temp%.\t2.bat nul call %temp%.\t2.bat 字符串 for %%? in (t1.dat t2.bat) do del %temp%.\%%? echo 字符串=%字符串% goto eof :error echo. echo @PROMPT Invalid input. Invalid characters: $Q $L $G $B$_> %temp%.\t2.bat %comspec% /c %temp%.\t2.bat |FIND ":" goto input :eof |
|
| Floor3 hzy | Posted 2003-07-23 00:00 |
| 高级用户 Posts 135 Credits 578 | |
|
Thanks, moderator! But I still don't quite understand after reading it!
|
|
| Floor4 GOTOmsdos | Posted 2003-08-18 00:00 |
| 铂金会员 Posts 1,827 Credits 5,154 | |
|
First of all, I think the batch processing method generally can't meet the requester's requirement (otherwise a complex script would have to be compiled). Can we be flexible, for example, if we want to use the "%wer%" variable, we need to set it in advance (except in the autoexec.bat and config.sys, we can also enter "set wer=dfhgire.txt" on the screen at the moment. Does this meet the requester's requirement of entering a string on the screen?), otherwise the requester's requirement is: entering only this "variable name" (wer) on the screen, which is in contradiction with the usage of the "%--%" variable in batch processing. In fact, there is another variable that can meet the requester's basic intention: %1-%9. Write the batch processing: for %a in (c: d: e: f: g: h
do dir /b /s %a\%1, and then enter dfhgire.txt on the screen. Is this basically meeting the requester's requirement? |
|
| Floor5 willsort | Posted 2003-09-06 00:00 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re hzy:
As GOTOmsdos said, to achieve the requester's requirement with batch processing composed solely of basic DOS commands, a rather complex script needs to be written. Roy's example can be seen as such. Usually in such cases, what we need to do is resolve the contradiction and handle it flexibly rather than just stubbornly proceeding. In fact, Roy's example is of no other value except to make people surprised, amazed, and confused. Although I myself also like to make others surprised, amazed, and confused : ) Let's provide another flexible idea. Usually, in batch processing, to obtain a string, the simplest and most commonly used method is to use command-line parameters. We can display the help text when the user doesn't enter a parameter string and give the correct parameter usage; when an incorrect parameter string is entered, give an error prompt. It's not difficult to do this. Or, if it's foreseen that the optional fields of the string are not very large, then we can use "single-choice": echo 1. *.txt 2. *.tmp 3. *.exe choice /c:123 /n Alternatively, if you just want to obtain a string halfway through the batch processing run and it exists in an environment variable, then you need a tool: askenv.exe. The script tools of "New DOS Era" can be downloaded. In addition, correct a small bug: "Enter 'string' through the screen" The screen is an output device and cannot "enter" a string. Entering a string is usually the task of the keyboard. |
|
| Floor6 iceboy | Posted 2003-09-06 00:00 |
| 银牌会员 Posts 512 Credits 1,681 | |
|
From the above batch program, we can learn a lot...
For example, if you are not sure whether %TEMP% is C:\ or C:\TEMP, use %TEMP%\. If it is C:\, it becomes C:\.\ If it is C:\TEMP, it becomes C:\TEMP\.. Both are correct! |
|
| Floor7 jhhjwok | Posted 2003-10-16 00:00 |
| 中级用户 Posts 52 Credits 278 | |
|
It can be operated with the INPUT applet!
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |