vb--操作方法
0 Reply , Posted in 程序代码 on 2017 28, 2017
1.add
Sub AddNewFolder(path, folderName)
Dim fso, f, fc, nf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(path)
Set fc = f.SubFolders
If folderName <> "" Then
Set nf = fc.Add(folderName)
Else
Set nf = fc.Add("New Folder")
End If
End Sub
2.BuildPath
Function GetBuildPath(path)
Dim fso, newpath
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.BuildPath(path, "Sub Folder")
GetBuildPath = newpath
End Function
3.Close
Sub CreateAFile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("这是一个测试。")
MyFile.Close
End Sub
4.copy
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("这是一个测试")
MyFile.Close
Set MyFile = fso.GetFile("c:\testfile.txt")
MyFile.Copy ("c:\windows\desktop\test2.txt")
5.exits
Function KeyExistsDemo
Dim d, msg '创建一些变量。
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" '添加一些键和项目。
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
If d.Exists("c") Then
msg = "指定的键存在。"
Else
msg = "指定的键不存在。"
End If
KeyExistsDemo = msg
End Function
6. 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>" '返回结果。
Next
DicDemo = s
End Function
6.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.ReadAll
End Function