当前位置:首页 > ASP教程 > ASP基础

ASP编程入门进阶(十八):FSO组件之文件操作(下)

一,fso.getfile
提取文件相应的 file 对象

1,getfile.asp


<%
whichfile=server.mappath("cnbruce.txt")
set fso = createobject("scripting.filesystemobject")
set f1 = fso.createtextfile(whichfile,true)
f1.write ("this is a test.my name is cnbruce.")
f1.close
set f2 = fso.getfile(whichfile)

s = "文件名称:" & f2.name & "<br>"
s = s & "文件短路径名:" & f2.shortpath & "<br>"
s = s & "文件物理地址:" & f2.path & "<br>"
s = s & "文件属性:" & f2.attributes & "<br>"
s = s & "文件大小: " & f2.size & "<br>"
s = s & "文件类型: " & f2.type & "<br>"
s = s & "文件创建时间: " & f2.datecreated & "<br>"
s = s & "最近访问时间: " & f2.datelastaccessed & "<br>"
s = s & "最近修改时间: " & f2.datelastmodified
response.write(s)
%>



其效果正如右键某文件,看到的具体属性信息。
其中attributes返回的数值“32”表示:(archive)上次备份后已更改的文件。可读写。

其它值附录如下:


normal 0 普通文件。 没有设置任何属性。
readonly 1 只读文件。 可读写。
hidden 2 隐藏文件。 可读写。
system 4 系统文件。 可读写。
directory 16 文件夹或目录。 只读。
archive 32 上次备份后已更改的文件。 可读写。
alias 1024 链接或快捷方式。 只读。
compressed 2048 压缩文件。 只读。



二,file.move
作用将指定的文件或文件夹从某位置移动到另一位置。其实该方法仍然属于fso.getfile后的一个应用。

2,movefile.asp


<%
whichfile=server.mappath("cnbruce.txt")
set fso = createobject("scripting.filesystemobject")
set f1 = fso.createtextfile(whichfile,true)
f1.write ("this is a test.my name is cnbruce.")
f1.close
set f2 = fso.getfile(whichfile)
f2.move "c:"
%>
<a href="c:">查看下有没有</a>



简单的剪切粘贴的功能实现。

三,file.copy
同样属于fso.getfile后的一个应用。就只是单纯地拷贝文件到某位置。

3,copyfile.asp


<%
whichfile=server.mappath("cnbruce.txt")
set fso = createobject("scripting.filesystemobject")
set f1 = fso.createtextfile(whichfile,true)
f1.write ("this is a test.my name is cnbruce.")
f1.close
set f2 = fso.getfile(whichfile)
f2.copy "d:"
%>
<a href="d:">查看下有没有</a>



和本asp页面同在目录下的cnbruce.txt文件依然存在。

四,file.delete
很显然,就是直接删除文件了。

4,delfile.asp


<%
whichfile=server.mappath("cnbruce.txt")
set fso = createobject("scripting.filesystemobject")
set f1 = fso.createtextfile(whichfile,true)
f1.write ("this is a test.my name is cnbruce.")
f1.close
set f2 = fso.getfile(whichfile)
f2.move "d:"
set f3 = fso.getfile("d:cnbruce.txt")
f3.delete
%>
<a href="d:">查看下是没有该文件的</a>



当然fso还没有结束,比如上传文件,asp转html等都需要用到fso。更精彩的依然是在后面。

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!