asp替换文件中的文本asp

/ / 2023-06-01   阅读:2517
asp替换文件中的文本...

ASP没有内置的文件替换函数,但可以使用FileSystemObject对象实现文件替换。


下面是一个替换文件中指定字符串的函数示例:

Sub ReplaceStringInFile(ByVal file_path, ByVal old_str, ByVal new_str)
    Dim fso, file, content
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    ' 以文本文件格式打开文件
    Set file = fso.OpenTextFile(file_path, 1)
    ' 读取文件内容
    content = file.ReadAll()
    file.Close()
    ' 使用VBScript Replace函数替换字符串
    content = Replace(content, old_str, new_str, 1, -1, vbTextCompare)
    ' 以覆盖模式打开文件,并写入替换后的内容
    Set file = fso.OpenTextFile(file_path, 2, True)
    file.Write(content)
    file.Close()
End Sub


示例中的参数包括:file_path-文件路径,old_str-被替换的字符串,new_str-替换后的字符串。


调用函数的方式如下:

ReplaceStringInFile "E:\test\test.asp","old string","new string"


在执行替换操作前,需要保证文件可写且有读写权限,否则会出现访问拒绝的错误。

我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈