主頁 > 知識庫 > 如何對文件進行操作?

如何對文件進行操作?

熱門標簽:教育機構地圖標注 重慶400電話哪里辦理 武漢如何辦理400電話 語音電銷機器人視頻 常州智能外呼電銷機器人如何 自適應地圖標注 推銷電話機器人怎么打電話的 400電話是怎么申請 遼寧營銷智能外呼系統(tǒng)價格多少

%@ 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">
創(chuàng)建
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

' 執(zhí)行請求操作.
%>
p>
/font>
/body>/html>

% sub CreateFile(path)
    dim fs, file
    set fs = CreateObject("Scripting.FileSystemObject")
    if fs.FileExists(path) then
      Response.Write(b>
,文件" filename " 已經(jīng)存在,創(chuàng)建失敗!/b>br>" vbCrLf)

     ' 如果文件早已存在則顯示一個錯誤信息
    else
      Response.Write(
恭喜, " filename "創(chuàng)建成功!br>" vbCrLf)

' 否則創(chuàng)建它并寫入數(shù)據(jù)
      set file = fs.CreateTextFile(path)
      Response.Write(
" filename "寫入數(shù)據(jù)...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 "不存在!但它將會被創(chuàng)建...br>" vbCrLf)
    end if
    '
如果文件不存在,顯示一個提示信息.

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

' 當需要給它添加新內容時打開或創(chuàng)建文件
    file.Close()

  end sub %>

 


    

[1]

標簽:中山 襄陽 邯鄲 遵義 朔州 柳州 威海 雞西

巨人網(wǎng)絡通訊聲明:本文標題《如何對文件進行操作?》,本文關鍵詞  如何,對,文件,進行,操作,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何對文件進行操作?》相關的同類信息!
  • 本頁收集關于如何對文件進行操作?的相關信息資訊供網(wǎng)民參考!
  • 推薦文章