我本人就是一个懒汉,不过懒汉有自己的方法.我的一个程序中很多文件中地方都用到了同样的语句,但后来才发现,有一个共同的地方需要更改,虽然文件也有五十个左右,也不想一个一个的去改,觉得这是对作为程序员的一种侮辱.呵呵,严重了.所以就编写了下面这个程序,一下就搞定.
其实原来写过,不过,搞忘了.所以这里贴出来,以便于将来查找.
(注:你要替换的内容,最好在文件中是唯一的.否则也许会把你不想替换的地方也替换掉了哦@_@)
<%
dim picWidth,picHeight
picWidth=600
picHeight=400
on error resume next
function get_extend_name(file_name,action_type)'取得文件的扩展名或文件名
n1=instrrev(file_name,".") '得到文件名.的位置
lengtext=len(file_name) '得到文件名长度
n2=cint(lengtext)-cint(n1)+1
if action_type=2 then
exname=left(file_name,cint(lengtext)-n2) '得到文件名的扩展名
elseif action_type=1 then
exname=right(file_name,n2) '得到文件名的扩展名
end if
get_extend_name=Lcase(exname) '返回文件名的扩展名
end function
function getFile(path)'取得指定路径下的全部文件
dim FSO,thisPath,objFile,objFolders,objSubFolders
dim txt,readContent
dim Out
dim ForWriting
ForWriting=2'打开文件用于写入
thisPath=Server.MapPath(path)
set FSO=Server.CreateObject("Scripting.FileSystemObject")
set objFolders=FSO.getFolder(thisPath)
for each objFile in objFolders.files
if Replace(get_extend_name(objFile.name,1),".","")="asp" then
set txt=FSO.openTextFile(Server.mapPath(objFile.name),1)
readContent=txt.readAll
Response.write "文件读取完成"&"<br>"
txt.close
readContent=Replace(readContent,"if Replace(get_extend_name(objFile.name,1),""."","""")=""jpg"" then","if Replace(get_extend_name(objFile.name,1),""."","""")=""jpg"" or Replace(get_extend_name(objFile.name,1),""."","""")=""gif"" then")
'写入文件:方法一
'''''''''''''''''
'Set txt=FSO.CreateTextFile(Server.mapPath(objFile.name))'新建一个同名文件,取代原来的文件
'txt.writeLine(readContent)'这里为什么可以只有这个writeLine文件就可以?
'''''''''''''''''
'写入文件:方法二
'''''''''''''''''
Set txt=FSO.OpenTextFile(Server.mapPath(objFile.name),ForWriting,true)
txt.write readContent
'''''''''''''''''
Response.write "文件写入完成"&"<br>"
txt.close
end if
if err.number>0 then
response.write err.description
err.clear
end if
next
set objFile=nothing
set objFolders=nothing
set FSO=nothing
end function
call getFile("/")
%>