使用VBS编辑我的.text文档

时间:2015-03-22 19:39:55

标签: vbscript

在VBScript中,我想用新数据更新文本文件并将其显示在消息框中。

以下是我到目前为止的情况;我做错了什么?

Option Explicit 
Dim oFso, Michael, John, Valery, Susane, Katterina
Dim oStream, oFolder, f, myArrayList

Const ForAppending = 8
Const ForReading = 1, ForWriting = 2

Set myArrayList = CreateObject("System.Collections.ArrayList")  
    myArrayList.Add "Misko, Janko, Vierka,"

'create '
Call WriteLineToFile

Function WriteLineToFile
    Set oFso = CreateObject("Scripting.FileSystemObject") 
    Set f = oFso.CreateTextFile("D:\TestFile1.txt", 2, True)

    f.WriteLine "Misko, Janko, Vierka,"

    MsgBox "Subor C:\TestFile.txt bol " & "vytvoreny."

    f.Close

    Set f = Nothing
    Set oFso = Nothing

    MsgBox "Uspesne vytvoreny " & TestFile2.txt & "."   
End Function

2 个答案:

答案 0 :(得分:0)

Option Explicit 'explicit declaration of all variables'

Dim oFso, f, sPath, sPath2, i, sTemp 'deklaracia' Dim arrString

Const ForReading = 1, ForWriting = 2

sPath = "D:\TestFile1.txt" 'cesta ku datam' sPath2 = "D:\TestFile2.txt"

arrString = Array("Marek", "Tomas") 'pole alebo polia' ReDim arrString(2) arrString(0) = "Misko" arrString(1) = "Janko" arrString(2) = "Vierka"

sTemp = ""      'empty pole pred runom'

For i = 0 To UBound(arrString) 'ide po upper bound ' If i = UBound(arrString) Then sTemp = sTemp + arrString(i) 'odstrani ciarku na konci' Else sTemp = sTemp + arrString(i) + ", " End If Next MsgBox sTemp

答案 1 :(得分:0)

Call WriteLineToFile (sPath, sTemp)         'zavola sub routine'

ReDim Preserve arrString (4) arrString(3) = "Zuzka" arrString(4) = "Katka"

sTemp = ""

For i = 0 To UBound(arrString) If i = UBound(arrString) Then sTemp = sTemp + arrString(i) Else sTemp = sTemp + arrString(i) + ", " End If
Next MsgBox sTemp

Call WriteLineToFile (sPath2, sTemp)

Sub WriteLineToFile (sFilePath, sText)      
    Set oFso = CreateObject("Scripting.FileSystemObject") 
    Set f = oFso.CreateTextFile(sFilePath, 2, True)
    'For i = 0 To UBound(arrString) - 1         'nepotrebne'
        f.WriteLine sText
    'Next
        MsgBox "Subor " & sFilePath & " vytvoreny."

f.Close Set f = Nothing Set oFso = Nothing End Sub