您的位置:首页 > 编程语言 > VB

VBS基础篇 - 对象(5) - File对象

2014-08-26 15:18 495 查看
描述:提供对文件所有属性的访问,从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 必选项。应为 FileFolder 对象的名称

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,MyFile

Dim
Attributes

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'Attributes属相,获取文件属性

Attributes= MyFile.Attributes

Msgbox Attributes

'设置c:\test.txt的属性为只读并显示出来

MyFile.Attributes= 1 

Attributes= MyFile.Attributes

Msgbox Attributes


  DateCreated

描述:返回指定的文件或文件夹的创建日期和时间

语法:object.DateCreated

参数:object 应为 File 或 Folder 对象的名称

示例:显示c:\test.txt的创建时间  

?
1
2
3
4
5
6
7
8
9
Dim
Fso,MyFile

Dim
DateCreated

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'DateCreated属性,获取文件创建的时间

DateCreated= MyFile.DateCreated

MsgBox DateCreated


  DateLastAccessed

描述:返回指定的文件或文件夹的上次访问日期和时间

语法:object. DateLastAccessed

参数:object 应为 File 或 Folder 对象的名称

示例:显示c:\test.txt的上次访问时间  

?
1
2
3
4
5
6
7
8
9
Dim
Fso,MyFile

Dim
DateLastAccessed

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'DateLastAccessed属性,获取文件上一次访问的时间

DateLastAccessed= MyFile.DateLastAccessed

MsgBox DateLastAccessed


  DateLastModified

描述:返回指定的文件或文件夹的上次修改日期和时间

语法:object. DateLastModified

参数:object 应为 File 或 Folder 对象的名称

示例:显示c:\test.txt的创建时间  

?
1
2
3
4
5
6
7
8
9
Dim
Fso,MyFile

Dim
DateLastModified

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'DateLastModified属性,获取文件上一次修改的时间

DateLastModified= MyFile.DateLastModified

MsgBox 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,MyFile

Dim
name

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'name属性,获取文件名称

name= MyFile.name

MsgBox name

'修改文件名

MyFile.name= 
"test1.txt"

name= MyFile.name

MsgBox name


  ParentFolder

描述:返回指定文件或文件夹的父文件夹

语法:object.ParentFolder

参数:object 应为 File 或 Folder 对象的名称

示例:显示c:\test.txt所在文件夹名称  

?
1
2
3
4
5
6
7
8
9
Dim
Fso,MyFile

Dim
parentfolder

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"c:\test.txt"
)

'parentfolder属性,获取文件父文件夹

parentfolder= MyFile.parentfolder

MsgBox 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,MyFile

Dim
ShortName

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"C:\Program Files\Internet Explorer\JSProfilerCore.dll"
)

'ShortName属性,获取文件的ShortName

ShortName= MyFile.ShortName

MsgBox 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,MyFile

Dim
ShortPath

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"C:\Program Files\Internet Explorer\JSProfilerCore.dll"
)

'ShortPath属性,获取文件的ShortPath

ShortPath= MyFile.ShortPath

MsgBox ShortPath


  Size

描述:返回指定文件的字节数

语法:object. Size

参数:object 应为 File 或 Folder 对象的名称

示例:获取C:\test.txt文件大小  

?
1
2
3
4
5
6
7
8
9
10
Dim
Fso,MyFile

Dim
size

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"C:\test.txt"
)

'size属性,获取文件的大小

size= MyFile.Size 

size= 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,MyFile

Dim
MyType

'创建FileSystemObject对象

Set
Fso = CreateObject(
"Scripting.FileSystemObject"
)

'使用GetFile方法获取文件的句柄

Set
MyFile = Fso.GetFile(
"C:\test.txt"
)

'Type属性,获取文件的类型

MyType= MyFile.Type 

MsgBox MyType


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: