首页 > 互联资讯 > 技术交流  > 

如何对文件进行操作?

<%@ LANGUAGE="VBScript" %>

<% 'Set file i/o constants.
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
filename = "test.txt"
path = Server.MapPath("data") & "" & filename

' 映射文件名到服务器的物理路径.
operation = Request.Form("operation") %>

' 得到请求操作.


</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 9.0pt; mso-ascii-font-family: ' '; mso-hansi-font-family: ' '">千花飞舞之处理文件</SPAN><SPAN lang=EN-US style="FONT-FAMILY: ' '; mso-bidi-font-size: 9.0pt">







" method="post">
创建
删除
读取
写入
添加



确定"> 重置">



返回





<%
if operation = "create" then
call CreateFile(path)
call ReadFile(path)
elseif operation = "delete" then
call DeleteFile(path)
elseif operation = "read" then
call ReadFile(path)
elseif operation = "write" then
call WriteFile(path)
call ReadFile(path)
elseif operation = "append" then
call AppendFile(path)
call ReadFile(path)
end if

' 执行请求操作.
%>





<% sub CreateFile(path)
dim fs, file
set fs = CreateObject("Scripting.FileSystemObject")
if fs.FileExists(path) then
Response.Write(,文件" & filename & " 已经存在,创建失败!
" & vbCrLf)

' 如果文件早已存在则显示一个错误信息
else
Response.Write(
恭喜, " & filename & "创建成功!
" & vbCrLf)

' 否则创建它并写入数据
set file = fs.CreateTextFile(path)
Response.Write(
" & filename & "写入数据...
" & vbCrLf)
file.WriteLine("Line 1 added " & Now() & ".")
file.WriteLine("Line 2 added " & Now() & ".")
file.WriteLine("Line 3 added " & Now() & ".")
file.WriteLine("Line 4 added " & Now() & ".")
file.WriteLine("Line 5 added " & Now() & ".")
file.Close()
end if
end sub
sub DeleteFile(path)
dim fs, file
set fs = CreateObject("Scripting.FileSystemObject")
if not fs.FileExists(path) then

' 如果文件不存在则显示一个错误信息.
Response.Write(
,文件" & filename & "不存在,您删除什么!
" & vbCrLf)
else
Response.Write(恭喜," & filename & "删除成功!
" & vbCrLf)

' 否则删除它.
fs.DeleteFile(path)
end if
end sub
sub ReadFile(path)
dim fs, file
set fs = CreateObject("Scripting.FileSystemObject")
if not fs.FileExists(path) then
Response.Write(
,文件" & filename & "不存在,您读取什么!
" & vbCrLf)
' 如果文件不存在则显示一个错误信息.

else
set file = fs.OpenTextFile(path, ForReading)
Response.Write(
恭喜," & filename & "读取成功!" & vbCrLf)

' 否则打开它并显示内容.
Response.Write("

" & vbCrLf)
Response.Write("


" & vbCrLf)
Response.Write("
" & vbCrLf)
do while not file.AtEndOfStream
Response.Write(file.ReadLine & vbCrLf)
loop
Response.Write("
" & vbCrLf)
Response.Write("
" & vbCrLf)
file.Close()
end if
end sub
sub WriteFile(path)
dim fs, file
set fs = CreateObject("Scripting.FileSystemObject")
if not fs.FileExists(path) then
Response.Write(,文件" & filename & "不存在,您写入什么!
" & vbCrLf)

' 如果文件不存在则显示一个错误信息.

else
set file = fs.OpenTextFile(path, ForWriting)

' 否则,改写内容.
Response.Write(Rewriting file " & filename & ".
" & vbCrLf)
file.WriteLine("Rewritting file with line 1 added " & Now() & ".")
file.WriteLine("Rewritting file with line 2 added " & Now() & ".")
file.WriteLine("Rewritting file with line 3 added " & Now() & ".")
file.Close()
end if

end sub

sub AppendFile(path)

dim fs, file

set fs = CreateObject("Scripting.FileSystemObject")

if fs.FileExists(path) then
set file = fs.GetFile(path)
if file.size > 500 then

' 如果文件存在,检查它的大小并退出,如果它超过500K.
Response.Write(
,文件" & filename & "超过了大小限制, 添加失败!
" &
vbCrLf)
exit sub
end if

else
Response.Write(,文件" & filename & "不存在!但它将会被创建...
" & vbCrLf)
end if
'
如果文件不存在,显示一个提示信息.

Response.Write(
添加内容到文件" & filename & "...
" & vbCrLf)
set file = fs.OpenTextFile(path, ForAppending, true)
file.WriteLine("
添加的新内容:" & Now() & ".")

' 当需要给它添加新内容时打开或创建文件
file.Close()

end sub %>


[1]

如何对文件进行操作?由讯客互联技术交流栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“如何对文件进行操作?