China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-06 16:17
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » [Help] How to write the statement to capture the string variable entered by the screen in batch processing View 1,860 Replies 6
Original Poster Posted 2003-07-23 00:00 ·  中国 广东 肇庆 四会市 电信
高级用户
★★
Credits 578
Posts 135
Joined 2003-07-10 00:00
22-year member
UID 6709
Gender Male
Status Offline
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%

Floor 2 Posted 2003-07-23 00:00 ·  中国 香港
管理员
★★★★
專業島民
Credits 4,869
Posts 1,633
Joined 2002-12-10 00:00
23-year member
UID 465
Gender Male
Status Offline
:: 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
我的網站:http://mw16.2ya.com/ 我的網誌: http://scrappedblog.blogspot.com/
~
我的Winamp正在播放的歌曲:
Floor 3 Posted 2003-07-23 00:00 ·  中国 广东 肇庆 四会市 电信
高级用户
★★
Credits 578
Posts 135
Joined 2003-07-10 00:00
22-year member
UID 6709
Gender Male
Status Offline
Thanks, moderator! But I still don't quite understand after reading it!
Floor 4 Posted 2003-08-18 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
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?
Floor 5 Posted 2003-09-06 00:00 ·  中国 山东 烟台 福山区 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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.
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2003-09-06 00:00 ·  中国 湖北 武汉 联通
银牌会员
★★★
Credits 1,681
Posts 512
Joined 2003-08-02 00:00
22-year member
UID 7953
Gender Male
Status Offline
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!
Somehow somewhere I've got to choose.
No matter if it is win or lose.
Floor 7 Posted 2003-10-16 00:00 ·  中国 浙江 金华 电信
中级用户
★★
Credits 278
Posts 52
Joined 2003-06-10 00:00
23-year member
UID 4662
Gender Male
Status Offline
It can be operated with the INPUT applet!
Forum Jump: