Metodo CreateTextFile

Crea un nome file specificato e restituisce un oggetto TextStream che può essere utilizzato per eseguire operazioni di lettura o scrittura nel file.

Sintassi

object.CreateTextFile(filename[, overwrite[, unicode]])

Argomenti

  • Object: obbligatorio. Sempre il nome dell'oggetto FileSystemObject.
  • Filename: obbligatorio. Espressione stringa che identifica il file da creare.
  • Overwrite: facoltativo. Valore booleano che indica se è possibile sovrascrivere un file esistente. Il valore è true se il file può essere sovrascritto o false se non può essere sovrascritto.
  • Unicode: facoltativo. Valore booleano che indica se il file viene creato come file Unicode o ASCII. Il valore è true se il file viene creato come file Unicode o false se viene creato come file ASCII.

    Nota:

    L'argomento Unicode è inattivo e viene mantenuto come riferimento alla sintassi dello script VB. Non ha alcun effetto. Tutti i file creati vengono creati solo in formato Unicode.

Note

Nel codice seguente viene illustrato come utilizzare il metodo CreateTextFile per creare e aprire un file di testo.

Esempio 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 l'argomento overwrite è false e il nome di file esiste già, si verifica un errore.

Esempio 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

Esempio 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