vbs解決處理TXT文本數(shù)據(jù)相關(guān)問題實現(xiàn)代碼

字號:


    使用vbs處理txt數(shù)據(jù)時,會遇到一些問題,本文將提供詳細的解決方法,希望可以幫助你們
    有個小問題,如下:
    現(xiàn)有文本文件1.txt,內(nèi)容如下:
    數(shù)值_1出現(xiàn)頻度12647
    數(shù)值_2出現(xiàn)頻度10000
    數(shù)值_3出現(xiàn)頻度12608
    數(shù)值_4出現(xiàn)頻度8712
    數(shù)值_5出現(xiàn)頻度10658
    數(shù)值_6出現(xiàn)頻度8472
    數(shù)值_7出現(xiàn)頻度11232
    數(shù)值_8出現(xiàn)頻度8648
    數(shù)值_9出現(xiàn)頻度9264
    數(shù)值_10出現(xiàn)頻度7192
    數(shù)值_11出現(xiàn)頻度7192
    。。。。
    大概有100行
    要求把里面每行的數(shù)值放到變量中,然后輸出成文本文件 2.txt
    舉例: 把第一行的12674,放到變量a1中
    把第二行的10000,放到變量a2中
    把第三行的12608,放到變量a2中
    ….直到最后一行
    最后輸出成“2.txt” 文本文件的內(nèi)容為:
    a1 = 12647
    a2 = 10000
    a3 = 12608
    a4 = 8712
    ….
    a11 = 7192
    希望能能夠找到相關(guān)代碼,并且是能在windows下運行的??!找呀找呀找呀。。
    實現(xiàn)代碼如下:
    VB code:
    代碼如下:
    set fso = createobject("scripting.filesystemobject")
    set file=fso.opentextfile("1.txt")
    ts = file.readall
    file.close
    set fil = fso.createtextfile("2.txt")
    ts=replace(ts,"數(shù)值_","a")
    ts=replace(ts,"出現(xiàn)頻度","=")
    '''如果有橫線和空行,加上這個,沒有就注釋掉
    ts=replace(ts,"-----------------------"+vbnewline+vbnewline,"")
    fil.write ts
    fil.close
    MsgBox "處理完成"上面的代碼是把1.txt直接改成了2.txt,中間變量a1~a100省去了,如果還需要中間變量做其它用途的話,可以讀取2.txt內(nèi)容并賦值,代碼如下:
    VBScript code:
    set fso = createobject("scripting.filesystemobject")
    set ts = fso.opentextfile("2.txt")
    i=0
    do while ts.AtEndOfStream=false
    str = ts.ReadLine
    execute str '執(zhí)行賦值
    i=i+1
    execute("value=a" & i)'獲取變量 a1…… 的值
    Response.Write("a" & i & "值為:" & value &"<br/>") '輸出
    loop還有一種方法,如下面的代碼所示:
    VBScript code:
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set txt1 = fs.OpenTextFile("1.txt", 1)
    Set txt2 = fs.CreateTextFile("C:\FSO\ScriptLog.txt")
    Do Until txt1.AtEndOfStream
    str_a = txt1.ReadLine
    str_a = replace(str_a, "度","$")
    str_ar = split(str_a, "$")
    if isnumeric(str_ar(ubound(str_a))) then
    txt2.writeline str_ar(ubound(str_a))
    end if
    Loop
    txt1.close
    txt2.close
    set txt1 = nothing
    set txt2 = nothing
    set fs = nothing