描述:提供对文件所有属性的访问,从FSO对象的GetFile方法获得。
使用File对象
要用File对象模型来编程必须先用FileSystemObject(FSO)对象的GetFile方法获取文件的句柄
1、使用 CreateObject 方法来创建 FileSystemObject 对象
2、使用GetFile方法获取文件的句柄
3、在创建的File对象上使用适当的方法
4、访问对象的属性
方法:(仅常用的方法)
Copy方法
描述:将指定的文件从某位置复制到另一位置。
语法:object.Copy(destination[, overwrite])
参数:object,必选项。应为 File对象的名称。
destination,必选项。复制文件的目标位置。不允许使用通配符。
overwrite,可选项。Boolean值。如果覆盖现有文件,则为True(默认),否则为 False。
示例:复制c:\testfile.txt这个文件到D盘
|
1
2
3
4
5
6
7
8
|
Dim Fso,MyFile‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘Copy方法,将指定的文件复制到指定的存在的位置MyFile.Copy("D:\") ‘覆盖存在的文件MyFile.Copy "D:\",False ‘不覆盖存在的文件 |
Delete方法
描述:删除指定的文件
语法:object.Delete force
参数:object必选项。应为 File对象的名称。
force可选项。Boolean 值。如果要删除的文件的属性设置为只读属性,则该值为 True;否则为 False(默认)。
示例:删除c:\testfile.txt这个文件
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFile‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘Delete方法,删除指定的文件MyFile.Delete‘True 删除只读文件MyFile.Delete True |
Move方法
描述:将指定的文件从某位置移动到另一位置。
语法:object.Move(destination)
参数:object必选项。应为 File对象的名称。
destination必选项。目标位置。表示要将文件移动到该位置。不允许使用通配符。
示例:移动c:\testfile.txt到d盘
|
1
2
3
4
5
6
7
8
|
Dim Fso,MyFile‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘Move方法,移动指定的文件MyFile.Move("D:\") 属性:(仅常用的属性) |
Attributes
描述:设置或返回文件的属性,可读写或只读(与属性有关)
语法:object.Attributes [= newattributes]
参数:object 必选项。应为 File 或 Folder 对象的名称
newattributes可选项。如果指定参数,则 newattributes 为指定的 object 的属性的新值。
newattributes 参数可为下列设置之一或下列设置的合理组合:
Normal 0 普通文件。没有设置任何属性。
ReadOnly 1 只读文件。可读写。
Hidden 2 隐藏文件。可读写。
System 4 系统文件。可读写。
Directory 16 文件夹或目录。只读。
Archive 32 上次备份后已更改的文件。可读写。
Alias 1024 链接或快捷方式。只读。
Compressed 2048 压缩文件。只读。
说明:忽略对只读属性(别名,压缩或目录)所作的改变。当设置属性时,应首先阅读当前属性,然后按要求改变个别属性,最后反写属性.
示例:获取c:\test.txt的属性,设置c:\test.txt的属性为只读并显示出来
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Dim Fso,MyFileDim Attributes‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘Attributes属相,获取文件属性Attributes = MyFile.AttributesMsgbox Attributes‘设置c:\test.txt的属性为只读并显示出来MyFile.Attributes = 1Attributes = MyFile.AttributesMsgbox Attributes |
DateCreated
描述:返回指定的文件或文件夹的创建日期和时间
语法:object.DateCreated
参数:object 应为 File 或 Folder 对象的名称
示例:显示c:\test.txt的创建时间
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim DateCreated‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘DateCreated属性,获取文件创建的时间DateCreated = MyFile.DateCreatedMsgBox DateCreated |
DateLastAccessed
描述:返回指定的文件或文件夹的上次访问日期和时间
语法:object. DateLastAccessed
参数:object 应为 File 或 Folder 对象的名称
示例:显示c:\test.txt的上次访问时间
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim DateLastAccessed‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘DateLastAccessed属性,获取文件上一次访问的时间DateLastAccessed = MyFile.DateLastAccessedMsgBox DateLastAccessed |
DateLastModified
描述:返回指定的文件或文件夹的上次修改日期和时间
语法:object. DateLastModified
参数:object 应为 File 或 Folder 对象的名称
示例:显示c:\test.txt的创建时间
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim DateLastModified‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘DateLastModified属性,获取文件上一次修改的时间DateLastModified = MyFile.DateLastModifiedMsgBox DateLastModified |
Name
描述:设置或返回指定的文件或文件夹的名称,可读写
语法:object. Name [= newname]
参数:object必选项。应为 File 或 Folder 对象的名称
newname可选项。如果提供此参数,则指定的 object 名称更新为 newname
示例:显示c:\test.txt的名字,且修改文件名再显示文件名
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Dim Fso,MyFileDim name‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘name属性,获取文件名称name = MyFile.nameMsgBox name‘修改文件名MyFile.name = "test1.txt"name = MyFile.nameMsgBox name |
ParentFolder
描述:返回指定文件或文件夹的父文件夹
语法:object.ParentFolder
参数:object 应为 File 或 Folder 对象的名称
示例:显示c:\test.txt所在文件夹名称
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim parentfolder‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("c:\test.txt")‘parentfolder属性,获取文件父文件夹parentfolder = MyFile.parentfolderMsgBox parentfolder |
ShortName
描述:返回按照早期 8.3 文件命名约定转换的短文件名
语法:object.ShortName
参数:object 应为 File 或 Folder 对象的名称
示例:获取” C:\Program Files\Internet Explorer\JSProfilerCore.dll”的ShortName
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim ShortName‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("C:\Program Files\Internet Explorer\JSProfilerCore.dll")‘ShortName属性,获取文件的ShortNameShortName = MyFile.ShortNameMsgBox ShortName |
ShortPath
描述:返回按照 8.3 命名约定转换的短路径名
语法:object. ShortPath
参数:object 应为 File 或 Folder 对象的名称
示例:获取” C:\Program Files\Internet Explorer\JSProfilerCore.dll”的ShortPath
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim ShortPath‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("C:\Program Files\Internet Explorer\JSProfilerCore.dll")‘ShortPath属性,获取文件的ShortPathShortPath = MyFile.ShortPathMsgBox ShortPath |
Size
描述:返回指定文件的字节数
语法:object. Size
参数:object 应为 File 或 Folder 对象的名称
示例:获取C:\test.txt文件大小
|
1
2
3
4
5
6
7
8
9
10
|
Dim Fso,MyFileDim size‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("C:\test.txt")‘size属性,获取文件的大小size = MyFile.Sizesize = size/1024&"KB"MsgBox size |
Type
描述:返回文件或文件夹的类型信息
语法:object. Type
参数:object 应为 File 或 Folder 对象的名称。
示例:获取C:\test.txt的文件类型
|
1
2
3
4
5
6
7
8
9
|
Dim Fso,MyFileDim MyType‘创建FileSystemObject对象Set Fso = CreateObject("Scripting.FileSystemObject")‘使用GetFile方法获取文件的句柄Set MyFile = Fso.GetFile("C:\test.txt")‘Type属性,获取文件的类型MyType = MyFile.TypeMsgBox MyType |
VBS基础篇 - 对象(5) - File对象,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/jinjiangongzuoshi/p/3834663.html