Creating Files Step-by-Step
上一篇 /
下一篇 2010-03-31 00:29:30
/ 个人分类:VBScript
Creating Files
Option Explicit
On Error Resume Next
Dim LogFile 'holds path to the log file
Dim objFSO 'holds connection to the FileSystemObject
Dim objFile 'used by OpenTextFile command to allow writing to file
LogFile = "C:\FSO\fso.txt"
Const ForWriting = 2
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set bjFile = objFSO.OpenTextFile(LogFile,ForWriting)
objFile.WriteLine "beginning logging " & Now
objFile.WriteLine "working on process " & Now
objFile.WriteLine "Logging completed at " & Now
objFile.Close
Creating a Log File
Chapter 6 Quick Reference
To |
Do This |
Write to a file |
Choose either the Write, WriteLine, or WriteBlankLines methods |
Include a carriage return and a line feed when you write to a line |
Use the WriteLine method |
Append to a line when you write to it |
Use the Write method |
Verify the existence of a file prior to writing to it |
Use the FileExists method |
Read file attributes |
Use the Attribute property of a File object |
Obtain a list of all files in a folder |
Use the Files method once you have connected to a folder |
Connect to a folder |
Use the GetFolder method |
Work with a single file from a collection of files |
Iterate through the collection of files by using a For Each...Next loop |
Option Explicit
On Error Resume Next
Dim LogFile 'holds path to the log file
Dim objFSO 'holds connection to the FileSystemObject
Dim objFile 'used by OpenTextFile command to allow writing to file
LogFile = "C:\FSO\fso.txt"
Const ForWriting = 2
Const ForAppending = 8
Set bjFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(LogFile) Then
Set bjFile = objFSO.OpenTextFile(LogFile,ForAppending)
objFile.Write "appending " & Now
Else
Set bjFile = objFSO.CreateTextFile(LogFile)
objFile.write "writing to new file " & Now
End If
objFile.Close
收藏
举报
TAG: