Zeile an Text-Datei anhängen mit VBScript

Zeile an Text-Datei anhängen mit VBScript

Funktion „AppendStringToFile“
Name: AppendStringToFile
Beschreibung: Funktion hängt eine neue Zeile an eine Text-Datei an
Parameter: string sFile (file name), string sValue (string to append to the file)
Rückgabe: boolean (TRUE wenn die Datei existiert, FALSE wenn nicht)

function AppendStringToFile(byval sFile, byval sValue)
    ' ********************************************************************************
    ' NAME:        AppendStringToFile
    '
    ' DESCRIPTION: Appends a string to a text file
    '
    ' PARAMETERS:  string sFile (file name)
    '              string sValue (string to append to the file)
    '
    ' RETURN:      boolean (TRUE if the file exists, FALSE if not)
    ' ********************************************************************************

    CONST ForAppending = 8
    dim oWSHShell : set oWSHShell = WScript.CreateObject("WScript.Shell")
    dim oFSO : set oFSO = CreateObject("Scripting.FileSystemObject")
    dim oFile

    AppendStringToFile = FALSE
    if NOT oFSO.FolderExists(oFSO.GetParentFolderName(sFile)) then
        oWSHShell.Run "%COMSPEC% /c mkdir """ & oFSO.GetParentFolderName(sFile) & """",0,TRUE
    end if
    if NOT oFSO.FileExists(sFile) then
        oFSO.CreateTextFile sFile, TRUE
    end if

    set oFile = oFSO.OpenTextFile(sFile, ForAppending, TRUE)
    oFile.WriteLine sValue
    oFile.close
    Wscript.sleep 100
    if oFSO.FileExists(sFile) then AppendStringToFile = TRUE
end function

Schreibe einen Kommentar