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-08-01 15:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » If you get input variables in a batch processing View 1,668 Replies 8
Original Poster Posted 2003-03-19 00:00 ·  中国 辽宁 朝阳 联通
初级用户
Credits 105
Posts 1
Joined 2003-03-19 00:00
23-year member
UID 1193
Gender Male
Status Offline
I want to make a batch processing to achieve that when running, it requires entering a string, and then turns to the corresponding branch according to this string. (It must be entered during running, and the form of adding parameters at the beginning cannot be used). How should I do it?

I started to want to use environment variables, and use a batch processing enhancement program INPUT to accept keyboard input, but it didn't work. My program is:
cls
input "Please Input:" FN /s rem: This sentence is to accept input and assign it to "FN"
if FN=="aaa" goto a
if FN=="bbb" goto b
goto c

:a
echo This is a
goto c

:b
echo this is b
goto c

:c
End!!!
Later, I found that the batch processing enhancement command INPUT didn't work.

Dear experts, please help.
Floor 2 Posted 2003-03-19 00:00 ·  中国 广东 佛山 顺德区 电信
中级用户
Credits 223
Posts 36
Joined 2003-01-08 00:00
23-year member
UID 707
Gender Male
Status Offline
You can consider using choice. It's very simple to use. Just type choice /? to see how to use it.
Floor 3 Posted 2003-03-27 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
To PLATCH:

Running and accepting string variables is a major weakness of bat. If there are no other enhanced commands to help, simply relying on the internal and external commands that come with DOS to achieve this feature, the amount of code will be astonishing (I have tried it myself), and the value is not very great.

When I encounter such problems, I generally convert it into accepting string parameters from the command line, and then transfer to the corresponding subroutine site. This is not too difficult to implement:

:start
find ":%1" > nul
if errorlevel 0 goto error
goto %1

:proc1
......

:proc2
......

:error
echo Invalid argument!

:end

If you must implement this function, I can give you a suggestion. Throw out a DOS sub-SHELL from the main program with %comspec%, pass in the string disguised as the command line to the program, and then implement the subroutine as an independent bat program and call it directly. Maybe I didn't make it very clear. If you want to discuss this problem, contact me again!

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2003-03-27 00:00 ·  加拿大 安大略省 多伦多
系统支持
★★★★★★
“新DOS时代”站长
Credits 27,736
Posts 10,521
Joined 2002-10-09 12:00
23-year member
UID 9
Status Offline
I tried, it's not that INPUT doesn't work, but that this batch program itself has serious bugs.

For example:
if FN=="aaa" goto a
if FN=="bbb" goto b

Should be changed to:

if "%FN%"=="aaa" goto a
if "%FN%"=="bbb" goto b
Wengier - 新DOS时代

欢迎大家来到我的“新DOS时代”网站,里面有各类DOS软件和资料,地址:
http://wendos.mycool.net/

E-Mail & MSN: wengierwu AT hotmail.com (最近比较忙,有事请联系DOSroot和雨露,谢谢!)

Floor 5 Posted 2006-05-01 11:00 ·  中国 江苏 南京 电信
初级用户
Credits 21
Posts 9
Joined 2006-04-09 22:43
20-year member
UID 53591
Status Offline
Floor 6 Posted 2006-05-01 17:28 ·  日本
银牌会员
★★★
Credits 1,246
Posts 488
Joined 2003-11-11 00:00
22-year member
UID 12699
Gender Male
Status Offline
One more thing:

if "%FN%"=="bbb" goto b
How to represent the inequality sign in Chinese?
Floor 7 Posted 2006-05-01 22:58 ·  中国 广东 云浮 新兴县 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
As far as I know, without using the inequality symbol notation like "!=" in C++ or "<>" in VB, we can only use:
if not "%FN%"== "bbb" goto b
Floor 8 Posted 2006-05-02 08:02 ·  中国 上海 东方有线
中级用户
★★
Credits 377
Posts 66
Joined 2005-04-04 00:00
21-year member
UID 37817
Gender Male
Status Offline
If you only use the 26 letters (like drive letters) as input, you can use the following cumbersome method:

Choice /C:defghijklmnopqrstuvwxyz /n Please Input disk symbol
if errorlevel 23 set input=z
if errorlevel 23 goto z
if errorlevel 22 set input=y
if errorlevel 22 goto y
if errorlevel 21 set input=x
if errorlevel 21 goto x
if errorlevel 20 set input=w
if errorlevel 20 goto w
if errorlevel 19 set input=v
if errorlevel 19 goto v
if errorlevel 18 set input=u
if errorlevel 18 goto u
if errorlevel 17 set input=t
if errorlevel 17 goto t
if errorlevel 16 set input=s
if errorlevel 16 goto s
if errorlevel 15 set input=r
if errorlevel 15 goto r
if errorlevel 14 set input=q
if errorlevel 14 goto q
if errorlevel 13 set input=p
if errorlevel 13 goto p
if errorlevel 12 set input=o
if errorlevel 12 goto o
if errorlevel 11 set input=n
if errorlevel 11 goto n
if errorlevel 10 set input=m
if errorlevel 10 goto m
if errorlevel 9 set input=l
if errorlevel 9 goto l
if errorlevel 8 set input=k
if errorlevel 8 goto k
if errorlevel 7 set input=j
if errorlevel 7 goto j
if errorlevel 6 set input=i
if errorlevel 6 goto i
if errorlevel 5 set input=h
if errorlevel 5 goto h
if errorlevel 4 set input=g
if errorlevel 4 goto g
if errorlevel 3 set input=f
if errorlevel 3 goto f
if errorlevel 2 set input=e
if errorlevel 2 goto e
if errorlevel 1 set input=d
if errorlevel 1 goto d
Floor 9 Posted 2006-05-02 08:16 ·  中国 上海 东方有线
中级用户
★★
Credits 377
Posts 66
Joined 2005-04-04 00:00
21-year member
UID 37817
Gender Male
Status Offline
Can set /p be used?

set /p input= Please input a "string"
if %input%=="aaa" goto a
if %input%=="bbb" goto b

[ Last edited by hxj on 2006-5-2 at 08:26 ]
Forum Jump: