|
hzy
高级用户
   
积分 578
发帖 135
注册 2003-7-10
状态 离线
|
『楼 主』:
[求助]批处理中 录入屏幕输入的字符串变量 语句如何
使用 LLM 解释/回答一下
我想写一个这样的批处理:
通过 屏幕输入“ 字符串”,然后将这个% 字符串%变量赋值到下面的命令,该怎么写?要用什么工具吗?
for %a in (c: d: e: f: g: h<img src="images/smilies/face-laugh.png" align="absmiddle" border="0"> do Dir /B /S %a\% 字符串%
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%
|
|
2003-7-23 00:00 |
|
|
Roy
管理员
          專業島民
积分 4869
发帖 1633
注册 2002-12-10
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
:: 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
:: 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正在播放的歌曲:
 |
|
2003-7-23 00:00 |
|
|
hzy
高级用户
   
积分 578
发帖 135
注册 2003-7-10
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢斑竹!可是我看了还是不太懂!
Thanks, moderator! But I still don't quite understand after reading it!
|
|
2003-7-23 00:00 |
|
|
GOTOmsdos
铂金会员
       C++启程者
积分 5154
发帖 1827
注册 2003-7-18
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
首先我觉得批处理的手段一般来说达不到问者的这个要求(否则要编一个复杂的脚本),能不能灵活处理一下,比如,要用"%wer%"变量就得预先设好(,还可当场在屏幕中输入"set wer=dfhgire.txt",这是不是满足了问者要在屏幕输入字符串的要求?),
要不然问者的要求是:在屏幕输入仅仅这个"变量名"(wer),这跟批处理的"%--%"变量的用法矛盾.
其实,还有一种变量可以符合问者的基本意图: %1-%9
写成批处理: for %a in (c: d: e: f: g: h<img src="images/smilies/face-laugh.png" align="absmiddle" border="0"> do dir /b /s %a\%1
然后在屏幕输入dfhgire.txt
这样是不是基本满足问者的要求?
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?
|
|
2003-8-18 00:00 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Re hzy:
正如 GOTOmsdos 所说,单纯使用基本dos命令所组成的批处理,要达到问者的这个要求,需要编写相当复杂的脚本。Roy给出的范例就可见一般。通常这种情况下,我们所要做的是转化矛盾,灵活处理,而非一味蛮干。事实上,Roy所给出的例子除了能让人惊讶、感叹和迷糊之外,没有什么其他的价值。虽然我自己也很喜欢让别人惊讶、感叹和迷糊 :)
再提供一个灵活的思路吧,通常在批处理中需要获取一个字符传,最简单也最常用的方法是使用命令行参数。我们可以在用户未输入参数串显示帮助文本,给出正确的参数用法;在输入错误的参数串时给出错误提示,这样做起来并不难。
或者,如果预见到字符串的可选域不是很大,那么可以“单项选择”:
echo 1. *.txt 2. *.tmp 3. *.exe
choice /c:123 /n
或者,你就想在批处理运行半截子之后,突然想到获取一个字符串,并且存在环境变量中,那么你需要一个工具:askenv.exe。“新dos时代”的脚本工具就有下载。
另外,纠正一个小bug:“通过屏幕输入“字符串””
屏幕是输出设备,不能“输入”字符串,输入字符串通常是键盘的任务。
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2003-9-6 00:00 |
|
|
iceboy
银牌会员
    
积分 1681
发帖 512
注册 2003-8-2
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
从上面的批处理程序,我们可以学到不少东西哦...
如,不确定 %TEMP% 是 C:\ 还是 C:\TEMP,就用 %TEMP%.\
如果是 C:\,就成了 C:\.\
如果是 C:\TEMP,就成了 C:\TEMP.\
两者都没错哦!
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. |
|
2003-9-6 00:00 |
|
|
jhhjwok
中级用户
  
积分 278
发帖 52
注册 2003-6-10
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
用INPUT小程序可以操作!
It can be operated with the INPUT applet!
|
|
2003-10-16 00:00 |
|