Board logo

标题: 如何获得命令执行后返回的值 [打印本页]

作者: kavenlee72     时间: 2008-1-2 12:55    标题: 如何获得命令执行后返回的值
如何获得命令执行后返回的值?
比如,有个文件,名为a.txt,内容是:
1
用type命令:
C:\>TYPE A.TXT
C:\>1
在批处理中如何获得这个type命令执行后显示的1,并把它加1后保存入a.txt中,即a.txt的内容就变为2了
C:\>TYPE A.TXT
C:\>2

环境要求:DOS6.22或DOS7.1

谢谢!

作者: everdos     时间: 2008-1-3 01:27
for /f %%a in (a.txt) do set /a pp=%%a+1
echo %pp% > a.txt

作者: lianjiang2004     时间: 2008-1-3 09:13
楼上的,dos下能用吗?呵呵。

作者: fastslz     时间: 2008-1-3 10:21
test1.bat
call test2.bat 1 2 3 4 5 6 7 8 9


test2.bat
:start1
if "%1"=="" goto end
find "%1" A.TXT
if errorlevel 0 goto xxx
shift
goto start1

:xxx
shift
echo %1>A.TXT
:end


给个思路,代码未测试

作者: fastslz     时间: 2008-1-3 11:00
测试代码可以了,只是执行结果echo %1>A.TXT 其实是echo 2>A.TXT,所以要加转定义符号echo ^%1>A.TXT

作者: fastslz     时间: 2008-1-3 11:40
加强判断errorlevel返回值
test1.bat
@echo off
call test2.bat 1 2 3 4 5 6 7 8 9


test2.bat
:start1
if "%1"=="" goto end
find "%1" A.TXT>nul
if not errorlevel 1 if errorlevel 0 goto xxx
shift
goto start1
goto end

:xxx
shift
echo ^%1>A.TXT

:end

作者: kavenlee72     时间: 2008-1-4 10:26
测试时不知哪里出了问题?有两个问题:
(1)出现“一般性错误读驱动器F”的提示
(2)第二次运行test1.bat后的结果还是2,而没有变成3
请指教。

下面是运行的结果

E:\>TYPE A.TXT
1

E:\>TEST1

一般性错误读驱动器F
A:放弃,R:重试,F:失败?a
E:\>type a.txt
2

e:\>test1

一般性错误读驱动器F
A:放弃,R:重试,F:失败?a
E:\>type a.txt
2

不知如何贴图,只好敲上去了

作者: fastslz     时间: 2008-1-4 10:48
出现一般性错误读驱动器F和代码无关,代码部分没尝试要访问F盘
纯DOS代码不稳定很容易出现一般性错误读驱动器F这样的错误,请检查你的批处理代码是否有访问F盘部分,或者不要加载光驱驱动。
另外6楼的代码你可以在XP下测试,代码纯DOS和XP通用的,只是纯DOS要把echo ^%1>A.TXT改为echo %1>A.TXT

Last edited by fastslz on 2008-1-4 at 11:04 AM ]

作者: kavenlee72     时间: 2008-1-4 11:56
我是按6楼的代码进行测试的,一点都没变

作者: kavenlee72     时间: 2008-1-4 12:05
(1)在XP下测试没有问题,能够替加,但到9后就不能再加了。
(2)在DOS7.1下测试就出现“一般性错误读驱动器F”。而且不会替加

作者: fastslz     时间: 2008-1-4 12:10
我猜测分析下是什么原因吧
F应该是你的光盘盘符,你启动的DOS下没有find.com外部命令,而Path变量里有%CDROM%或F:\.....,这个时候DOS就尝试读取光盘下目录寻找find.com,所以出现一般性错误读驱动器F,纯DOS下就这样没个细节都不能错。
最后再强调下纯DOS要把6楼的代码echo ^%1>A.TXT改为echo %1>A.TXT

作者: fastslz     时间: 2008-1-4 12:23
(1)在XP下测试没有问题,能够替加,但到9后就不能再加了。


只能是9呢也没办法,是find.com限制,因为10里面包含1,如果用sed就没有这个限制了

作者: kavenlee72     时间: 2008-1-4 13:02
谢谢! 真的是没有find.exe命令。加上后就OK了!

在dos7.1下就没有办法突破10以内的限制吗?

作者: kavenlee72     时间: 2008-1-4 13:11
在新DOS时代网站的DOS使用中有介绍count,加法计算的工具,但不知哪里有下载?论坛里没找到

作者: fastslz     时间: 2008-1-4 13:22
@echo off
call test2.bat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
:start1
if "%1"=="" goto end
sed -n "/%1/p" A.TXT>nul
if not errorlevel 1 if errorlevel 0 goto xxx
shift
goto start1
goto end

:xxx
shift
echo %1>A.TXT

:end

sed纯DOS版我的google论坛上有,点我签名连接
sed用法http://sed.sourceforge.net/sed1line_zh-CN.html

作者: kavenlee72     时间: 2008-1-4 14:05
已从你的google论坛上下载了sed纯DOS版。并考贝到C盘,也按你的代码修改了test1.bat和test2.bat,但还是出现如下错误:
c:\>type a.txt
19

c:\>test1

一般性错误读驱动器F
A:放弃,R:重试,F:失败?a
c:\>

不知何解,请高手赐教。

作者: kavenlee72     时间: 2008-1-4 14:11
重新测试发现,在命令行输入sed /?时,出现:

一般性错误读驱动器F
A:放弃,R:重试,F:失败?a

测试环境:DOS7.1

作者: fastslz     时间: 2008-1-4 14:18
晕~你也太.....
C盘是Fat32吗?
C盘在Path里吗?
C盘在DOS下确实是C盘吗?
DOS下先找到sed,或者sed test1 test2放一起

作者: kavenlee72     时间: 2008-1-4 15:00
是在一起的,是用Microsoft Virtual PC做的DOS虚拟机。文件格式是Fat32,安装系统是DOS7.1
所有的文件都在同一个目录下。test1.bat、test2.bat、sed.exe、以及前面提到的find.exe等等都在一起。

谢谢!

作者: kavenlee72     时间: 2008-1-4 15:02
对不起,是都放在了C:盘的根目录下,包括A.TXT。从E:盘拷过去的。在E:盘下出出现同样问题。

作者: fastslz     时间: 2008-1-4 15:35
你那个path变量更改下否则麻烦不断
或者试试这样
@echo off
call test2.bat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

:start1
if "%1"=="" goto end
.\sed -n "/%1/p" A.TXT>nul
if not errorlevel 1 if errorlevel 0 goto xxx
shift
goto start1
goto end

:xxx
shift
echo %1>A.TXT

:end


Last edited by fastslz on 2008-1-4 at 03:36 PM ]

作者: kavenlee72     时间: 2008-1-4 16:42
c:\>type a.txt
2

一般性错误读驱动器F
A:放弃,R:重试,F:失败?f
Load error: no DPMI - Get csdpm*b.zip


在出现“一般性错误读驱动器F”提示时,如果输入"f",就出现"Load error: no DPMI - Get csdpmi*b.zip"

请教:什么是“DPMI”,上面的话是什么意思?如何解决?

作者: fastslz     时间: 2008-1-4 17:32
哦我也忽略了,DPMI 是DOS Protected Mode Interface ,DOS下提供保护模式支持
http://www.cn-dos.net/forum/viewthread.php?tid=17093&fpage=1&highlight=DPMI

作者: fastslz     时间: 2008-1-4 17:49
测试了一下这个不够稳定,下载站长推荐的
http://www.cn-dos.net/forum/viewthread.php?tid=18577&fpage=1

作者: kavenlee72     时间: 2008-1-4 18:20
谢谢指教,再次运行之:

C:\>DPMI.EXE
C:\>TYPE A.TXT
2

C:\>TEST1
C:\>TYPE A.TXT
2

运行dpmi.exe后,没有错误出现,但A.TXT里的内容却不会替加了,保持原来的值。还有,DPMI是不是每运行一次sed.exe前都要运行一次?

谢谢!

作者: fastslz     时间: 2008-1-4 19:16
我也试了,一样不稳定
这里可以下载到最新版的DPMI http://japheth.de/Download/HXRT.ZIP
sed.exe也可以考虑用最新版的vkill那有3个版本的sed http://www.vkill.net

作者: kavenlee72     时间: 2008-1-4 22:57
楼上的高手啊,问题还是没有解决啊!帮帮忙吧,谢谢啦!

没有找到Http://www.vkill.net里的sed.exe

作者: fastslz     时间: 2008-1-5 14:16
作者: kavenlee72     时间: 2008-1-5 16:55
问题没解决,不会替加!:(:(:(

请教fastslz老大,有办法解决么?

谢谢了!

Last edited by kavenlee72 on 2008-1-5 at 06:58 PM ]

作者: everdos     时间: 2008-1-5 20:21
用fc试试

test1.bat
--------------------------------------------------------------------------------------------
@echo off
call test2.bat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
type A.TXT
--------------------------------------------------------------------------------------------
test2.bat
--------------------------------------------------------------------------------------------
:start1
echo %1 > A2.TXT
fc A.TXT A2.TXT > nul
shift
if "%1"=="" goto end
if errorlevel 1 goto start1

echo %1 > A.TXT

:end

作者: logan0279     时间: 2008-1-5 23:32
@echo off
for /f %%b in (a.txt) do set c=%%b
set /a c+=1
echo %c% >a.txt

这个就可以执行一次加一次1了!

作者: kavenlee72     时间: 2008-1-6 00:03
楼上的,你这个好象不能在DOS6.22及DOS7.1下使用吧

作者: kavenlee72     时间: 2008-1-6 00:05
Originally posted by everdos at 2008-1-5 08:21 PM:
用fc试试

test1.bat
--------------------------------------------------------------------------------------------
@echo off
call test2.bat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
...



请教:fc是什么命令?哪里有得下?

作者: everdos     时间: 2008-1-6 09:58
fc是DOS标准命令,用于比较两个文件的内容,去DOS目录里拷贝过来就行了。
不知道楼主想做什么用,如果是想找个程序,每执行一次就将A.txt里面的内容加1,自己编个程序就简单多了。

作者: fastslz     时间: 2008-1-6 15:54
恩可以用fc来判断,只是多个临时文件,fc站长的dos7.1里有

作者: kavenlee72     时间: 2008-1-10 11:06
未找到FC,请大侠能给我发一个,邮箱:hopeless_2005@126.com

作者: fastslz     时间: 2008-1-10 11:53
估计这个tfind行的,我英文不好,自己看看吧
http://home.mnet-online.de/horst.muc/int/find23.zip

--------------------------------------------------------------------
TFIND String search Ver 2.3 (c) 2001, Horst Schaeffer
--------------------------------------------------------------------

TFIND searches for strings, like MS-DOS FIND, with the following
differences:

* Several strings may be specified. TFIND will search for lines
where all given strings are found (Boolean AND).

* The search can be limited to a field in a fixed field
(i.e. column oriented) list; see option /F

* An extended search mode is available, where only letters and
digits are relevant (option /X)

* Alternative errorlevel with number of lines reported (option /E)

* Case ignored by default, Option /C for case sensitive search

* Only one single file may be given
(use FOR loop to scan several files)

* TFIND expects a text file with lines not exceeding 2048 bytes.
The file size is not limited.

* MS-DOS FIND Options /C (count) and /N are not supported.


Syntax:
-----------
TFIND "string" input file > output file

The input file will also be read from STDIN, Examples:

TFIND "something" FOO.TXT > RESULT.TXT
TFIND "something" < FOO.TXT > RESULT.TXT
dir | TFIND " " /F1,1 /V > RESULT.TXT

The command arguments are separated by blank space or comma; the
options may be placed anywhere. Strings must be enclosed in double
or single quote marks.

If several strings are given, TFIND selects the lines where all
strings were found (Boolean AND).

For a multiple file scan, use a FOR loop, e.g.:

for %a in (*.TXT *.ASC) do TFIND "something" %a >> RESULT.TXT

Use double redirection symbols to collect the results, and make sure
to delete the output file at the beginning (unless you want to keep
previous results). For file name headers see option /H or /L.


Options:
------------
/Fc,l Search is limited to a field (starting column c, length l)
The string must be completely within the given field.
Default length: rest of line

/C Case sensitive search (case ignored by default)

/V Select (and output) lines where the string was NOT found.
Opposite of the default, which is of course to output all
lines with hits.

/E Set errorlevel to number of lines reported (0..254)
Errorlevel 254 is returned for 254 or more.
Syntax error returns 255.
Note: With option /V as well, the number of lines is
relevant, NOT how many times the string was found!

Default mode: errorlevel 0: string was found, 1: not found

/H Header line with file name. This option is recommended when
files are scanned with a FOR loop. A header is not produced
if there is no output.
Note that MS-DOS FIND writes this header line by default.
No header line generated when the file is read from STDIN.

By default a prefix with 10 dashes plus 1 blank space
is inserted (as with MS-DOS FIND).
/"prefix"
custom prefix string. Use /"" if you want no prefix.

/L Same as /H, however with LFN and full path (Win9x/2000 only)

/X Extended search (see below)


Extended search:
--------------------
With option /X anything but letters and digits is removed from the
lines as well as from the given search string,

i.e. "X-25" is the same as "X #25" or "X25"
"100 Kg" is the same as "100KG"
"03/12/2001" is the same as "03-12-2001" or "03122001"
"preselect" is the same as "pre-select"

There is only one exception: blank space is not removed from the
source line, when occurring between letters, i.e. words in normal
text remain separated. This means:

"newspaper" will NOT find "news paper"

For convenience (when the keywords are specified by user input,
for example), several search strings are not required as individual
strings in quote marks, but may be specified in one pair of quote
marks, and separated by blank space.

i.e. TFIND "foo any X-25" /X
works the same way as TFIND "foo" "any" "X-25" /X

Note that this is only possible with option /X.

Important:
The extended search may produce unwanted hits sometimes. This mode
is meant to preselect lines for further choice by the user.


Language support
--------------------
Upper case conversion is handled for the standard character set
(codepage 437). See CASE103.PAT for country specific modifications.

A special German version is available that handles "Umlauts" in
extended search ("? = "ae" etc.)


*** 15 JUL 2001

作者: everdos     时间: 2008-1-10 20:26    标题: fc.exe
上传了一个dos6.22的fc.exe到http://greenerycn.ys168.com/的公开下载区。
另外,传了一个Borland C++ 3.1的 grep.com上去,grep.com可按如下方式使用:

grep -w 1 A.txt
if errorlevel 1 goto notfound

(-w表示要全字匹配)

作者: kavenlee72     时间: 2008-1-11 14:44
谢谢!试一下