CreateTextFile Method
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.
object.CreateTextFile(filename[, overwrite[, unicode]])
The following code illustrates how to use the CreateTextFile method to create and open a text file.
var fso = new ActiveXObject("Scripting.FileSystemObject"); var a = fso.CreateTextFile("c:\\testfile.txt", true); a.WriteLine("This is a test."); a.Close();
Sub CreateAfile
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
End Sub
If the overwrite argument is false, or is not provided, for a filename that already exists, an error occurs.
Applies To:
Overwrite
I have also found the Boolean parameter to overwrite files does not work as documented.
Set protectedFiles = fso.CreateTextFile(sSavePath & "\logfile.txt", True) gave an error saying the file already existed. Shouldn't true allow the file to be overwritten?
Set protectedFiles = fso.CreateTextFile(sSavePath & "\logfile.txt", True) gave an error saying the file already existed. Shouldn't true allow the file to be overwritten?
- 7/27/2011
- csc077
Omitting of overwrite option
I have just tested this and found when I omitted the overwrite option the text file I created was infact overwritten which is oposite to what the text re this option states.
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objOutputFile = objFSO.CreatetextFile("c:\fred.txt")
Text from argument description
overwrite
Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.
If you use the code above you will find the text file is overwritten without warning.
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objOutputFile = objFSO.CreatetextFile("c:\fred.txt")
Text from argument description
overwrite
Optional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.
If you use the code above you will find the text file is overwritten without warning.
- 5/3/2011
- CLANMAN