欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何对文件进行操作?

程序员文章站 2022-04-15 09:19:00
<%@ language="vbscript" %><% 'set file i/o cons...

<%@ 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") %>

  ' 得到请求操作.
<html>
<head>
<title>
千花飞舞之处理文件</title>
</head>
<body>
<font face="arial,helvetica" size=2>

<table bgcolor="#000000" border=0 cellpadding=1 cellspacing=0><tr><td>
<table bgcolor="#008080" border=0 cellpadding=8 cellspacing=0><tr valign=bottom><td>
<font face="arial,helvetica" size=2>
<form action="<% = request.servervariables("script_name") %>" method="post">
<input name="operation" type="radio" value="create">
创建
<input name="operation" type="radio" value="delete">
删除
<input name="operation" type="radio" value="read" checked>
读取
<input name="operation" type="radio" value="write">
写入
<input name="operation" type="radio" value="append">
添加
<p>
<center>
<input type="submit" value="
确定"> <input type="reset" value="重置">
</center>
</form>
<p>
<a href="fileops.html"><b>
返回</b></a>
</font>
</td></tr></table>
</td></tr></table>
<p>

<%
  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

' 执行请求操作.
%>
<p>
</font>
</body></html>

<% sub createfile(path)
    dim fs, file
    set fs = createobject("scripting.filesystemobject")
    if fs.fileexists(path) then
      response.write(<b>
,文件" & filename & " 已经存在,创建失败!</b><br>" & vbcrlf)

     ' 如果文件早已存在则显示一个错误信息
    else
      response.write(
恭喜, " & filename & "创建成功!<br>" & vbcrlf)

' 否则创建它并写入数据
      set file = fs.createtextfile(path)
      response.write(
" & filename & "写入数据...<br>" & 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(<b>
,文件" & filename & "不存在,您删除什么!</b><br>" & vbcrlf)
    else
      response.write(
恭喜," & filename & "删除成功!<br>" & 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(<b>
,文件" & filename & "不存在,您读取什么!</b><br>" & vbcrlf)
    '
如果文件不存在则显示一个错误信息.

    else
      set file = fs.opentextfile(path, forreading)
      response.write(
恭喜," & filename & "读取成功!" & vbcrlf)

  ' 否则打开它并显示内容.
      response.write("<p>" & vbcrlf)
      response.write("<hr size=1>" & vbcrlf)
      response.write("<font size=3><pre>" & vbcrlf)
      do while not file.atendofstream
        response.write(file.readline & vbcrlf)
      loop
      response.write("</pre></font>" & vbcrlf)
      response.write("<hr size=1>" & 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(<b>
,文件" & filename & "不存在,您写入什么!</b><br>" & vbcrlf)

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

    else
      set file = fs.opentextfile(path, forwriting)

   ' 否则,改写内容.
      response.write(rewriting file " & filename & ".<br>" & 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(<b>
,文件" & filename & "超过了大小限制, 添加失败!</b><br>" &
vbcrlf)
        exit sub
      end if

    else
      response.write(
,文件" & filename & "不存在!但它将会被创建...<br>" & vbcrlf)
    end if
    '
如果文件不存在,显示一个提示信息.

    response.write(
添加内容到文件" & filename & "...<br>" & vbcrlf)
    set file = fs.opentextfile(path, forappending, true)
    file.writeline("
添加的新内容:" & now() & ".")

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

  end sub %>

 


    

[1]