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-02 03:36
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion]Data Query and Database Operation Scripts View 1,460 Replies 10
Original Poster Posted 2006-08-15 23:09 ·  中国 山西 运城 移动
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
Today I went to a company. During the waiting process, suddenly a phone call asked the clerk for someone's phone number. I saw that clerk drag the scroll bar with the mouse to find that name one by one and then report the phone number!

Suddenly I felt it was very funny, and I thought if there was a batch script that could solve this problem?

On the bus on the way back, I kept thinking about this problem... But after I solved this problem, I felt that the根本 existence meaning of this batch script was gone, because whether it was Word or Excel or even Notepad, there were all functions to find strings... Hey...

But since I wrote it, I'll post the code... for everyone to have a laugh... Hehe...

@echo off
set /p uu=Please enter the name or phone number you want to find:
type config.cfg |find /i "%uu%"
echo.
echo.
echo.
pause
exit

Description: config.cfg is the configuration file of CS. I estimate everyone knows this, and those three echo. are to have a blank space after finding and displaying, so that the display of pause does not affect the normal display output, making it easier to view!

[ Last edited by willsort on 2006-8-22 at 03:21 ]
房东说:这娃是个好孩子!
Floor 2 Posted 2006-08-16 07:28 ·  中国 福建 厦门 思明区 电信
初级用户
Credits 147
Posts 23
Joined 2005-01-23 00:00
21-year member
UID 35557
Gender Male
Status Offline
You mentioned CS, does it refer to Counter-Strike? Then what's the relation to the phone?
Floor 3 Posted 2006-08-16 13:11 ·  中国 上海 徐汇区 电信
中级用户
★★
论坛第一菜鸟
Credits 361
Posts 166
Joined 2006-04-15 13:23
20-year member
UID 53870
Status Offline
Is `cs` a register?


So that the display of `pause` does not affect the normal display output, making it easier to watch!

Isn't it enough to use `pause > nul`?


But since it's written out, just post the code... for everyone to have a laugh... heh heh...

It doesn't matter. The most important thing about batch commands is not being proficient, but innovation.
Diskette Operating System
Floor 4 Posted 2006-08-16 22:19 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
21-year member
UID 40733
Gender Male
Status Offline
I think the purpose of writing a script is to conveniently solve some practical problems. As long as script writing is needed, it must meet specific requirements. The other consideration is just to see if it is practical.
There was a period when I needed to frequently check the postal codes and area codes of some places, so I made the following script. In order to format the output and enhance the fault tolerance of input, gawk.exe was used. Since it was not made very carefully, I was not very satisfied and did not show it off.
The usage method is: for example, to check the postal code of my hometown Tai'an, it can be like this
1、YBQH 泰安
2、YBQH tai an
3、YBQH taian
4、YBQH ta
5、YBQH 0538
It can also run with empty parameters and input according to the prompt. Among them, returning many non-desired results according to the initials of pinyin. Other files are in the attachment. Since I can't upload the attachment today, there seems to be a problem with the upload system. Let me know if you need it.
YBQH.bat


  1. ::Simple postal code and area code query program
  2. ::BY 无奈何@cn-dos.net
  3. @echo off
  4. title Postal code and area code query
  5. echo.
  6. gawk -f find.awk %*
  7. goto next

  8. :loop
  9. echo.
  10. gawk -f find.awk
  11. :next
  12. echo.
  13. set j=
  14. set /p j= Press "0" key or space bar to continue; any other key to exit....
  15. if "%j%" == " " goto loop
  16. if "%j%" == "0" (goto loop) else exit
  17. goto :end
  18. :end
Written by 无奈何 on 2006-08-16 21:51


find.awk

BEGIN {
IGNORECASE = 1
FS="\t"
if (ARGV != ""){
for (i = 1; i <= ARGC; i++) {
imput=imput " " ARGV
}
} else {
printf "Please enter the keyword to be queried: "
getline imput < "-"
}
gsub(/^ +| +$/,"",imput)
str=toupper(imput)
gsub(/ +/,"",str)
if (str == "") {
print "\n\t\tThe keyword should not be left blank!"
exit
}
printf "\n\n%-10s%-16s%10s%10s\n","Province and city","Area","Area code","Postal code"
printf "================================================\n"
while((getline < "YBQH.txt") > 0){
if (($0~str)||(gensub(/ +/,"","g",$3)~str)) {
printf "%-10s%-16s%10s%10s\n",$1,$2,$5,$6
flag=1
}
}
print "\n"
if (flag==0)
print "\t\tNo record with this keyword!\n"
}

  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 5 Posted 2006-08-16 22:43 ·  中国 安徽 安庆 电信
初级用户
Credits 36
Posts 14
Joined 2006-04-29 14:56
20-year member
UID 54667
Status Offline
Wow, you've even come up with such a batch script. I'm impressed.
Floor 6 Posted 2006-08-16 22:52 ·  中国 山西 运城 移动
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
CS is the configuration file for Counter-Strike, but here I'm using it as a substitute for a phone book!
I have a bad habit, which is always posting the actual files instead of the files I actually envisioned.
After reading the code of the helpless version master, I think my code still needs to be improved a bit.
In fact, it doesn't matter whether the code is useful or not. The most important thing is that we need to think about whether we can make the computer make life simpler...
房东说:这娃是个好孩子!
Floor 7 Posted 2006-08-17 20:44 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
But after I solved this problem, I felt that this batch processing doesn't have any meaning at all, because both Word and Excel, and even Notepad, have the function of finding strings... Hey...

You don't need to be too self-deprecating. Not to mention whether it's useful or not, every time you write a script, your ability to write scripts will improve. Simply the flexibility and diversity of scripts are incomparable to popularized programs. Of course, without popularized applications, the functions of scripts will also be greatly affected. In fact, my main point is: Don't put scripts and applications on opposite sides, but should consider how to make scripts combine with applications to play their roles more greatly.
For example, the following is something pieced together from a few of my previous scripts:
BAT file part:

@echo off
:start
setlocal
cls
echo 1. Add record
echo 2. Query record
echo 3. Save record as csv format
echo 4. Save as database
echo 5. View data from web page
echo Any other key to exit...
set /p var=Choose:
set var=%var:~0,1%
call :%var% %0 2>nul || goto :eof
endlocal
goto start

:1
setlocal
cls
echo Enter record:
set /p str=
echo %str% >>%1
if exist 电话号码数据库.mdb wscript.exe 数据库脚本.vbs /a %str%
endlocal
goto :eof

:2
setlocal
cls
echo Enter query condition:
set /p str=
cls
more +52 %1 | findstr %str% || echo Record not found!
echo.&echo Press any key to return to main menu! & pause>nul
endlocal & dir >nul
goto :eof

:3
setlocal >记录.csv
for /f "skip=52 tokens=1,2" %%i in (%~fsnx1) do echo %%i,%%j >>记录.csv
endlocal
goto :eof

:4
wscript.exe 数据库脚本.vbs /c %1 && echo Operation completed!
pause
goto :eof

:5
wscript.exe 数据库脚本.vbs
goto :eof


Note: There is a carriage return at the end of the code, and the file name can be arbitrary.

VBS file part:


Set objArgs = WScript.Arguments
strDataName = "电话号码数据库.mdb"
strTableName = "MyTelTable"

On Error Resume Next
If WScript.Arguments.Count <> 0 Then
Select Case objArgs(0)
Case "/c" bat2mdb
Case "/a" If Not AddRecord(objArgs(1),objArgs(2)) Then bat2mdb
Case Else PrintData
End Select
Else
PrintData
End If


'****************************************************************************
'bat2mdb
'****************************************************************************
Sub bat2mdb()

CreateDataBase
With CreateObject("Scripting.FileSystemObject").OpenTextFile(objArgs(1))
For i = 1 to 52
.skipline
Next
Do While .AtEndOfStream <> True
On Error Resume Next
strArray = Split(.ReadLine)
AddRecord strArray(0),strArray(1)
Loop

End With

End sub

'****************************************************************************
'Create database file
'****************************************************************************
Sub CreateDataBase()

On Error Resume Next
CreateObject("Adox.Catalog").Create ( _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source =" & strDataName )

'Create a new table
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open _
"Provider= Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & strDataName
objConnection.Execute( "CREATE TABLE " & strTableName & _
"(Name TEXT(10) , Tel TEXT(20) )" )
End Sub

'****************************************************************************
'Add data
'****************************************************************************
Function AddRecord(strName,strTel)
On Error Resume Next
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

AddRecord = objConnection.Open( _
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDataName )
objRecordSet.Open _
"SELECT * FROM " & strTablename, objConnection, 3, 3

objRecordSet.AddNew
objRecordSet("Name") = strName & space(1)
objRecordSet("Tel")= strTel & space(1)
objRecordSet.Update

End Function

'****************************************************************************
'Get HTML code
'****************************************************************************
Function GetStrHtml()

strHTML = "<table border='1' style='border-collapse: collapse' " & _
"bordercolor='#111111' width='100%' id='Table1' >"
strHTML = strHTML & "<tr><td>Name</td><td>Tel</td></tr>"

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open _
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDataName
objRecordSet.Open _
"SELECT * FROM " & strTablename, objConnection, 3, 3

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
strHTML = strHTML & "<tr>"
for i = 1 to objrecordset.fields.count
strHTML = strHTML & "<td>" & objrecordset.fields(i - 1) & "</td>"
next
strHTML = strHTML & "</tr>"
objRecordSet.MoveNext
Loop
GetStrHtml = strHTML & "</table>"

End Function

'****************************************************************************
'Display data
'****************************************************************************
Sub PrintData()

Set objIE = Wscript.CreateObject("InternetExplorer.Application")
With objIE
.menubar = 0
.addressbar = 0
.toolbar = 0
.statusbar = 0
.resizable = 0
.width = 400
.height = 500
.navigate "about:blank"
.left = fix((.document.parentwindow.screen.availwidth-.width)/2)
.top = fix((.document.parentwindow.screen.availheight-.height)/2)
.visible = 1
end With


With objIE.Document
.Write "<HTML><HEAD><TITLE>Phone Database</TITLE></HEAD>"
.Write "<BODY STYLE='font:14pt arial; color:white;"
.Write "filter:progid:DXImageTransform.Microsoft.Gradient"
.Write "(GradientType=0, StartColorStr=#800000, EndColorStr=#DB7093)'>"
.Write "<DIV id='WriteStr'></DIV>"
.Write "<HR COLOR=#DB7093></FONT></CENTER></BODY></HTML>"
end With
Set objDiv = objIE.Document.All.WriteStr
objDiv.innerHTML = GetStrHtml()

End Sub


Note: The saved file name must be: 数据库脚本.vbs
Originally planned to echo the vbs file in the bat, but because the code is too long, it is separated into two files. Since the code is pieced together from a few of my previous scripts, there are inevitable deficiencies, and everyone can modify it by themselves for fun.
Attachments
脚本.rar (2.05 KiB, Credits to download 1 pts, Downloads: 37)
Floor 8 Posted 2006-08-18 18:39 ·  中国 江苏 无锡 电信
初级用户
Credits 44
Posts 16
Joined 2006-08-15 07:15
19-year member
UID 60550
Status Offline
I've learned a lot again.. I need to come up more often in the future///
Floor 9 Posted 2006-08-20 11:17 ·  中国 山西 运城 移动
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
### 1. What does `more +52 %1` mean?
`more` is a command in DOS (Disk Operating System) used to view the content of a file. The `+52` option means to start displaying the file starting from the 52nd line. `%1` is a batch file parameter, which represents the first command-line argument passed to the batch script. So `more +52 %1` means to use the `more` command to display the file passed as the first argument starting from the 52nd line.

### 2. Why use two pipe symbols for `|| echo 记录不存在!`?
In batch scripting, the `||` operator is a logical OR operator. It means that if the preceding command (in this case, `findstr %str%`) returns a non-zero exit code (indicating that the search did not find a match), then the command following `||` (here, `echo 记录不存在!`) will be executed. So `findstr %str% || echo 记录不存在!` means: if `findstr %str%` does not find the string `%str%`, then output "记录不存在!" (which means "Record does not exist!"). The two pipe symbols are used to connect the `findstr %str%` command and the `echo 记录不存在!` command in the logical OR structure.
房东说:这娃是个好孩子!
Floor 10 Posted 2006-08-20 13:32 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by maya0su at 2006-8-20 11:17:
Please ask the following
more +52 %1 | findstr %str% || echo Record not found!
1. What does more +52 %1 mean?
2. Why use two pipe symbols for || echo Record not found!?
Please explain...


  1. Display the content of the file starting from line 52;

  2. || is not the use of two consecutive pipe symbols, but it means that if the previous statement fails to execute, then the subsequent statement is executed; similarly, && means that the subsequent statement is executed only after the previous statement is executed successfully;

[ Last edited by namejm on 2007-2-5 at 10:41 AM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-09-04 08:36 ·  中国 山西 运城 移动
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
To add a "delete" option to this batch script, you can modify it as follows:

```batch
@echo off
title 电话本查找工具
color 0A
:start
cls
echo.
echo.
echo 查找电话或姓名----1
echo 添加电话或姓名----2
echo 删除电话或姓名----3
echo 退出本程序--------4
echo.
echo.
set /p var=选择:
set var=%var:~0,1%
call :%var% %0 2>nul || goto :eof


:1
cls
set /p uu=请输入你要查找的姓名或电话号码:
type 电话本.txt | find /i "%uu%" || echo 没有找到你要查找的姓名和电话
echo.
echo.
echo.
pause
goto :start

:2
cls
set /p nn=请输入你要添加的姓名和电话:
echo %nn% >>电话本.txt
echo 添加完毕.
echo.
echo.
echo.
pause
goto :start

:3
cls
set /p del_str=请输入要删除的姓名或电话号码:
findstr /v /c:"%del_str%" 电话本.txt > temp.txt
move /y temp.txt 电话本.txt >nul
echo 删除完毕.
pause
goto :start

:4
exit
```

Here's the explanation of the added part:

- Added a new option "3" for deleting.
- In the `:3` label, it uses `findstr /v` to find lines that do not match the string to be deleted, saves the result to a temporary file, and then moves the temporary file back to replace the original phone book file to achieve the deletion function.
房东说:这娃是个好孩子!
Forum Jump: