返回随机生成的临时文件或文件夹的名称,用于执行要求临时文件或文件夹的操作。
object.GetTempName
可选项参数 object 应为 FileSystemObject 对象的名称。
GetTempName 方法不创建文件,该方法仅提供临时文件名。CreateTextFile 使用该临时文件名创建文件。
下面例子举例说明如何使用 GetTempName 方法:
Dim fso, tempfile Set fso = CreateObject("Scripting.FileSystemObject")Function CreateTempFile Dim tfolder, tname, tfile Const TemporaryFolder = 2 Set tfolder = fso.GetSpecialFolder(TemporaryFolder) tname = fso.GetTempName Set tfile = tfolder.CreateTextFile(tname) Set CreateTempFile = tfile End FunctionSet tempfile = CreateTempFile tempfile.WriteLine "世界你好"tempfile.Close
GetAbsolutePathName 方法 | GetBaseName 方法 | GetDrive 方法 | GetDriveName 方法 | GetExtensionName 方法 | GetFile 方法 | GetFileName 方法 | GetFolder 方法 | GetParentFolderName 方法 | GetSpecialFolder 方法
应用于:FileSystemObject 对象
返回一个数组,其中包含有 Dictionary 对象中的所有项目。
object.Items
object 应为 Dictionary 对象的名称。
以下代码举例说明如何使用 Items 方法:
Function DicDemo Dim a,d,I,s'创建一些变量Set d = CreateObject("Scripting.Dictionary")d.Add "a","Athens"'添加键和项目d.Add "b","Belgrade"d.Add "c","Cairo"a = d.Items'获取项目。For i = 0 To d.Count -1'循环使用数组。s = s & a(i) & "<BR>"'创建返回字符串。NextDicDemo = sEnd Function
Add 方法(Dictionary) | Exists 方法 | Keys 方法 | Remove 方法 | RemoveAll 方法
应用于: Dictionary 对象
返回一数组,其中包含有 Dictionary 对象的所有现存键。
object.Keys
object 应为 Dictionary 对象的名称。
下列代码举例说明如何使用 Keys 方法:
Function DicDemo Dim a,d,i'创建一些变量。Set d = CreateObject("Scripting.Dictionary")d.Add "a","Athens"'添加键和项目。d.Add "b","Belgrade"d.Add "c","Cairo"a = d.Keys'获取键。For i = 0 To d.Count -1'循环使用数组。s = s & a(i) & "<BR>"'返回结果。NextDicDemo = sEnd Function
Add (Dictionary)方法 | Exists 方法 | Items 方法 | Remove 方法 | RemoveAll 方法
应用于: Dictionary 对象
将指定的文件或文件夹从某位置移动到另一位置。
object.Move destination
object
必选项。应为 File 或 Folder 对象的名称。
destination
必选项。目标位置。表示要将文件或文件夹移动到该位置。不允许使用通配符。
对 File 或 Folder 应用 Move 方法的结果与使用 FileSystemObject.MoveFile 或 FileSystemObject.MoveFolder 执行的操作完全相同。然而,要注意的是 FileSystemObject.MoveFile 或 FileSystemObject.MoveFolder 方法可移动多个文件或文件夹。
下面例子举例说明如何使用 Move 方法:
Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile("c:\testfile.txt", True) MyFile.WriteLine(“这是一个测试。”)MyFile.CloseSet MyFile = fso.GetFile("c:\testfile.txt")MyFile.Move "c:\windows\desktop\"
Copy 方法 | Delete 方法 | MoveFile 方法 | MoveFolder 方法 | OpenAsTextStream 方法
应用于: File 对象 | Folder 对象
将一个或多个文件从某位置移动到另一位置。
object.MoveFile source, destination
object
必选项。应为 FileSystemObject 的名称。
source
必选项。要移动的文件的路径。source 参数字符串仅可在路径的最后一个组成部分中用通配符。
destination
必选项。指定路径,表示要将文件移动到该目标位置。destination 参数不能包含通配符。
如果 source 包含通配符或 destination 以路径分隔符 (\) 结束,则假定 destination 指定现有文件夹,将匹配文件移动到该文件夹中。否则,假定 destination 是要创建的目标文件。在任一种情况下,移动单个文件时,可能出现以下三种情况:
如果在 source 使用通配符,但没有匹配文件时,也会出现错误。MoveFile 方法在遇到出现的第一个错误时停止。该方法不会撤消错误发生前所作的任何更改。
下面例子举例说明如何使用 MoveFile 方法:
Sub MoveAFile(Drivespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile Drivespec, "c:\windows\desktop\"
End Sub
注意 GetParentFolderName方法只有在已知的路径上起作用,它不能建立路径,也不能确定指定路径是否存在。
CopyFile 方法 | DeleteFile 方法 | GetFile 方法 | GetFileName 方法 | Move 方法 | MoveFolder 方法 | OpenTextFile 方法
应用于: FileSystemObject 对象
将一个或多个文件夹从某位置移动到另一位置。
object.MoveFolder source, destination
object
必选项。应为 FileSystemObject 的名称。
source
必选项。要移动的文件夹的路径。source 参数字符串仅可在路径的最后一个组成部分中包含通配符。
destination
必选项。指定路径,表示要将文件夹移动到该目标位置。destination 参数不能包含通配符。
如果 source 包含通配符或 destination 以路径分隔符 (\) 结束,则假定 destination 指定现有文件夹,将匹配文件移动到该文件夹中。否则,假定 destination 是要创建的目标文件夹。在任一种情况下,移动单个文件夹时,可能会发生以下三种情况:
如果 source 使用通配符,但没有匹配文件夹时,则会出现错误。MoveFolder 方法在遇到出现的第一个错误时停止。该方法不会撤消错误发生前所作的任何更改。
下面例子举例说明如何使用 MoveFolder 方法:
Sub MoveAFolder(Drivespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFolder Drivespec, "c:\windows\desktop\"
End Sub
要点 只有当操作系统支持时,此方法才允许在两个卷之间移动文件夹。
CopyFolder 方法 | CreateFolder 方法 | DeleteFolder 方法 | GetFolder 方法 | GetParentFolderName 方法 | Move 方法 | MoveFile 方法
应用于: FileSystemObject 对象
打开指定的文件并返回一个 TextStream 对象,此对象用于对文件进行读、写或追加操作。
object.OpenAsTextStream([iomode, [format]])
object
必选项。应为 File 对象的名称。
iomode
可选项。输入/输出模式,是下列三个常数之一:ForReading、ForWriting 或 ForAppending。
format
可选项。三个 Tristate 值之一,指出以何种格式打开文件。忽略此参数,则文件以 ASCII 格式打开。
iomode 参数可为下列设置之一:
| 常数 | 值 | 描述 |
|---|---|---|
| ForReading | 1 | 以只读模式打开文件。不能对此文件进行写操作。 |
| ForWriting | 2 | 以可读写模式打开文件。如果已存在同名的文件,则覆盖旧的文件。 |
| ForAppending | 8 | 打开文件并在文件末尾进行写操作。 |
format 参数可为下列设置之一:
| 常数 | 值 | 描述 |
|---|---|---|
| TristateUseDefault | -2 | 以系统默认格式打开文件。 |
| TristateTrue | -1 | 以 Unicode 格式打开文件。 |
| TristateFalse | 0 | 以 ASCII 格式打开文件。 |
OpenAsTextStream 方法可提供与 FileSystemObject 对象的 OpenTextFile 方法相同的功能。另外,使用 OpenAsTextStream 方法可对文件进行写操作。
以下代码举例说明如何使用 OpenAsTextStream 方法:
Function TextStreamTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, f, ts Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateTextFile "test1.txt" '创建一个文件。Set f = fso.GetFile("test1.txt")Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)ts.Write "嗨,你好!"ts.CloseSet ts = f.OpenAsTextStream(ForReading, TristateUseDefault)TextStreamTest = ts.ReadLinets.CloseEnd Function
Copy 方法 | CreateTextFile 方法 | Delete 方法 | Move 方法 | OpenTextFile 方法
应用于: File 对象
打开指定的文件并返回一个 TextStream 对象,可以读取、写入此对象或将其追加到文件。
object.OpenTextFile(filename[, iomode[, create[, format]]])
object
必选项。应为 FileSystemObject 对象的名称。
filename
必选项。字符串表达式,指明要打开的文件名称。
iomode
可选项。输入/输出模式,是下列三个常数之一:ForReading,ForWriting,或 ForAppending。
create
可选项。Boolean 值,指出当指定的 filename 不存在时是否能够创建新文件。允许创建新文件时为 True,否则为 False。默认值为 False。
format
可选项。三个 Tristate 值之一,指出以何种格式打开文件。若忽略此参数,则文件以 ASCII 格式打开。
iomode 参数可为下列设置之一:
| 常数 | 值 | 描述 |
|---|---|---|
| ForReading | 1 | 以只读模式打开文件。不能对此文件进行写操作。 |
| ForWriting | 2 | 以只写方式打开文件。不能对此文件进行读操作。 |
| ForAppending | 8 | 打开文件并在文件末尾进行写操作。 |
〈P〉 format 参数可为下列设置之一:
| 常数 | 值 | 描述 |
|---|---|---|
| TristateUseDefault | -2 | 以系统默认格式打开文件。 |
| TristateTrue | -1 | 以 Unicode 格式打开文件。 |
| TristateFalse | 0 | 以 ASCII 格式打开文件。 |
以下代码举例说明如何使用 OpenTextFile 方法打开写文件:
Sub OpenTextFileTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:\testfile.txt", For Writing, True) f.Write "嗨,你好!"f.CloseEnd Sub
OpenAsTextStream 方法 | CreateTextFile 方法
应用于: FileSystemObject 对象
从 TextStream 文件中读入指定数目的字符并返回结果字符串。
object.Read(characters)
object
必选项。应为 TextStream 对象的名称。
characters
必选项。要从文件读的字符数目。
下面例子举例说明如何使用 Read 方法从文件中读取五个字符并返回字符串结果:
Function ReadTextFileTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f, Msg Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True) f.Write "世界你好!"Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)ReadTextFileTest = f.Read(5)End Function
Close 方法 | ReadAll 方法 | ReadLine 方法 | Skip 方法 | SkipLine 方法 | Write 方法 | WriteLine 方法 | WriteBlankLines 方法
应用于: TextStream 对象
读入全部 TextStream 文件并返回结果字符串。
object.ReadAll
object 应为 TextStream 对象的名称。
对于大文件,使用 ReadAll 方法浪费内存资源。应该使用其他技术输入文件,例如按行读文件。
Function ReadAllTextFile Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True) f.Write "世界你好!"Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)ReadAllTextFile = f.ReadAllEnd Function
Close 方法 | Read 方法 | ReadLine 方法 | Skip 方法 | SkipLine 方法 | Write 方法 | WriteLine 方法 | WriteBlankLines 方法
应用于: TextStream 对象
从 TextStream 文件中读入一整行字符(直到下一行,但不包括下一行字符),并返回结果字符串。
object.ReadLine
object 应为 TextStream 对象的名称。
下面例子说明如何使用 ReadLine 方法从 TextStream 文件中读取字符并返回字符串:
Function ReadLineTextFile Const ForReading = 1, ForWriting = 2 Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForWriting, True) MyFile.WriteLine "Hello world!" MyFile.WriteLine "The quick brown fox" MyFile.Close Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading) ReadLineTextFile = MyFile.ReadLine' Returns "Hello world!"End Function
Close 方法 | Read 方法 | ReadAll 方法 | Skip 方法 | SkipLine 方法 | Write 方法 | WriteLine 方法 | WriteBlankLines 方法
应用于: TextStream 对象
从 Dictionary 对象中删除键和项目对。
object.Remove(key)
object
必选项。应为 Dictionary 对象的名称。
key
必选项。与要从 Dictionary 对象中删除的键和项目对相关联的 Key。
如果指定的键和项目对不存在,则会出现错误。
下列代码示范如何使用 Remove 方法:
Dim a, d'创建变量。Set d = CreateObject("Scripting.Dictionary")d.Add "a", "Athens"'添加键和项目。d.Add "b", "Belgrade"d.Add "c", "Cairo"...a = d.Remove("b")'删除第二个项目对
Add 方法(Dictionary) | Exists 方法 | Items 方法 | Keys 方法 | RemoveAll 方法
应用于: Dictionary 对象