excel_vba_编程教程(完整版)-第48节
按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
CreateObject(〃Scripting。FileSystemObject〃)
Set objFolder = fs。GetFolder(〃C:”)
Workbooks。Add
For Each objFile In objFolder。Files
ActiveCell。Select
Selection。Formula = objFile。Name
ActiveCell。Offset(0; 1) _
。Range(〃A1〃)。Select
Selection。Formula = objFile。Type
ActiveCell。Offset(1; …1) _
。Range(〃A1〃)。Select
Next
Columns(〃A:B〃)。Select
Selectionlumns。AutoFit
End Sub
158
… 页面 175…
GetSpecialFolder 返回操作系统文件夹路径:
0 — 视窗文件夹
1 — 系统文件夹
2 — 临时文件夹
Sub SpecialFolders()
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
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
CopyFolder 复制文件夹
Sub MakeFolderCopy()
Dim fs As FileSystemObject
Set fs = New FileSystemObject
If fs。FolderExists(〃C:TestFolder〃) Then
fspyFolder 〃C:TestFolder〃;
〃C:FinalFolder〃
MsgBox 〃The folder was copied。〃
End If
End Sub
MoveFolder 移动文件夹
DeleteFolder 删除文件夹
Sub RemoveFolder()
Dim fs As FileSystemObject
Set fs = New FileSystemObject
If fs。FolderExists(〃C:TestFolder〃) Then
fs。DeleteFolder 〃C:TestFolder〃
MsgBox 〃The folder was deleted。〃
End If
End Sub
CreateTextFile 创建文本文件
159
… 页面 176…
OpenTextFile 打开文本文件
Sub ReadTextFile()
Dim fs As Object
Dim objFile As Object
Dim strContent As String
Dim strFileName As String
strFileName = 〃C:WINNTSystem。ini〃
Set fs =
CreateObject(〃Scripting。FileSystemObject〃)
Set objFile = fs。OpenTextFile(strFileName)
Do While Not objFile。AtEndOfStream
strContent = strContent & objFile。ReadLine &
vbCrLf
Loop
objFile。Close
Set objFile = Nothing
ActiveWorkbook。Sheets(3)。Select
Range(〃A1〃)。Select
Selection。Formula = strContent
End Sub
对象FileSystemObject只有一个属性,叫做Drives,它返回对硬盘驱动集合的引用。使用该属性,
你可以查看一个电脑硬盘的清单,如下所示:
Sub DrivesList()
Dim fs As Object
Dim colDrives As Object
Dim strDrive As String
Set fs = CreateObject(〃Scripting。FileSystemObject〃)
Set colDrives = fs。Drives
For Each Drive In colDrives
strDrive = 〃Drive 〃 & Drive。DriveLetter & 〃: 〃
Debug。Print strDrive
Next
End Sub
26。对象 File 的属性
对象File允许你访问某特定文件的所有属性,下面的代码创建了对对象File的引用:
Set fs = CreateObject(〃Scripting。FileSystemObject〃)
Set objFile = fs。GetFile(“C:My DocumentsmyFile。doc”)
在本章前面的过程FileInfo里你可以发现使用对象File的例子。
属性 描述
Attributes 返回文件属性(和本章前面介绍的VBA函数GetAttr比较一下)
DateCreated 文件创建日期
DateLastAccessed 最后访问日期
DateLastModified 最后修改日期
Drive 驱动器名称,后面带冒号
Name 文件名
ParentFolder 文件的父文件夹
160
… 页面 177…
Path 文件完整路径
Size 以字节表示的文件大小(与本章前面介绍的VBA函数FileLen比较一
下)
Type 文件类型。这就是显示在Windows文件浏览器类型一列中的文本(例
如,参数设置,应用程序,快捷方式)
27。文件夹对象属性
对象Folder提供了对所有文件夹属性的访问。下面的代码行创建了对对象Folder的引用:
Set fs = CreateObject(〃Scripting。FileSystemObject〃)
Set objFolder = fs。GetFolder(“C:My Documents”)
属性 描述
Attributes 文件夹属性
DateCreated 文件夹创建日期
Drive 包含该文件夹的驱动器(译者:原文为Name of Folder)
Files 文件夹中的文件集合
Sub CountFilesInFolder()
Dim fs; strFolder; objFolder; colFiles
strFolder = InputBox(〃Enter the folder name:〃)
If Not IsFolderEmpty(strFolder) Then
Set fs = CreateObject(〃Scripting。FileSystemObject〃)
Set objFolder = fs。GetFolder(strFolder)
Set colFiles = objFolder。Files
MsgBox 〃The number of files in the folder 〃 & _
strFolder & 〃=〃 & colFilesunt
End If
End Sub
上面的过程调用函数IsFolderEmpty,该函数将在本表中的大小属性
里讨论。
IsRootFolder 如果本文件夹为根文件