0

0

Asp 对文件操作问题总结

怪我咯

怪我咯

发布时间:2017-03-30 13:56:28

|

1292人浏览过

|

来源于php中文网

原创


读取一个文件相关信息的范例

<% Dim objFSO,objFile '声明一个名称为 objFSO 的变量以存放对象实例 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(Server.MapPath("newfile.asp")) Then Set objFile = objFSO.GetFile(Server.MapPath("newfile.asp")) Response.Write "文件的路径:"&objFile.Path&"
" Response.Write "文件的建立日期:"&objFile.DateCreated&"
" Response.Write "文件的大小:"&objFile.Size&"
" Response.Write "文件的类型:"&objFile.Type&"
" Else Response.Write Server.MapPath("newfile.asp")&"不存在,无法读取相关信息" End If Set objFile = Nothing Set objFSO = Nothing '释放 FileSystemObject 对象实例内存空间 %>
动网论坛的代码,你参考一下吧



<%=Forum_info(0)%>--管理页面


>
<%
	if not master or session("flag")="" then
		Errmsg=Errmsg+"
"+"
  • 本页面为管理员专用,请登陆后进入。
  • 您没有管理本页面的权限。" call dvbbs_error() response.end end if on error resume next Sub ShowSpaceInfo(drvpath) dim fso,d,size,showsize set fso=server.createobject("scripting.filesystemobject") drvpath=server.mappath(drvpath) set d=fso.getfolder(drvpath) size=d.size showsize=size & " Byte" if size>1024 then size=(size/1024) showsize=size & " KB" end if if size>1024 then size=(size/1024) showsize=formatnumber(size,2) & " MB" end if if size>1024 then size=(size/1024) showsize=formatnumber(size,2) & " GB" end if response.write "" & showsize & "" End Sub Sub Showspecialspaceinfo(method) dim fso,d,fc,f1,size,showsize,drvpath set fso=server.createobject("scripting.filesystemobject") drvpath=server.mappath("pic") drvpath=left(drvpath,(instrrev(drvpath,"/")-1)) set d=fso.getfolder(drvpath) if method="All" then size=d.size elseif method="Program" then set fc=d.Files for each f1 in fc size=size+f1.size next end if showsize=size & " Byte" if size>1024 then size=(size/1024) showsize=size & " KB" end if if size>1024 then size=(size/1024) showsize=formatnumber(size,2) & " MB" end if if size>1024 then size=(size/1024) showsize=formatnumber(size,2) & " GB" end if response.write "" & showsize & "" end sub Function Drawbar(drvpath) dim fso,drvpathroot,d,size,totalsize,barsize set fso=server.createobject("scripting.filesystemobject") drvpathroot=server.mappath("pic") drvpathroot=left(drvpathroot,(instrrev(drvpathroot,"/")-1)) set d=fso.getfolder(drvpathroot) totalsize=d.size drvpath=server.mappath(drvpath) set d=fso.getfolder(drvpath) size=d.size barsize=cint((size/totalsize)*400) Drawbar=barsize End Function Function Drawspecialbar() dim fso,drvpathroot,d,fc,f1,size,totalsize,barsize set fso=server.createobject("scripting.filesystemobject") drvpathroot=server.mappath("pic") drvpathroot=left(drvpathroot,(instrrev(drvpathroot,"/")-1)) set d=fso.getfolder(drvpathroot) totalsize=d.size set fc=d.files for each f1 in fc size=size+f1.size next barsize=cint((size/totalsize)*400) Drawspecialbar=barsize End Function %>
    系统空间占用情况
    法规数据占用空间: 备份数据占用空间: 程序文件占用空间: 心情图片占用空间: 系统图片占用空间: 上传头像占用空间: 上传图片占用空间: 系统占用空间总计:
  • ASP--文件操作类 设计版


    '====================================================
    'version:  aspfile 1.0
    'data:  2006.6.20
    'copyright: (c) 2006 evan.cn all rights reserved
    '====================================================
    '文件操作类
    '文件名称
    '文件大小
    '文件创建
    'filename--文件名称
    'destinationfile--目标文件名称
    'filesize--获得文件大小
    'filecreateddate--获得文件创建日期
    'fileattributes--获得文件属性
    'showfileaccessinfo(infotype)--获得文件属性
    'createtxtfile()--创建文本文件
    'writetxtfile(textstr,writeorappendtype)==写文本文件
    'readtxtfile()--读取文本文件
    'deletefile()--删除文件
    'copyfile()--拷贝文件
    'movefile()--移动文件
    '
    '
    class cls_fso
    private o_fso,s_filename,s_foldername,s_destinationfile
    private s_foldername,s_destinationfolder

    private sub class_initialize()
             set o_fso = createobject("scripting.filesystemobject")
       end sub

    private sub class_terminate()
           set o_fso = nothing
       end sub

    '===============================================文件操作

    '设置文件名字
    public property let filename(byval values)
     s_filename=values
     'checkfile '检查文件
    end property

    '设置目标文件名
    public property let destinationfile(byval values)
     s_destinationfile=values
    end property


    '取文件大小
    public property get filesize()
     if fileexist then
      dim f
      set f = o_fso.getfile(s_filename)
               filesize = f.size
     else
      filesize=0
     end if
    end property

    '取文件
    public property get filecreateddate()
     if fileexist then
      dim f
      set f = o_fso.getfile(s_filename)
      filecreateddate = f.datecreated
     else
      filecreateddate="1900-1-1"
     end if

    end property

    '取文件大小
    public property get fileattributes()
     if fileexist then
      dim f,str
      set f = o_fso.getfile(s_filename)
      select case f.attributes
       case 0 str="普通文件。没有设置任何属性。 "
       case 1 str="只读文件。可读写。  "
       case 2 str="隐藏文件。可读写。  "
       case 4 str="系统文件。可读写。  "
       case 16 str="文件夹或目录。只读。  "
       case 32 str="上次备份后已更改的文件。可读写。 "
       case 1024 str="链接或快捷方式。只读。  "
       case 2048 str=" 压缩文件。只读。"
      end select
      fileattributes=str
     else
      fileattributes="文件不存在或不可预测地属性"
     end if

    end property

    public function showfileaccessinfo(infotype)
       '//功能:显示文件创建时信息
       '//形参:文件名,信息类别
       '//        1  -----创建时间
       '//        2  -----上次访问时间
       '//        3  -----上次修改时间
       '//        4  -----文件路径
       '//        5  -----文件名称
       '//        6  -----文件类型
       '//        7  -----文件大小
       '//        8  -----父目录
       '//        9  -----根目录
     '判断参数是否是数字
           if fileexist then
      dim f, s
      set f = o_fso.getfile(s_filename)
      select case infotype
       case 1
        s = f.datecreated        '//        1  -----创建时间
       case 2
        s = f.datelastaccessed    '//        2  -----上次访问时间
       case 3
        s = f.datelastmodified    '//        3  -----上次修改时间
       case 4
        s = f.path                '//        4 -----文件路径
       case 5
        s = f.name                '//        5 -----文件名称
       case 6
        s = f.type                '//        6 -----文件类型
       case 7
        s = f.size                '//        7 -----文件大小
       case 8
        s = f.parentfolder        '//        8  -----父目录
       case 9
        s = f.rootfolder            '//        9  -----根目录
      end select
      showfileaccessinfo=s
     else
      showfileaccessinfo="文件不存在:("
     end if

       end function

    private function fileexist
     fileexist = true
     '检查文件路径

     '检查文件是否存在
     if not(o_fso.fileexists(s_filename)) then
      fileexist = false
           end if
    end function

    private function showerr(s_msg)
     response.write(s_msg)
     set fso=nothing
     response.end()
    end function

    public function createtxtfile()
    '//创建空文件
     if fileexist then
      showerr "文件已经存在:("
     else
      dim f
      set f=o_fso.createtextfile(s_filename)
      f.close
     end if
    end function

    public function writetxtfile(textstr,writeorappendtype)
     const forreading = 1, forwriting = 2 , forappending = 8
           dim f, m
           select case writeorappendtype
           case 1:            '文件进行写操作
               set f = o_fso.opentextfile(s_filename, forwriting, true)
               f.write textstr
               f.close
               if not fileexist then
                   showerr "文件写入失败"
               end if
           case 2:            '文件末尾进行写操作
               if fileexist then
                   set f = o_fso.opentextfile(s_filename, forappending)
                   f.write textstr
                   f.close
               else
                   showerr "文件追加失败"
               end if
           end select
    end function

    public function readtxtfile()
     const forreading = 1, forwriting = 2
           dim f, m
           if fileexist then
               set f = o_fso.opentextfile(s_filename, forreading)
               m =  f.readline
               'm =  f.readall
               'f.skipline
               readtxtfile = m
               f.close
           else
               readtxtfile = "文件读取失败-文件不存在:("
           end if
    end function

    public function deletefile()
           if fileexist then
      o_fso.deletefile(s_filename)
     else
      showerr "文件已经不存在:("
     end if
    end function

    public function copyfile()
    '//功能:源文件存在时,才能对文件进行复制,目的文件无影响
    '//sourcefile=s_filename
     if fileexist then
      dim myfile
      if s_filename=s_destinationfile then
       showerr "禁止一相同文件名拷贝到同一目录下:("
      end if
      set myfile = o_fso.getfile(s_filename)
      myfile.copy (s_destinationfile)
      else
      showerr "文件已经不存在"
     end if

    end function

    public function movefile()
     if fileexist then
      dim sourcefile
      if s_filename=s_destinationfile then
       showerr "禁止一相同文件名移动到同一目录下:("
      end if
      o_fso.movefile s_filename,s_destinationfile
     else
      showerr "文件已经不存在:("
     end if

    end function

    '===============================================目录操作

    '设置目录名
    public property let foldername(byval values)
     s_foldername=values
     'checkfolder '检查文件
    end property

    '设置目标目录名
    public property let destinationfolder(byval values)
     s_destinationfolder=values
    end property

    '获得目录大小

    '获得目录创建信息

    '判断目录是否存在

    '创建目录

    '删除目录

    '拷贝目录

    '移动目录

    '重命名目录

    '===============================================遍历操作

    end class

    set myfile=new cls_fso
    myfile.filename=server.mappath("file.txt")
    'myfile.createtxtfile()
    myfile.writetxtfile "test",2
    myfile.destinationfile=server.mappath("file2.txt")
    myfile.copyfile()
    'myfile.filename=server.mappath("file2.txt")
    'myfile.deletefile()
    response.write(myfile.filesize)
    for i =0 to 8
    response.write(myfile.showfileaccessinfo(i) )
    response.write("
    ")
    next

    %>

    相关文章

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    通义千问
    通义千问

    阿里巴巴推出的全能AI助手

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    php中文乱码如何解决
    php中文乱码如何解决

    本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

    1

    2026.01.28

    Java 消息队列与异步架构实战
    Java 消息队列与异步架构实战

    本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

    1

    2026.01.28

    Python 自然语言处理(NLP)基础与实战
    Python 自然语言处理(NLP)基础与实战

    本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

    23

    2026.01.27

    拼多多赚钱的5种方法 拼多多赚钱的5种方法
    拼多多赚钱的5种方法 拼多多赚钱的5种方法

    在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

    120

    2026.01.26

    edge浏览器怎样设置主页 edge浏览器自定义设置教程
    edge浏览器怎样设置主页 edge浏览器自定义设置教程

    在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。

    50

    2026.01.26

    苹果官方查询网站 苹果手机正品激活查询入口
    苹果官方查询网站 苹果手机正品激活查询入口

    苹果官方查询网站主要通过 checkcoverage.apple.com/cn/zh/ 进行,可用于查询序列号(SN)对应的保修状态、激活日期及技术支持服务。此外,查找丢失设备请使用 iCloud.com/find,购买信息与物流可访问 Apple (中国大陆) 订单状态页面。

    191

    2026.01.26

    npd人格什么意思 npd人格有什么特征
    npd人格什么意思 npd人格有什么特征

    NPD(Narcissistic Personality Disorder)即自恋型人格障碍,是一种心理健康问题,特点是极度夸大自我重要性、需要过度赞美与关注,同时极度缺乏共情能力,背后常掩藏着低自尊和不安全感,影响人际关系、工作和生活,通常在青少年时期开始显现,需由专业人士诊断。

    7

    2026.01.26

    windows安全中心怎么关闭 windows安全中心怎么执行操作
    windows安全中心怎么关闭 windows安全中心怎么执行操作

    关闭Windows安全中心(Windows Defender)可通过系统设置暂时关闭,或使用组策略/注册表永久关闭。最简单的方法是:进入设置 > 隐私和安全性 > Windows安全中心 > 病毒和威胁防护 > 管理设置,将实时保护等选项关闭。

    7

    2026.01.26

    2026年春运抢票攻略大全 春运抢票攻略教你三招手【技巧】
    2026年春运抢票攻略大全 春运抢票攻略教你三招手【技巧】

    铁路12306提供起售时间查询、起售提醒、购票预填、候补购票及误购限时免费退票五项服务,并强调官方渠道唯一性与信息安全。

    183

    2026.01.26

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    ASP 教程
    ASP 教程

    共34课时 | 4.1万人学习

    PHP课程
    PHP课程

    共137课时 | 9.9万人学习

    ASP参考手册
    ASP参考手册

    共0课时 | 0人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号