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-06-25 04:54
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch processing: Address book View 4,252 Replies 20
Original Poster Posted 2006-11-16 00:14 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
After looking at zouzhxi's DOS version address book, I have some ideas:

Latest modification on floor 20:
Modified from 11-20: Adopt module method to reduce scripts with the same function, add the function of judging whether the contact exists, and modify the single item ending and the retrieval method for viewing all contacts


The search display of version by namejm (modified the single item ending retrieval method):

@echo off
:: The format of the thesaurus is: the word occupies one line (case-insensitive);
:: The translated content starts a new line (no pure letter lines are allowed), and there can be multiple lines.
:begin
cls
set input=
set line=
set /p input=Please enter the word to be searched (press Enter directly to exit):
if not defined input exit
for /f "tokens=1* delims=:" %%i in ('findstr /nirc:"^%input%" Tel.db') do (if /i "%%j"=="%input%" set line=%%i)
if not "%line%"=="" (goto display) else (
echo _________________________________
echo.
echo No record found for %input%
echo _________________________________
echo.
pause
goto begin)

:display
echo _________________________________
echo.
echo %input%:
for /f "skip=%line% delims=" %%i in (Tel.db) do (
if "%%i"=="::" goto end
echo %%i
)
:end
echo _________________________________
echo.
pause
goto begin


Batch processing to delete a certain record

@echo off
echo.
set /p DeletePerson= Delete person:
>TelN.db echo ::Address book database
>>TelN.db echo --------------------------------------------------
>>TelN.db echo.
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (Tel.db) do (
if "%%i" == "::" set /a Num+=1
)

set Delete=echo
for /f "skip=3 delims=" %%i in (Tel.db) do (
if "%%i" == "%DeletePerson%" set Delete=rem&&set /a N-=1
!Delete! %%i>>TelN.db
if "%%i" == "::" !Delete!.>>TelN.db&&set /a N+=1&&cls&&echo.&&echo !N!/%Num%&&set Delete=echo
)
pause
del Tel.db
ren TelN.db Tel.db
exit


Tel.db Address book database

::Address book database
--------------------------------------------------

Zhang San
Contact name: Zhang San
Contact phone: Zhang San
Contact address: Zhang San
Contact unit: Zhang San
::

Li Si
Contact name: Li Si
Contact phone: Li Si
Contact address: Li Si
Contact unit: Li Si
::

Wang Wu
Contact name: Wang Wu
Contact phone: Wang Wu
Contact address: Wang Wu
Contact unit: Wang Wu
::



Add single item

set ContactName=
set ContactPhone=
set ContactUnit=
set ContactAddress=
set...
set /p ContactName=Contact name:
set /p ContactPhone=Contact phone:
set /p ContactAddress=Contact unit:
set /p ContactUnit=Contact address:
set /p...

>>Tel.db echo %ContactName%
>>Tel.db echo Contact name: %ContactName%
>>Tel.db echo Contact phone: %ContactPhone%
if not "%ContactUnit%"=="" >>Tel.db echo Contact unit: %ContactUnit%
if not "%ContactAddress%"=="" >>Tel.db echo Contact address: %ContactAddress%
if...
>>Tel.db echo ::
>>Tel.db echo.


[ Last edited by zxcv on 2006-11-21 at 03:20 AM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
redtek +5 2006-11-16 00:48
ccwan +2 2006-11-16 01:39
Floor 2 Posted 2006-11-16 01:39 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
set Contact Name
set Contact Phone
set Contact Unit
set Contact Address

It seems that the environment variables are not defined, it should be
set Contact Name=
set Contact Phone=
set Contact Unit=
set Contact Address=

But it's done very well, worthy of bonus points.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2006-11-16 01:45 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
It is suggested to add a command for browsing all. Because I prefer to display all first and then search from them.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2006-11-16 01:56 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by ccwan at 2006-11-15 13:39:
set Contact Name
set Contact Phone
set Contact Unit
set Contact Address

The environment variable is not defined, it seems it should be
set Contact Name=
set Contact Phone=
set Contact Unit= ...

Missed it ^_^, thank you for pointing out

Originally posted by ccwan at 2006-11-15 13:45:
It is suggested to add a command to browse all, because I prefer to display all and then search from them.

It's simple, add an option to jump and execute "notepad Tel.db" to directly browse the library file ^_^
Floor 5 Posted 2006-11-16 03:12 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Hehe, add a start, okay? I don't like the black window to be open all the time.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2006-11-16 03:28 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
I think this is not as fast as just using Notepad
Floor 7 Posted 2006-11-16 03:40 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
re:vkill

Actually, this is just to provide an idea, letting everyone understand the usage of some commands. It's not necessary to use this address book.

There are many software that only complete a few simple commands but are made very complicated. ^_^

Brother vkill, don't take it to heart.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +1 2006-11-16 03:42
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 8 Posted 2006-11-16 03:52 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Basic application, no need to create "Tel.db" first

Address Book.bat
@echo off
title Address Book
mode con cols=80 lines=25
set DisPlay=
:Main
cls
echo.
echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
echo ※ ※
echo ※ ☆Welcome to the Command Prompt Version Address Book☆ ※
echo ※ ※
echo ※ zxcv@bbs.cn-dos.net zh59@bbs.blueshow.net ※
echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
echo.
echo ※ Add New Contact (N) ※ Delete Contact (D) ※ Modify Contact (M) ※ View Tel.db (V) ※
echo.
set input=
set line=
set /p input= Choose (directly enter contact to query)%DisPlay%:
if not exist Tel.db call :Tel.db
if "%input%"=="" set DisPlay=&goto Main
for %%n in (n N) do if "%input%"=="%%n" goto New
for %%n in (d D) do if "%input%"=="%%n" goto Del
for %%n in (m M) do if "%input%"=="%%n" goto Modify
for %%n in (v V) do if "%input%"=="%%n" goto db

for /f "tokens=1* delims=:" %%i in ('findstr /nirc:"^%input%" Tel.db') do (if /i "%%j"=="%input%" set line=%%i)
if not "%line%"=="" (goto display) else (
echo ________________________________________
echo.
echo No record found for %input%
echo ________________________________________
echo.
pause
set DisPlay=
goto Main)

:display
echo ________________________________________
echo.
for /f "skip=%line% delims=" %%i in (Tel.db) do (
if "%%i"=="::" goto end
echo %%i
)
:end
echo ________________________________________
echo.
pause
set DisPlay=
goto Main

:Tel.db
>Tel.db echo ::Address Book Database
>>Tel.db echo --------------------------------------------------
>>Tel.db echo.
goto :eof

:New
echo.
set Contact Name=
set Contact Phone=
set Contact Unit=
set Contact Address=
set /p Contact Name= Contact Name:
if "%Contact Name%"=="" goto Main
set /p Contact Phone= Contact Phone:
set /p Contact Address= Contact Unit:
set /p Contact Unit= Contact Address:

>>Tel.db echo %Contact Name%
>>Tel.db echo Contact Name: %Contact Name%
>>Tel.db echo Contact Phone: %Contact Phone%
if not "%Contact Unit%"=="" >>Tel.db echo Contact Unit: %Contact Unit%
if not "%Contact Address%"=="" >>Tel.db echo Contact Address: %Contact Address%
>>Tel.db echo ::
>>Tel.db echo.
set DisPlay=(Added new contact: %Contact Name%)
goto Main

:Del
echo.
set N=
set Num=
set /p Delete Contact= Delete Contact:
if "%Delete Contact%"=="" set DisPlay=&goto Main

>TelN.db echo ::Address Book Database
>>TelN.db echo --------------------------------------------------
>>TelN.db echo.
set Delete=echo
setlocal EnableDelayedExpansion

for /f "skip=3 delims=" %%i in (Tel.db) do (
if "%%i" == "%Delete Contact%" set Delete=rem
!Delete! %%i>>TelN.db
if "%%i" == "::" !Delete!.>>TelN.db&&set Delete=echo
)
echo.
move/y Tel.db TelBak.db
ren TelN.db Tel.db
set DisPlay=(Contact %Delete Contact% has been deleted)
goto Main

:Modify
echo.
set Modify Contact Name=
set /p Modify Contact Name= Modify Contact Name:
if "%Modify Contact Name%"=="" set DisPlay=&goto Main
echo.
echo Modify:
echo.
set Contact Name=
set Contact Phone=
set Contact Unit=
set Contact Address=
set /p Contact Name= Contact Name:
if "%Contact Name%"=="" goto Main
set /p Contact Phone= Contact Phone:
set /p Contact Address= Contact Unit:
set /p Contact Unit= Contact Address:

set Delete Contact=%Modify Contact Name%
>TelN.db echo ::Address Book Database
>>TelN.db echo --------------------------------------------------
>>TelN.db echo.
set Delete=echo
setlocal EnableDelayedExpansion

for /f "skip=3 delims=" %%i in (Tel.db) do (
if "%%i" == "%Delete Contact%" set Delete=rem
!Delete! %%i>>TelN.db
if "%%i" == "::" !Delete!.>>TelN.db&&set Delete=echo
)
echo.
move/y Tel.db TelBak.db
ren TelN.db Tel.db

>>Tel.db echo %Contact Name%
>>Tel.db echo Contact Name: %Contact Name%
>>Tel.db echo Contact Phone: %Contact Phone%
if not "%Contact Unit%"=="" >>Tel.db echo Contact Unit: %Contact Unit%
if not "%Contact Address%"=="" >>Tel.db echo Contact Address: %Contact Address%
>>Tel.db echo ::
>>Tel.db echo.
set DisPlay=(Contact %Modify Contact Name% has been modified)
goto Main

:db
start notepad Tel.db
goto Main


Actually, modifying a contact is a combined process of deleting a contact and adding a new contact

[ Last edited by zxcv on 2006-11-15 at 03:55 PM ]
Attachments
IE 通讯录.gif
新增联系人.gif
联系人资料.gif
Floor 9 Posted 2006-11-16 05:31 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
Reply to the upstairs:
You have code but it seems there is no two-way query function,

Knowing the name but not the number, this query function already exists.
But only knowing the number but not the name, this query function is not available.
Because when we write the address book, it's not just 10 or 100, there are more, so we can't remember (in a vague state).



[ Last edited by zouzhxi on 2006-11-16 at 05:32 AM ]
Floor 10 Posted 2006-11-16 05:35 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Come on! Is it necessary to be overly critical?
I said it's just an idea. You can use it as a reference and think of similar cases by yourself!
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 11 Posted 2006-11-16 05:40 ·  IANA 局域网IP(Private-Use)
中级用户
★★
蝴蝶之吻
Credits 430
Posts 177
Joined 2006-09-20 12:00
19-year member
UID 63170
From 广东深圳
Status Offline
I just said, can the function of two-way fuzzy query be realized. There is no meaning of blame...
Floor 12 Posted 2006-11-16 06:14 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by zouzhxi at 2006-11-16 05:40:
I just said, can the two-way fuzzy query function be realized. There is no meaning of blame...

This is completely possible. However, the format of the "database" determines the simplicity of your code. If sed code is used, it is easier to write, similar to the bus route query. ~
http://www.cn-dos.net/forum/viewthread.php?tid=24668&fpage=1 The 11th floor of the post is the sed version
Floor 13 Posted 2006-11-16 10:27 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Modify: Two-way query of contacts or phone numbers, view all contacts and phone numbers

Address Book.bat

@echo off
title Address Book
mode con cols=80 lines=25
set DisPlay=
setlocal EnableDelayedExpansion
:Main
cls
echo.
echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
echo ※                                    ※
echo ※           ☆Welcome to the Command Prompt Version Address Book☆           ※
echo ※                                    ※
echo ※       zxcv@bbs.cn-dos.net  zh59@bbs.blueshow.net       ※
echo ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
echo.
echo   ※ Add (N) ※ Delete (D) ※ Modify (M) ※ View All (A) ※ View Tel.db (V) ※
echo.
set input=
set line=
set /p input= Select (directly enter contact or phone number to query)%DisPlay%:
if not exist Tel.db call :Tel.db
if "%input%" == "" set DisPlay=&goto Main
if /i "%input%" == "n" goto New
if /i "%input%" == "d" goto Del
if /i "%input%" == "m" goto Modify
if /i "%input%" == "a" goto All
if /i "%input%" == "v" goto db

for /f "tokens=1,2,3* delims=:" %%i in ('findstr /nirc:"%input%" Tel.db') do (
if "%%j" == "%input%" set line=%%i
if "%%k" == "%input%" set line=%%i)
if not "%line%" == "" (goto display) else (
echo ________________________________________
echo.
echo No record found for %input%
echo ________________________________________
echo.
pause
set DisPlay=
goto Main)

:display
echo ________________________________________
echo.
for /f "skip=%line% delims=" %%i in (Tel.db) do (
if "%%i" == "::" goto end
echo %%i
)
:end
echo ________________________________________
echo.
pause
set DisPlay=
goto Main

:Tel.db
>Tel.db echo ::Address Book Database
>>Tel.db echo --------------------------------------------------
>>Tel.db echo.
goto :eof

:New
echo.
set ContactName=
set ContactPhone=
set ContactUnit=
set ContactAddress=
set /p ContactName= Contact Name:
if "%ContactName%" == "" goto Main
set /p ContactPhone= Contact Phone:
set /p ContactAddress= Contact Unit:
set /p ContactUnit= Contact Address:

>>Tel.db echo %ContactName%:%ContactPhone%:::
>>Tel.db echo Contact Name: %ContactName%
>>Tel.db echo Contact Phone: %ContactPhone%
if not "%ContactUnit%" == "" >>Tel.db echo Contact Unit: %ContactUnit%
if not "%ContactAddress%" == "" >>Tel.db echo Contact Address: %ContactAddress%
>>Tel.db echo ::
>>Tel.db echo.
set DisPlay=(Added new contact: %ContactName%)
goto Main

:Del
echo.
set N=
set Num=
set /p DeleteContact= Delete contact:
if "%DeleteContact%" == "" set DisPlay=&goto Main

>TelN.db echo ::Address Book Database
>>TelN.db echo --------------------------------------------------
>>TelN.db echo.
set Delete=echo

for /f "skip=3 delims=" %%i in (Tel.db) do (
call :DelName %%i
if "!Database!" == "%DeleteContact%" set Delete=rem
!Delete! %%i>>TelN.db
if "%%i" == "::" !Delete!.>>TelN.db&&set Delete=echo
)
echo.
move/y Tel.db TelBak.db
ren TelN.db Tel.db
set DisPlay=(Contact %DeleteContact% has been deleted)
goto Main

:Modify
echo.
set ModifyContactName=
set /p ModifyContactName= Modify contact name:
if "%ModifyContactName%" == "" set DisPlay=&goto Main
echo.
echo Modify:
echo.
set ContactName=
set ContactPhone=
set ContactUnit=
set ContactAddress=
set /p ContactName= Contact Name:
if "%ContactName%" == "" goto Main
set /p ContactPhone= Contact Phone:
set /p ContactAddress= Contact Unit:
set /p ContactUnit= Contact Address:

set DeleteContact=%ModifyContactName%
>TelN.db echo ::Address Book Database
>>TelN.db echo --------------------------------------------------
>>TelN.db echo.
set Delete=echo
setlocal EnableDelayedExpansion

for /f "skip=3 delims=" %%i in (Tel.db) do (
call :DelName %%i
if "!Database!" == "%DeleteContact%" set Delete=rem
!Delete! %%i>>TelN.db
if "%%i" == "::" !Delete!.>>TelN.db&&set Delete=echo
)
echo.
move/y Tel.db TelBak.db
ren TelN.db Tel.db

>>Tel.db echo %ContactName%:%ContactPhone%:::
>>Tel.db echo Contact Name: %ContactName%
>>Tel.db echo Contact Phone: %ContactPhone%
if not "%ContactUnit%" == "" >>Tel.db echo Contact Unit: %ContactUnit%
if not "%ContactAddress%" == "" >>Tel.db echo Contact Address: %ContactAddress%
>>Tel.db echo ::
>>Tel.db echo.
set DisPlay=(Contact %ModifyContactName% has been modified)
goto Main

:All
cls
echo.
echo All contacts in the address book:
echo.
set N=
for /f "skip=3 delims=" %%i in (Tel.db) do (
set All=%%i
if "!All:~-3!" == ":::" set /a N+=1&set AllShow=!All::::=!&&echo !AllShow::=: !
set /a P=!N!%%19
if "!P!" == "0" if "!All:~-3!" == ":::" call :AllPaus)
echo.
pause
goto Main

:db
start notepad Tel.db
goto Main

:DelName
for /f "tokens=1* delims=:" %%n in ("%1") do set Database=%%n
goto :eof

:AllPaus
echo.
set Paus=
set /p Paus= Press Enter to continue (B to return to main interface)...
if /i "%Paus%" == "b" goto Main
cls
echo.
echo All contacts in the address book:
echo.
goto :eof


[ Last edited by zxcv on 2006-11-15 at 11:14 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +5 2006-11-18 00:02
Attachments
修改联系人资料.gif
所有联系人资料.gif
Floor 14 Posted 2006-11-16 11:05 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
For a `for` statement like
```code
for %%n in (n N) do if "%input%" == "%%n" goto New
```
it can be replaced with
```code
if /i "%input%"=="n"
```
to simplify the code.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 15 Posted 2006-11-16 11:08 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
if /i "%input%"=="n"
Forgot this

Thanks to moderator namejm for the reminder
Forum Jump: