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-07 07:19
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » How to obtain the value returned after a command is executed View 3,368 Replies 38
Original Poster Posted 2008-01-02 12:55 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
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!
Floor 2 Posted 2008-01-03 01:27 ·  中国 上海 宝山区 电信
初级用户
Credits 52
Posts 21
Joined 2008-01-01 09:30
18-year member
UID 107370
Gender Male
Status Offline
for /f %%a in (a.txt) do set /a pp=%%a+1
echo %pp% > a.txt
Floor 3 Posted 2008-01-03 09:13 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
The person upstairs, can it be used under DOS? Hehe.
Windows 一键还原
http://www.yjhy.com
Floor 4 Posted 2008-01-03 10:21 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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


Give an idea, the code is not tested
第一高手 第二高手

Floor 5 Posted 2008-01-03 11:00 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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
第一高手 第二高手

Floor 6 Posted 2008-01-03 11:40 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
Strengthen judgment of errorlevel return value

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
第一高手 第二高手

Floor 7 Posted 2008-01-04 10:26 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
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
Floor 8 Posted 2008-01-04 10:48 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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 ]
第一高手 第二高手

Floor 9 Posted 2008-01-04 11:56 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
I tested according to the code on floor 6, and didn't change it at all
Floor 10 Posted 2008-01-04 12:05 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
(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.
Floor 11 Posted 2008-01-04 12:10 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
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
第一高手 第二高手

Floor 12 Posted 2008-01-04 12:23 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
(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
第一高手 第二高手

Floor 13 Posted 2008-01-04 13:02 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
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?
Floor 14 Posted 2008-01-04 13:11 ·  中国 广东 深圳 龙岗区 电信
中级用户
★★
Credits 369
Posts 87
Joined 2003-09-14 00:00
22-year member
UID 9822
Gender Male
Status Offline
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.
Floor 15 Posted 2008-01-04 13:22 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
@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
第一高手 第二高手

Forum Jump: