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-07-27 02:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please help optimize and modify this? View 972 Replies 1
Original Poster Posted 2010-04-01 22:23 ·  中国 广东 惠州 电信
初级用户
★★
Credits 126
Posts 53
Joined 2007-02-04 19:21
19-year member
UID 78631
Gender Male
From -
Status Offline
@echo off
mode con lines=20 cols=90

:sot
cls
echo.
set ID=
set /p ID= Please enter the 3-digit student code:
for /f "tokens=1*" %%i in ('findstr "%ID:~0,10%" mdb.txt') do (
cls
echo.
echo Student ID: %ID%
echo.
echo.
echo Student information: %%j
)
pause >nul 2>nul
goto sot

Contents of mdb.txt
00001 Zhang San Chinese Score: 100 points
00002 Zhang Si Chinese Score: 85 points
00003 Zhang Wu Chinese Score: 90 points

Please ask an expert to help modify this so the results are displayed on separate lines (for example: name on one line, subject on one line, score on one line)
Thanks
Floor 2 Posted 2010-04-02 13:07 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
'findstr "%ID:~0,10%" mdb.txt' is wrong when used like this. For example, when ID is 1, findstr cannot distinguish between 00001 and 00011 and things like 成绩:100分.
@echo off&setlocal enabledelayedexpansion&mode con lines=20 cols=90
:s
cls&echo.&set "a=This student code will"&set "b=cause an error&goto e"&set m=100&set n=0&set ID=
set/p ID= Please enter %a:~5,12%:
if "%ID%"=="" echo %a% is empty %b%
:n
if not "!ID:~%n%,1!"=="" set/a n+=1 &goto n
if %n% gtr 5 echo %a% length is out of range %b%
set i=%ID%
for /l %%i in (0 1 9) do if not "!i!"=="" set "i=!i:%%i=!"
if not "%i%"=="" echo %a% contains invalid characters %b%
for /l %%i in (5 -1 1) do if !ID:~0^,%%i! equ 0 set ID=!ID:~%%i!
if %ID% gtr %m% echo %a% numeric value is out of range %b%
set ID=0000%ID%&set ID=!ID:~-5!
::query
for /f "tokens=1-3,*" %%i in (mdb.txt) do if "%%i"=="%ID%" cls&echo.&echo Student ID: %ID%&echo.&echo.&echo. Name: %%j&echo. Subject: %%k&echo. %%l
:e
echo.&echo.&echo. Press any key to restart&pause>nul
goto s

This allows ID values like 1 or 01 or 001 or 0001 or 00001, all equivalent to 00001
The variable %m% is the maximum value of the student ID
It is recommended that the IDs in mdb.txt be consecutive numbers starting from 00001
If the entered ID exactly matches the one in mdb.txt, then the part from :n to ::query can be omitted

[ Last edited by Hanyeguxing on 2010-4-2 at 14:52 ]
Forum Jump: