找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 2110|回复: 6

[【原创】] 文本替换工具2007源代码解析

[复制链接]
发表于 2007-9-5 09:20:54 | 显示全部楼层 |阅读模式
文本批量替换工具2007版说明书
功能描述
1、  创建,打开,修改,保存文本文件。
2、  按用户要求批量替换文本文件内容。用户批量替换可以选择三种不同的替换方法:①屏蔽手机号码的末四位;②仅保留手机号码;③自定义替换。前面两种方法是针对无线媒体公司的短信发送报表的特点而定制的。自定义替换适用于替换所有的文本文件。
模式说明
1.  经典模式。可创建,打开,修改,保存,替换文本文件。用户在使用第一、二种替换方法的时候不要求文本文件的数据格式良好。系统有智能检索机制,可自动分辨文本中的手机号码,发送时间,重发次数,发送状况等数据。(目前版本没有设置分类统计这些数据的功能。)为了不引起系统超长时间处于处理分析数据(经典模式程序执行速度比极速模式慢8到10倍,在我的测试中,包含3.6万条短信发送情况的大小为1.43兆的短信报告,执行替换任务需要耗时4至6分钟)的困境。建议在经典模式下,文件大小不要超过100KB。
2.  极速模式。只有替换文本文件的功能。用户在使用第一、二种替换方法的时候要求文件格式良好。系统取消了浏览文本的功能并优化了核心代码,极大的提高了程序的执行速度。在我的测试中,包含3.6万条短信发送情况的大小为1.43兆的短信报告,执行替换任务需要耗时3至7秒。建议在极速模式下,文件大小不要超过10兆。
格式良好的文件示例:
[文件主题部分]
接收手机号码  发送时间  重发次数  发送状态
1399031**** 07-7-26 8:59:15  0  10
1397150**** 07-7-26 8:59:15  0  10
为了保护个人隐私,在此屏蔽了手机号码的末四位数字。
系统环境要求
适用于windows系统。本系统已经在windows XP professional及windows server 2003环境下测试通过。(我只装了两个系统)。本系统是绿色免安装软件。下载解压缩后可直接运行。
版权说明
作者:自考中国网www.chinazk.com 911
Email:chinazk911@gmail.com
授权:本系统是开源免费软件,任何人都可以从自考中国网获得源代码。因此为了防止别有用心的人在本系统里嵌入恶意代码,建议您从自考中国网下载本工具。您有任何宝贵的意见,都欢迎您来信与我交流。
源代码
主窗口(经典模式—form1)
‘====================================================
‘版本:2007测试版
‘作者:自考中国网 911
Email:chinazk911@gmail.com
‘网址:www.chinazk.com
‘声明:您可以合法的自由使用本系统的所有代码,但请保留这段声明。
‘=========================================================
'dim TextChange=1表示文件已经被修改 TextSave=1表示文件已经被保存
Dim TextChange, TextSave As Integer
Dim filename As String


Private Sub CmdExit_Click()
msg = MsgBox(\"是否退出?\", vbOKCancel)
  If msg = 1 Then
  End
  End If
End Sub

Private Sub CmdHelp_Click()
Unload Me
frmHelp.Show


End Sub

Private Sub CmdNew_Click()
'若文件被修改了且未保存,则在新建文件的时候提示保存
'即TextChange=1 and TextSave=0
If TextChange = 1 And TextSave = 0 Then
  msg = MsgBox(\"是否保存修改?\", vbOKCancel)
  If msg = 1 Then
  CommonDialog1.ShowSave
filename = CommonDialog1.filename
   Open filename For Output As #9
Print #9, Text1.Text
TextSave = 1
TextChange = 0
Close #9
End If
End If
form1.Caption = \"无标题.txt-文本替换工具\"

'内容显示框清空
Text1.Text = \"\"
'内容显示框获得焦点
Text1.SetFocus
'文件保存状态设置为未保存
TextSave = 0
Text2.Text = \"新建文件\"
End Sub

Private Sub CmdOpen_Click()
Dim curstring As String
On Error GoTo openerror
If TextSave = 0 Then
  msg = MsgBox(\"是否保存修改?\", vbOKCancel)
  If msg = 1 Then
  CommonDialog1.ShowSave
filename = CommonDialog1.filename
  Open filename For Output As #9
Print #9, Text1.Text
TextSave = 1
TextChange = 0
Close #9
  End If
End If
Text1.Text = \"\"
CommonDialog1.ShowOpen
filename = CommonDialog1.filename
If filename = \"\" Then Exit Sub
'打开文件
If CommonDialog1.CancelError = True Then Exit Sub
Text2.Text = \"正在加载文件....\"
Open filename For Input As #1
form1.Caption = filename + \"-文本替换工具\"

'逐行读取内容
Do While Not EOF(1)
Line Input #1, curstring
Text1.Text = Text1.Text + curstring + Chr(13) + Chr(10)
Loop
'Text1.Text = Input(LOF(1), #1)
'msg = MsgBox(Text1.Text, vbOKCancel)
''Do While Not EOF(1)
'Input #1, curstring
'Text1.Text = Text1.Text + curstring
'Loop
'关闭文件
Close #1
Text2.Text = \"已经打开文件\"
'因为上面的加载改变了text1的值,所以在此要重新设置文本没有被修改。
TextChange = 0
TextSave = 1

openerror:
  Exit Sub
End Sub

Private Sub CmdReplace_Click()
OptionPho(0).Value = True
OptionPho(0).Enabled = True
OptionPho2.Value = False
OptionPho2.Enabled = True

OptionFree.Value = False
OptionFree.Enabled = True
Command1.Enabled = True
Command2.Enabled = True

txtFind.Enabled = False
txtReplace.Enabled = False
'禁止新建,打开,保存按钮的使用
CmdNew.Enabled = False
CmdOpen.Enabled = False
CmdSave.Enabled = False
Text2.Text = \"替换参数设置\"

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复

使用道具 举报

 楼主| 发表于 2007-9-5 09:21:57 | 显示全部楼层
End Sub

Private Sub CmdSave_Click()
'保存文件
CommonDialog1.ShowSave
filename = CommonDialog1.filename
If filename = "" Then Exit Sub
Text2.Text = "正在保存文件...."
Open filename For Output As #2
form1.Caption = filename + "-文本替换工具"
Print #2, Text1.Text
TextSave = 1
TextChange = 0
Close #2
Text2.Text = "已经保存文件"
End Sub

Private Sub Command1_Click()
'替换操作
Dim string_1 As String
Dim txt_save As String
Text2.Text = "正在执行替换命令..."
'打开文件
Open filename For Input As #1
'逐行读取内容放入变量l_1
Do While EOF(1) = False
Line Input #1, string_1
'========================================================
'逐行修改内容
'l_12 = l_12 + l_1 + "asd" + Chr(13) + Chr(10)
'获得该行的长度
string_len = Len(string_1)
'利用for循环查找手机号码的该行开始位置
string_Find = False
string_Findnum = 0
If string_len >= 11 Then
  For j = 1 To string_len
    '判断其后是否还有10个字符
     If j + 10 > string_len Then
      
      Exit For
     End If
   string_Mid = Mid(string_1, j, 1)
   '检查该字是否是数字
   If IsNumeric(string_Mid) = True Then
    '检查其后10个字符是否是数字
    string_Mid2 = Mid(string_1, j + 10, 1)
    If IsNumeric(string_Mid2) = True Then
    '找到手机号码
     string_Find = True
     string_Findnum = j
     Exit For
    End If
   
   End If
   
  Next
End If
'-------------------------------------------------
'处理数据
For j1 = 1 To 1
If string_Find = True Then
  '屏蔽手机末四位数字
  If OptionPho(0).Value = True Then
   string_mid3 = Mid(string_1, j + 7, 4)
   '替换数字
   string_1 = Replace(string_1, string_mid3, "****")
   Exit For
  End If
  '只保留手机号码
  If OptionPho2.Value = True Then
   string_mid3 = Mid(string_1, j, 11)
   string_1 = string_mid3
   Exit For
  End If
  '自定义替换
  If OptionFree.Value = True Then
   rep1 = txtFind.Text
   rep2 = txtReplace.Text
   string_1 = Replace(string_1, rep1, rep2)
   Exit For
  End If
   
End If
Next
'---------------------------------------------------
txt_save = txt_save + string_1 + Chr(13) + Chr(10)

'======================================================
Loop
'关闭文件
Close #1
'保存文件
Open filename For Output As #2
Print #2, txt_save
Text2.Text = "替换完成"
'========================================================
'************显示替换的内容************
Text2.Text = "正在加载文件...."
Open filename For Input As #5
form1.Caption = filename + "-文本替换工具"

'逐行读取内容
Do While Not EOF(5)
Line Input #5, curstring
Text1.Text = Text1.Text + curstring + Chr(13) + Chr(10)
Loop
'关闭文件
Close #5
Text2.Text = "已经打开文件"
TextSave = 1
TextChange = 0

'******************************************
'==============================================================

End Sub

Private Sub Command2_Click()
'取消替换操作
'OptionPho(0).Value = false
OptionPho(0).Enabled = False
'OptionPho2.Value = False
OptionPho2.Enabled = False

'OptionFree.Value = False
OptionFree.Enabled = False
Command1.Enabled = False
Command2.Enabled = False

txtFind.Enabled = False
txtReplace.Enabled = False
'禁止新建,打开,保存按钮的使用
CmdNew.Enabled = True
CmdOpen.Enabled = True
CmdSave.Enabled = True
Text2.Text = "已经取消替换命令"

End Sub


Private Sub Command4_Click()
Unload Me
Form2.Show
End Sub

Private Sub Form_Load()
Text1.Text = ""
TextSave = 1
TextChange = 0

'打开文件
'Open "c:\11.txt" For Input As #1
'逐行读取内容放入变量l_1
'Do While EOF(1) = False
'Line Input #1, l_1
'逐行修改内容
'l_12 = l_12 + l_1 + "asd" + Chr(13) + Chr(10)
&#39rint #1, l_12
'Loop
'关闭文件
'Close #1
'保存文件
'Open "c:\11.txt" For Output As #2
&#39rint #2, l_12
End Sub

Private Sub OptionFree_Click()
txtFind.Enabled = True
txtReplace.Enabled = True
End Sub

Private Sub OptionPho_Click(Index As Integer)
txtFind.Enabled = False
txtReplace.Enabled = False

End Sub

Private Sub OptionPho2_Click()
txtFind.Enabled = False
txtReplace.Enabled = False
End Sub

Private Sub Text1_Change()
TextChange = 1
TextSave = 0

End Sub

Private Sub txtFind_GotFocus()
'获得焦点,清除默认值
txtFind.Text = ""
End Sub

Private Sub txtReplace_GotFocus()
txtReplace.Text = ""
End Sub
回复

使用道具 举报

 楼主| 发表于 2007-9-5 09:22:29 | 显示全部楼层
极速模式(form2)
Dim filename
Private Sub CmdReplace_Click()
OptionPho(0).Value = True
OptionPho(0).Enabled = True
OptionPho2.Value = False
OptionPho2.Enabled = True

OptionFree.Value = False
OptionFree.Enabled = True
Command1.Enabled = True
Command2.Enabled = True
CmdReplace.Enabled = False

txtFind.Enabled = False
txtReplace.Enabled = False

CommonDialog1.ShowOpen
filename = CommonDialog1.filename
If filename = "" Then Exit Sub
'打开文件
If CommonDialog1.CancelError = True Then Exit Sub
'==================================================================

Text2.Text = "替换参数设置"


End Sub

Private Sub Command1_Click()
'替换操作
Dim string_1 As String
Dim txt_save As String
Text2.Text = "正在执行替换命令..."
'打开文件
Open "c:\temp.txt" For Output As #10
Print #10, ""
Close #10
Open filename For Input As #4
Open "c:\temp.txt" For Append As #3
'逐行读取内容放入变量l_1
Do While EOF(4) = False
Line Input #4, string_1
'========================================================
'逐行修改内容
'l_12 = l_12 + l_1 + "asd" + Chr(13) + Chr(10)
'获得该行的长度
string_len = Len(string_1)
'利用for循环查找手机号码的该行开始位置
string_Find = False
string_Findnum = 0
If string_len >= 11 Then
  For j = 1 To string_len
    '判断其后是否还有10个字符
     If j + 10 > string_len Then
      
      Exit For
     End If
   string_Mid = Mid(string_1, j, 1)
   '检查该字是否是数字
   If IsNumeric(string_Mid) = True Then
    '检查其后10个字符是否是数字
    string_Mid2 = Mid(string_1, j + 10, 1)
    If IsNumeric(string_Mid2) = True Then
    '找到手机号码
     string_Find = True
     string_Findnum = j
     Exit For
    End If
   
   End If
   
  Next
End If
'-------------------------------------------------
'处理数据
For j1 = 1 To 1
If string_Find = True Then
  '屏蔽手机末四位数字
  If OptionPho(0).Value = True Then
   string_mid3 = Mid(string_1, j + 7, 4)
   '替换数字
   string_1 = Replace(string_1, string_mid3, "****")
   Exit For
  End If
  '只保留手机号码
  If OptionPho2.Value = True Then
   string_mid3 = Mid(string_1, j, 11)
   string_1 = string_mid3
   Exit For
  End If
  '自定义替换
  If OptionFree.Value = True Then
   rep1 = txtFind.Text
   rep2 = txtReplace.Text
   string_1 = Replace(string_1, rep1, rep2)
   Exit For
  End If
   
End If
Next
'---------------------------------------------------
txt_save = string_1
'把数据存入临时文件

Print #3, txt_save



'======================================================
Loop
'关闭文件
Close #4
Close #3
'复制文件
FileCopy "c:\temp.txt", filename

'保存文件
'Open filename For Output As #2
&#39rint #2, txt_save
Text2.Text = "替换完成"
CmdReplace.Enabled = True
Command2.Enabled = False
'========================================================
'************显示替换的内容************
'Text2.Text = "正在加载文件...."
'Open filename For Input As #5
'form1.Caption = filename + "-文本替换工具"

'逐行读取内容
'Do While Not EOF(5)
' Line Input #5, curstring
' Text1.Text = Text1.Text + curstring + Chr(13) + Chr(10)
'Loop
'关闭文件
'Close #5
'Text2.Text = "已经打开文件"
'******************************************
'==============================================================

End Sub

Private Sub Command2_Click()
CmdReplace.Enabled = True
txtFind.Enabled = False
txtReplace.Enabled = False
OptionFree.Value = False
OptionPho(0).Value = True
End Sub

Private Sub Command3_Click()
Unload Me
form1.Show
End Sub
回复

使用道具 举报

 楼主| 发表于 2007-9-5 09:23:11 | 显示全部楼层
Private Sub Form_Load()

End Sub

Private Sub OptionFree_Click()
txtFind.Enabled = True
txtReplace.Enabled = True

End Sub

Private Sub OptionPho_Click(Index As Integer)
txtFind.Enabled = False
txtReplace.Enabled = False

End Sub

Private Sub OptionPho2_Click()
txtFind.Enabled = False
txtReplace.Enabled = False
End Sub



Private Sub txtFind_GotFocus()
'获得焦点,清除默认值
txtFind.Text = ""
End Sub


Private Sub txtReplace_GotFocus()
txtReplace.Text = ""
End Sub
回复

使用道具 举报

 楼主| 发表于 2007-9-5 09:24:58 | 显示全部楼层
本工具是本人原创作品,用vb写的,生成的exe文件在自考中国网有下。这篇解析文章系首发原创。
回复

使用道具 举报

发表于 2007-9-6 09:40:56 | 显示全部楼层
引用第4楼hn911于2007-09-05 09:24发表的 :
本工具是本人原创作品,用vb写的,生成的exe文件在自考中国网有下。这篇解析文章系首发原创。

exe和源码文件也发上来吧
回复

使用道具 举报

 楼主| 发表于 2007-9-6 11:03:33 | 显示全部楼层
exe和源代码文件

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-6-15 10:45 , Processed in 0.469306 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表