FileSystemObject的方法和属性
1,FileExists : 如果文件存在就返回TRUE
Sub FileExists()
Dim fs As Object
Dim strFile As String
Set fs = CreateObject("scripting.filesystemobject")
strFile = InputBox("Enter the full name of the file:")
If fs.FileExists(strFile) Then
MsgBoxstrFile & " was found."
Else
MsgBox "Filedoes not exist."
End If
End Sub
2,GetFile :返回一对象File
3,GetFileName :返回带路径文件名
4,GetFileVersion : 返回文件版本
5,CopyFile : 复制文件
Sub CopyFile()
Dim fs As Object
Dim strFile As String
Dim strNewFile As String
strFile = "c:hello.doc"
strNewFile = "C:programs fileshello.doc"
Set fs = CreateObject("Scripting.filesystemobject")
fs.CopyFile strFile, strNewFile
MsgBox "A copy of the specified file was created."
Set fs = Nothing
End Sub
6,MoveFile :移动文件
7,DeleteFile : 删除文件
Sub DeleteFile()
Dim fs As FileSystemObject
Set fs = New FileSystemObject
fs.DeleteFile "C:programs fileshello.doc"
MsgBox "The requested file was deleted."
End Sub
8,DriveExists : 如果给定硬盘存在则返回TRUE
Function DriveExists(disk)
Dim fs As Object
Dim strMsg As String
Set fs = CreateObject("scripting.filesystemobject")
If fs.DriveExists(disk) Then
strMsg ="drive [" & UCase(disk) & "]exists."
Else
strMsg ="drive [" & UCase(disk) & "] wasnot found."
End If
DriveExists = strMsg
'run this function from the worksheet
'by enter in any cell the following: =driveexists("e:")
End Function
9,GetFolder : 返回对象Folder
Sub FilesInFolder()
Dim fs As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
i = 1
Set fs = CreateObject("scripting.filesystemobject")
Set objFolder = fs.GetFolder("C:")
Range("A1").Select
With Selection
For EachobjFile In objFolder.Files
.Offset(i, 0).Value = objFile.Name
.Offset(i, 1).Value = objFile.Type
i = i + 1
NextobjFile
End With
End Sub
10,GetSpecialFolder : 返回操作系统文件夹路径
0-视窗文件夹
1-系统文件夹
2-临时文件夹
SubSpecialFolders()
Dim fs As Object
Dim strWindowsFolder As String
Dim strSystemFolder As String
Dim strTempFolder As String
Set fs = CreateObject("scripting.filesystemobject")
strWindowsFolder = fs.GetSpecialFolder(0)
strSystemFolder = fs.GetSpecialFolder(1)
strTempFolder = fs.GetSpecialFolder(2)
MsgBox strWindowsFolder & vbCrLf &_
strSystemFolder & vbCrLf & _
strTempFolder, vbInformation + vbOKOnly, _
"special folders"
End Sub
11,CreateFolder : 创建文件夹
Sub MakeNewFolder()
Dim fs, objFolder
Set fs = CreateObject("scripting.filesystemobject")
Set objFolder = fs.CreateFolder("c:testfolder")
MsgBox " A new folder named “" & objFolder.Name& "” was created."
End Sub
12,CopyFolder : 复制文件夹
Sub MakeFolderCopy()
Dim fs As FileSystemObject
Set fs = New FileSystemObject
If fs.FolderExists("c:testfolder") Then
fs.CopyFolder "c:testfolder", "c:finalfolder"
MsgBox "The Folder was copied!"
End If
End Sub
13,MoveFolder :移动文件夹
14,DeleteFolder : 删除文件夹
Sub RemoveFolder()
Dim fs As FileSystemObject
Set fs = New FileSystemObject
If fs.FolderExists("c:testfolder") Then
fs.DeleteFolder "c:testfolder"
MsgBox "TheFolder was deleted."
End If
End Sub
15,CreateTextFile : 创建文本文件
16,OpenTextFile : 打开文本文件
Sub ReadTextFile()
Dim fs As Object
Dim objFile As Object
Dim strContent AsString'定义文件内容
Dim strFileName As String
Dim i As Integer
i = 1
strFileName = "C:Windowswin.ini"
Set fs = CreateObject("scripting.filesystemobject")
Set objFile = fs.OpenTextFile(strFileName)
Do While Not objFile.AtEndOfStream
'******分行列出文件内容******
strContent =objFile.ReadLine
Range("a"& i) = strContent
i = i +1
'******读取全部内容不分行******
' strContent= strContent & objFile.ReadLine &vbCrLf
Loop
objFile.Close
Set objFile = Nothing
End Sub