中国DOS联盟论坛

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-08-07 07:53
48,040 topics / 350,125 posts / today 1 new / 48,252 members
DOS疑难解答 & 问题讨论 (解答室) » How to obtain the value returned after a command is executed
Printable Version  3,376 / 38
Floor1 kavenlee72 Posted 2008-01-02 12:55
中级用户 Posts 87 Credits 369
How to get the value returned after a command is executed?
For example, there is a file named a.txt with content:
1
Using the type command:
C:\>TYPE A.TXT
C:\>1
How to get the 1 displayed after the type command is executed in a batch script and add 1 to it, then save it into a.txt, that is, the content of a.txt becomes 2.
C:\>TYPE A.TXT
C:\>2

Environment requirements: DOS6.22 or DOS7.1

Thank you!
Floor2 everdos Posted 2008-01-03 01:27
初级用户 Posts 21 Credits 52
for /f %%a in (a.txt) do set /a pp=%%a+1
echo %pp% > a.txt
Floor3 lianjiang2004 Posted 2008-01-03 09:13
金牌会员 Posts 1,884 Credits 3,946
The person upstairs, can it be used under DOS? Hehe.
Floor4 fastslz Posted 2008-01-03 10:21
铂金会员 Posts 2,315 Credits 5,493 From 上海
test1.bat


test2.bat


Give an idea, the code is not tested
Floor5 fastslz Posted 2008-01-03 11:00
铂金会员 Posts 2,315 Credits 5,493 From 上海
The test code is okay. It's just that the execution result echo %1>A.TXT is actually echo 2>A.TXT, so we need to add the escape definition symbol echo ^%1>A.TXT
Floor6 fastslz Posted 2008-01-03 11:40
铂金会员 Posts 2,315 Credits 5,493 From 上海
Strengthen judgment of errorlevel return value

test1.bat


test2.bat
Floor7 kavenlee72 Posted 2008-01-04 10:26
中级用户 Posts 87 Credits 369
What went wrong during the test? There are two issues:
(1) A prompt "General error reading drive F" appears.
(2) After running test1.bat for the second time, the result is still 2 instead of becoming 3.
Please give guidance.

Here are the running results

E:\>TYPE A.TXT
1

E:\>TEST1

General error reading drive F
A: Abort, R: Retry, F: Fail? a
E:\>type a.txt
2

e:\>test1

General error reading drive F
A: Abort, R: Retry, F: Fail? a
E:\>type a.txt
2

I don't know how to attach pictures, so I have to type them out
Floor8 fastslz Posted 2008-01-04 10:48
铂金会员 Posts 2,315 Credits 5,493 From 上海
General errors occur when reading drive F, which has nothing to do with the code. The code part did not attempt to access drive F.

The pure DOS code is unstable and easily causes errors like "general error reading drive F". Please check if your batch code has parts that access drive F, or do not load the CD-ROM driver.

In addition, you can test the code on floor 6 under XP. The code is universal for pure DOS and XP. Just change echo ^%1>A.TXT to echo %1>A.TXT in pure DOS.

[ Last edited by fastslz on 2008-1-4 at 11:04 AM ]
Floor9 kavenlee72 Posted 2008-01-04 11:56
中级用户 Posts 87 Credits 369
I tested according to the code on floor 6, and didn't change it at all
Floor10 kavenlee72 Posted 2008-01-04 12:05
中级用户 Posts 87 Credits 369
(1) Tested under XP with no issues, can add, but can't add after reaching 9.
(2) Tested under DOS 7.1 and got "General error reading drive F" and won't add.
Floor11 fastslz Posted 2008-01-04 12:10
铂金会员 Posts 2,315 Credits 5,493 From 上海
I will guess and analyze what the reason might be.

F should be your CD - ROM drive letter. The DOS you booted into doesn't have the external command find.com, and the Path variable has %CDROM% or F:\..... At this time, DOS tries to read the directory under the CD - ROM to find find.com, so a general error occurs when reading drive F. That's how it is in pure DOS; no detail can be wrong.

Finally, emphasize again In pure DOS, change the code echo ^%1>A.TXT in building 6 to echo %1>A.TXT
Floor12 fastslz Posted 2008-01-04 12:23
铂金会员 Posts 2,315 Credits 5,493 From 上海
(1) Tested under XP with no problem, can be added, but cannot be added after 9.


Only 9, there is no way, it is the limitation of find.com, because 10 contains 1, if you use sed, there is no such limitation
Floor13 kavenlee72 Posted 2008-01-04 13:02
中级用户 Posts 87 Credits 369
Thanks! It's really that there is no find.exe command. After adding it, it's OK!

Is there really no way to break through the limit within 10 under dos7.1?
Floor14 kavenlee72 Posted 2008-01-04 13:11
中级用户 Posts 87 Credits 369
In the DOS usage introduction on the New DOS Era website, there is an introduction to count, a tool for addition calculations, but I don't know where to download it. I didn't find it in the forum.
Floor15 fastslz Posted 2008-01-04 13:22
铂金会员 Posts 2,315 Credits 5,493 From 上海
@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
:end

:xxx
shift
echo %1>A.TXT

:end

The pure DOS version of sed is available on my Google forum. Click my signature link.
Sed usage http://sed.sourceforge.net/sed1line_zh-CN.html
1 2 3  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023