FileSystemObject的TextStream物件 - 小瓜瓜
[首頁]
●可由GetFile來取得File物件:
Private Sub Command1_Click() -
Open "c:\test.txt" For Output As #1 - Print #1, "test"
Close #1 - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
End Sub |
●File.Attributes屬性:傳回或設定檔案的屬性,MSDN上的說明表如下
Normal | 0 | Normal file. No attributes are set. |
ReadOnly | 1 | Read-only file. Attribute is read/write. |
Hidden | 2 | Hidden file. Attribute is read/write. |
System | 4 | System file. Attribute is read/write. |
Volume | 8 | Disk drive volume label. Attribute is read-only. |
Directory | 16 | Folder or directory. Attribute is read-only. |
Archive | 32 | File has changed since last backup. Attribute is read/write. |
Alias | 1024 | Link or shortcut. Attribute is read-only. |
Compressed | 2048 | Compressed file. Attribute is read-only. |
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
-
If (F.Attributes And 1) = 1 Then - MsgBox "這是唯讀檔"
-
Else - MsgBox "這不是唯讀檔"
End If End Sub |
●File.Copy方法:複製檔案
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- F.Copy "c:\test.bak"
End Sub |
●File.DateCreated屬性:檔案建立日期
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox "建立日期:" & F.DateCreated
End Sub |
●File.DateLastAccessed屬性:檔案最後存取日期
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox "最後存取日期:" & F.DateLastAccessed
End Sub |
●File.DateLastModified屬性:檔案最後修改
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox "最後修改日期:" & F.DateLastModified
End Sub |
●File.Delete方法:刪除檔案
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.bak")
- F.Delete
End Sub |
●File.Drive屬性:檔案所在的磁碟位置
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox "本檔案在" & F.Drive & "磁碟機"
End Sub |
●File.Move方法:移動檔案或改檔名
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- F.Move "c:\temp\" '最後一個字元是斜線為移動位置
- F.Move "c:\temp" '最後一個字元不是斜線為修改檔名
End Sub |
●File.Name屬性:傳回檔名
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox "本檔案是" & F.Name
End Sub |
●File.ParentFolder屬性:取得所在的資料夾
●File.ParentPath屬性:取得完整的路徑
●File.ShortName屬性:取得短檔名
●File.ShortPath屬性:取得短檔名路徑
●File.Size屬性:傳回檔案大小
Private Sub Command1_Click() - Dim FS As Object
- Dim F As Object
- Set FS = CreateObject("Scripting.FileSystemObject")
- Set F = FS.GetFile("c:\test.txt")
- MsgBox F.Size & "Byte(s)"
End Sub |
●File.Type屬性:傳回檔案類型