一,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.CloseSet 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.DateLastModifiedresponse.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.CloseSet 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.CloseSet 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.CloseSet f2 = fso.GetFile(whichfile)f2.move "d:\"Set f3 = fso.GetFile("d:\cnbruce.txt")f3.delete %><a href="d:\">查看下是没有该文件的</a>
VB基础:认识VB的文件系统对象FSO 出处:CSDN 责任编辑:ljx [04-3-16 15:14] 作者:zhangking
·热点推荐:·腾讯QQ2003 III正式版抢先试用! ·Flash发誓要成为最普及的工具! ·免受侵扰,我用阿达家长控制助手 ·VC学习:获取游戏手柄的按键输入 一周好软回顾:望穿秋水QQ终发布 ·首度曝光,XPSP2 2082 中文版安装图解 ·浅谈木马的加载方式 ·无限无线实战篇 校园网内部机也能出国访问 ·三度空间:微软P2P工具全攻略之安装篇 让WinRAR“不务正业” ·制作一个逼真的垃圾篓 ·全球500强企业网站截图欣赏(五) ·绘制你的个性摄像头 ·3DSMAX高级应用技巧之楼房生长动画 ·强烈推荐!超强超全QQ表情 ·XP酷主题:FFXBOX
在 VB 编程中经常需要和文件系统打交道,比如获取硬盘的剩余空间、判断文件夹或文件是否存在等。在VB 推出文件系统对象(File System Object)以前,完成这些功能需要调用 Windows API 函数或者使用一些比较复杂的过程来实现,使编程复杂、可靠性差又容易出错。使用 Windows 提供的的文件系统对象,一切变得简单多了。以下笔者举出一些编程中比较常用的例子,以函数或过程的形式提供给大家,读者可在编程中直接使用,也可以改进后实现更为强大的功能。
要应用 FSO 对象,须要引用一个名为 Scripting 的类型库,方法是,执行 VB6.0 的菜单项“工程/引用”,添加引用列表框中的“Microsoft Scripting Runtime”一项。然后我们在“对象浏览器”中就可以看到 Scripting 类型库下的众多对象及其方法、属性。
1、判断光驱的盘符: Function GetCDROM() ' 返回光驱的盘符(字母) Dim Fso As New FileSystemObject '创建 FSO 对象的一个实例 Dim FsoDrive As Drive, FsoDrives As Drives '定义驱动器、驱动器集合对象 Set FsoDrives = Fso.Drives For Each FsoDrive In FsoDrives '遍历所有可用的驱动器 If FsoDrive.DriveType = CDRom Then '如果驱动器的类型为 CDrom GetCDROM = FsoDrive.DriveLetter '输出其盘符 Else GetCDROM = "" End If Next Set Fso = Nothing Set FsoDrive = Nothing Set FsoDrives = Nothing End Function
2、判断文件、文件夹是否存在: '返回布尔值:True 存在,False 不存在,filername 文件名 Function FileExist(filename As String) Dim Fso As New FileSystemObject If Fso.FileExists(filename) = True Then FileExist = True Else FileExist = False End If Set Fso = Nothing Set fso = Server.CreateObject("Scripting.FileSystemObject") if fso.FileExists(Server.MapPath(url2)) then Response.Write "存在" else Response.Write "不存在" End If Set fso=nothing |