Cria um nome de arquivo especificado e retorna um objeto TextStream que pode ser usado para ler ou gravar no arquivo.
Sintaxe
object.CreateTextFile(filename[, overwrite[, unicode]])
Argumentos
FileSystemObject.Unicode: Opcional. Valor booliano que indica se o arquivo foi criado como um arquivo Unicode ou ASCII. O valor será verdadeiro se o arquivo for criado como um arquivo Unicode e será falso se for criado como um arquivo ASCII.
Nota:
O argumento Unicode está inativo e é mantido para imitar a sintaxe do VB Script. Ele não tem efeito. Todos os arquivos são criados no formato Unicode.
Comentários
O código a seguir ilustra como usar o método CreateTextFile para criar e abrir um arquivo de texto.
Exemplo 1:
Sub CreateAfile
Dim fso, tso
Set fso = CreateObject("Scripting.FileSystemObject")
Set tso = fso.CreateTextFile("c:\testfile.txt", True)
tso.Close
End Sub
CreateAfile
Se o argumento overwrite for falso, para um nome de arquivo que já existe, ocorrerá um erro.
Exemplo 2: CreateNewFile
Sub CreateNewFile()
Dim fso, tso
Set fso = CreateObject("Scripting.FileSystemObject")
Set tso = fso.CreateTextFile("C:\example1.txt", True) ' Overwrites if the file exists.
tso.WriteLine("This is a new text file.") ' Write some text to the file.
tso.Close ' Close the file.
Set fso = Nothing
End Sub
CreateNewFile
Exemplo 3:
Sub CreateFileNoOverwrite ()
Dim fso, tso
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set tso = fso.CreateTextFile("C:\Temp\testfile.txt", False) ' Do not overwrite if the file exists.
myerr = Err.Number
If myerr <> 0 Then
‘Error handling code File already exists and won't be overwritten."
Else
tso.WriteLine("This file won't be overwritten if it exists.") ' Write some text to the file.
tso.Close ' Close the file.
End If
Err.Clear
On Error GoTo 0
Set fso = Nothing
End Sub
CreateFileNoOverwrite