ASP中使用fso创建,修改TXT文本
ASP中使用fso创建修改TXT文本我就想知道怎么弄的,百度那么多答案,没有一个可以用,垃圾!<formaction="2.asp"method="post"><inpu...
ASP中使用fso创建修改TXT文本我就想知道怎么弄的,百度那么多答案,没有一个可以用,垃圾!
<form action="2.asp" method="post">
<input type="text" size="10" name="username">
<input type="text" size="10" name="homepage">
<input type="text" size="10" name="Email">
</form>
这是文本框,我想把输入的内容写入TXT文档,最好再做个修改,我实在是不会,找了一下午答案了,哎,没有完整的,我也不会改。。郁闷~ 麻烦各位了,谢谢~ 展开
<form action="2.asp" method="post">
<input type="text" size="10" name="username">
<input type="text" size="10" name="homepage">
<input type="text" size="10" name="Email">
</form>
这是文本框,我想把输入的内容写入TXT文档,最好再做个修改,我实在是不会,找了一下午答案了,哎,没有完整的,我也不会改。。郁闷~ 麻烦各位了,谢谢~ 展开
1个回答
展开全部
分少了哦!
呵呵,第一、fso创建TXT文件很简单。
第二、TXT文件要有节点或者项,要不然是不好修改的,最好写成ini的格式,就是
[MySession]
MyItem=
给你一个我自己写的函数吧,是读写INI文件的,INI也是TXT的一种。
<%
'==================================================================
'函数名称:ReadIni() WriteIni() ReadTxt()
'函数参数:ReadIni (FilePath_Name,MySession,MyItem)
' WriteIni(FilePath_Name,MySession,MyItem,MyValue)
'函数作用:读写INI文件
'返 回 值:对于读文件函数,找到给定项返回其值,找不到则返回空字符串;
'写文件函数无返回值,如果函数查找不到对应的节点或项,会自动创建
'CStr_Temp = ReadIni("./Test.ini","Colors","Color1") //读一
个值到CStr_Temp变量
' Call WriteIni("./Test.ini","Colors","Color1",1) //更改
Color1项的值为1
'注意事项:函数严格区分大小写;所读写的INI文件必须是ANSI编码,不支持
Unicode编码
'==================================================================
'=============读INI文件=============
'参数:FilePath_Name 文件相对路径;MySession 节点名称;MyItem 项名称
'返回值:找到给定项返回其值,找不到则返回空字符串
Function ReadIni(FilePath_Name,MySession,MyItem)
Dim MyString, MyArray,str_temp,sesstion_temp
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
ReadIni= Trim(MyArray(1))
Exit Function
End if
End if
Wend
Mytextfile.Close
ReadIni=""
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
'=============写INI文件=============
'参数:FilePath_Name 文件相对路径;MySession 节点名称;MyItem 项名称;
MyValue 写入值
Function WriteIni(FilePath_Name,MySession,MyItem,MyValue)
Dim MyString, MyArray,str_temp,FindBool,FindBool_Session
FindBool=False
FindBool_Session=False
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
If sesstion_temp=MySession then
FindBool_Session=True
End if
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
FindBool=True
End if
End if
Wend
Mytextfile.Close
If Not FindBool_Session Then
FindBool_Session=True
MyString=MyString & "[" & MySession & "]" & VbCrLf
Set MytextFile=MyfileObject.OpenTextFile
(Server.MapPath(FilePath_Name))
MyString=MyString & MytextFile.ReadAll
Mytextfile.Close
Set MytextFile=MyfileObject.CreateTextFile
(Server.MapPath(FilePath_Name))
MytextFile.WriteLine(MyString)
MytextFile.Close
End If
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
MyString=""
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
MyString=MyString & VbCrLf
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
MyArray(1)=MyValue
Str_temp=MyArray(0) & "=" & MyArray(1)
End if
If MyString=VbCrLf then
MyString=MyString & Str_temp
Else
MyString=MyString & VbCrLf & Str_temp
End If
If Not FindBool and sesstion_temp=MySession
Then
FindBool=True
MyString=MyString & VbCrLf & MyItem &
"=" & MyValue
End If
End if
Wend
Mytextfile.Close
Set MytextFile=MyfileObject.CreateTextFile(Server.MapPath
(FilePath_Name))
MytextFile.WriteLine(MyString)
MytextFile.Close
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
%>
当然如果不要节点,只要项也能修改的,把上面的函数精简一下就行了
也就是文件格式直接为:
MyItem=
也给你改个读的例子吧,写你只要稍做更改就行了:
<%
'=============读TXT文件=============
'参数:FilePath_Name 文件相对路径;MyItem 项名称
'返回值:找到给定项返回其值,找不到则返回空字符串
Function ReadTxt(FilePath_Name,MyItem)
Dim MyString, MyArray,str_temp,sesstion_temp
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem then
ReadTxt= Trim(MyArray(1))
Exit Function
End if
End if
Wend
Mytextfile.Close
ReadTxt=""
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
%>
把以上的代码另外保存成一个文件,如3.asp
你的2.asp就可以这样了:
<!--#include file="3.asp"-->
<%
username=request.form("username")
homepage=request.form("homepage")
email=request.form("email")
Call WriteIni("XXX.ini","ziliao","username",""&username&"")
Call WriteIni("XXX.ini","ziliao","homepage",""&homepage&"")
Call WriteIni("XXX.ini","ziliao","email",""&email&"")
%>
这个XXX.ini可以为任意文件名,文件生成的格式如下
XXX.ini
[ziliao]
username=
homepage=
email=
如果在后面要用到文件里面的内容就用下面格式调用ReadIni函数:
username=ReadIni("XXX.ini","ziliao","username")
呵呵,这样很方便吧
当然实际应用上面可以把username当做节点就更好了,那就不会有重复的了。
具体做法就是把上面的ziliao用username变量代替!
Call WriteIni("XXX.ini",""&username&"","homepage",""&homepage&"")
修改的方法就是直接写相同的节点相同的项进去就行了,跟创建时的方法是一样的。
呵呵,第一、fso创建TXT文件很简单。
第二、TXT文件要有节点或者项,要不然是不好修改的,最好写成ini的格式,就是
[MySession]
MyItem=
给你一个我自己写的函数吧,是读写INI文件的,INI也是TXT的一种。
<%
'==================================================================
'函数名称:ReadIni() WriteIni() ReadTxt()
'函数参数:ReadIni (FilePath_Name,MySession,MyItem)
' WriteIni(FilePath_Name,MySession,MyItem,MyValue)
'函数作用:读写INI文件
'返 回 值:对于读文件函数,找到给定项返回其值,找不到则返回空字符串;
'写文件函数无返回值,如果函数查找不到对应的节点或项,会自动创建
'CStr_Temp = ReadIni("./Test.ini","Colors","Color1") //读一
个值到CStr_Temp变量
' Call WriteIni("./Test.ini","Colors","Color1",1) //更改
Color1项的值为1
'注意事项:函数严格区分大小写;所读写的INI文件必须是ANSI编码,不支持
Unicode编码
'==================================================================
'=============读INI文件=============
'参数:FilePath_Name 文件相对路径;MySession 节点名称;MyItem 项名称
'返回值:找到给定项返回其值,找不到则返回空字符串
Function ReadIni(FilePath_Name,MySession,MyItem)
Dim MyString, MyArray,str_temp,sesstion_temp
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
ReadIni= Trim(MyArray(1))
Exit Function
End if
End if
Wend
Mytextfile.Close
ReadIni=""
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
'=============写INI文件=============
'参数:FilePath_Name 文件相对路径;MySession 节点名称;MyItem 项名称;
MyValue 写入值
Function WriteIni(FilePath_Name,MySession,MyItem,MyValue)
Dim MyString, MyArray,str_temp,FindBool,FindBool_Session
FindBool=False
FindBool_Session=False
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
If sesstion_temp=MySession then
FindBool_Session=True
End if
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
FindBool=True
End if
End if
Wend
Mytextfile.Close
If Not FindBool_Session Then
FindBool_Session=True
MyString=MyString & "[" & MySession & "]" & VbCrLf
Set MytextFile=MyfileObject.OpenTextFile
(Server.MapPath(FilePath_Name))
MyString=MyString & MytextFile.ReadAll
Mytextfile.Close
Set MytextFile=MyfileObject.CreateTextFile
(Server.MapPath(FilePath_Name))
MytextFile.WriteLine(MyString)
MytextFile.Close
End If
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
MyString=""
sesstion_temp=""
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" Then
If InStr(Trim(Str_temp),"[")<InStr(Trim
(Str_temp),"]") Then
sesstion_temp=Trim(Str_temp)
sesstion_temp=Replace(Trim
(sesstion_temp),"[","")
sesstion_temp=Replace(Trim
(sesstion_temp),"]","")
MyString=MyString & VbCrLf
End If
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem and
sesstion_temp=MySession then
MyArray(1)=MyValue
Str_temp=MyArray(0) & "=" & MyArray(1)
End if
If MyString=VbCrLf then
MyString=MyString & Str_temp
Else
MyString=MyString & VbCrLf & Str_temp
End If
If Not FindBool and sesstion_temp=MySession
Then
FindBool=True
MyString=MyString & VbCrLf & MyItem &
"=" & MyValue
End If
End if
Wend
Mytextfile.Close
Set MytextFile=MyfileObject.CreateTextFile(Server.MapPath
(FilePath_Name))
MytextFile.WriteLine(MyString)
MytextFile.Close
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
%>
当然如果不要节点,只要项也能修改的,把上面的函数精简一下就行了
也就是文件格式直接为:
MyItem=
也给你改个读的例子吧,写你只要稍做更改就行了:
<%
'=============读TXT文件=============
'参数:FilePath_Name 文件相对路径;MyItem 项名称
'返回值:找到给定项返回其值,找不到则返回空字符串
Function ReadTxt(FilePath_Name,MyItem)
Dim MyString, MyArray,str_temp,sesstion_temp
Set MyfileObject=Server.CreateObject
("Scripting.FileSystemObject")
Set MytextFile=MyfileObject.OpenTextFile(Server.MapPath
(FilePath_Name))
While Not MytextFile.AtendOfStream
Str_temp= MytextFile.ReadLine
If Trim(Str_temp)<>"" and InStr(Trim(Str_temp),";")<>1
Then
MyArray = Split(Trim(Str_temp), "=")
If Trim(MyArray(0))=MyItem then
ReadTxt= Trim(MyArray(1))
Exit Function
End if
End if
Wend
Mytextfile.Close
ReadTxt=""
Set MyfileObject=Nothing
Set MytextFile=Nothing
End Function
%>
把以上的代码另外保存成一个文件,如3.asp
你的2.asp就可以这样了:
<!--#include file="3.asp"-->
<%
username=request.form("username")
homepage=request.form("homepage")
email=request.form("email")
Call WriteIni("XXX.ini","ziliao","username",""&username&"")
Call WriteIni("XXX.ini","ziliao","homepage",""&homepage&"")
Call WriteIni("XXX.ini","ziliao","email",""&email&"")
%>
这个XXX.ini可以为任意文件名,文件生成的格式如下
XXX.ini
[ziliao]
username=
homepage=
email=
如果在后面要用到文件里面的内容就用下面格式调用ReadIni函数:
username=ReadIni("XXX.ini","ziliao","username")
呵呵,这样很方便吧
当然实际应用上面可以把username当做节点就更好了,那就不会有重复的了。
具体做法就是把上面的ziliao用username变量代替!
Call WriteIni("XXX.ini",""&username&"","homepage",""&homepage&"")
修改的方法就是直接写相同的节点相同的项进去就行了,跟创建时的方法是一样的。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询