相信有ASP开发经历的人都会有这种需求:需要从一堆的asp文件中查找几个指定的字符。
特别是修改从网上当下来的代码时,为了找一个字符苦苦寻找。
这个工具可以自动搜索你指定的目录下所有的文件,查找是否包含指定的字符。如果存在,则输出包含字符的文件路径及名称、查找字符在文件中的行数。
大家如果发现什么问题,欢迎大家和我一起讨论 !
[email protected]
程序源代码如下:
<%
'===============================================================
'【功能】在指定文件夹中查找字符串
'【参数】
'【作者】Garfield 2006/06/08
'【版本】V1.0
'===============================================================
Dim PFilePath, PFileType, PSearchText
Dim PHaveFolder
If Request("btnSearch") <> "" Then
'=====================================
'数据初始化、获取传递的参数值
'查找的文件路径
PFilePath = Request("FilePath")
'查找的字符
PSearchText = Request("SearchText")
'查找的文件类型
PFileType = Request("FileType")
'根目录是否包含子目录
PHaveFolder = False
'=====================================
'数据验证
If PFilePath = "" Then
Response.Write "<script language='javascript'>alert('请设置[查找路径]!');history.go(-1);</script>"
Response.End
ElseIf PSearchText = "" Then
Response.Write "<script language='javascript'>alert('请设置[查找字符]!');history.go(-1);</script>"
Response.End
ElseIf PFileType = "" Then
Response.Write "<script language='javascript'>alert('请设置[文件类型]!');history.go(-1);</script>"
Response.End
End If
Else
PFilePath = ""
PSearchText = ""
PFileType = "txt|asp|htm|html|aspx"
End If
%>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>字符查找工具</TITLE>
</HEAD>
<BODY>
<TABLE border=0 width=100%>
<form name=frm method=post>
<tr>
<TD width=130>设置查找路径:</TD>
<TD><INPUT type=text name=FilePath value="<%= PFilePath %>" size="54"></TD>
</tr>
<tr>
<TD width=130>设置查找字符:</TD>
<TD><INPUT type=text name=SearchText value="<%= PSearchText %>" size="54"></TD>
</tr>
<tr>
<TD width=130>设置文件类型:</TD>
<TD><INPUT type=text name=FileType value="<%= PFileType %>" size="54"></TD>
</tr>
<tr>
<TD colspan=2 align=center><INPUT type=submit name=btnSearch value=' 查找 '></TD>
</tr>
</form>
<tr>
<TD colspan="2">查询结果:</TD>
</tr>
</TABLE>
</BODY>
</HTML>
<%
If Request("btnSearch") <> "" Then
'=====================================
'调用查找函数
GetSearchResult(PFilePath)
If PHaveFolder = True Then
End If
End If
'获取当前目录下的所有子目录
Sub GetSearchResult(VFolderPath)
Dim SFSO, SFolder, SSubFolder,SFolderTemp
Dim FHaveSF
Set SFSO = CreateObject("Scripting.FileSystemObject")
Set SFolder = SFSO.GetFolder(VFolderPath)
Set SSubFolder = SFolder.SubFolders
For Each SFolderTemp In SSubFolder
FHaveSF = True
GetSearchResult(SFolderTemp.Path)
Next
'获取当前目录下的所有文件
GetSubFile(VFolderPath)
End Sub
'获取当前目录下的所有文件
Sub GetSubFile(VFolderPath)
Dim SFSO, SFolder, SFile, SSubFile
Dim SFileType,STemp
Set SFSO = CreateObject("Scripting.FileSystemObject")
Set SFolder = SFSO.GetFolder(VFolderPath)
Set SFile = SFolder.Files
For Each SSubFile in SFile
'======== 检查文件的类型 =======
STemp = Split(SSubFile.Name, ".")
If UBound(STemp) > 0 Then
SFileType = LCase(STemp(UBound(STemp)))
STemp = Replace(PFileType,SFileType,"")
If Len(STemp) < Len(PFileType) Then
'====== 在文件中查找指定字符 =====
Call StringSearch(SSubFile.path,PSearchText)
End If
End If
Next
End Sub
'查找所有文件,是否包含指定的字符
Sub StringSearch(VFilePath, VSearchText)
Const ForReading = 1, ForWriting = 2
Dim SFSO, SFile, SStringTemp, SReplaceText, SLineCounter
VSearchText = LCase(VSearchText)
SLineCounter = 1
SIndex = 0
Set SFSO = CreateObject("Scripting.FileSystemObject")
Set SFile = SFSO.OpenTextFile(VFilePath, ForReading)
While Not SFile.AtEndOfStream
SStringTemp = SFile.ReadLine
SStringTemp = LCase(SStringTemp)
SReplaceText = Replace(SStringTemp,VSearchText,"")
If Len(SReplaceText) < Len(SStringTemp) Then
Response.Write VFilePath & " " & "[line:" & SLineCounter & "]" & "<BR>"
End If
SLineCounter = SLineCounter + 1
Wend
SFile.Close
End Sub
%>