vbscript - 无法从文本文件中提取数字

时间:2017-03-02 14:58:04

标签: vbscript

我想检查一个字符串的txt文件。 如果该字符串不存在,则从key.txt获取一个数字并将其写入另一个文件中。那部分工作正常。

但是当找到字符串时,我想将前五个字符读入变量并将此变量用于创建xml。当我运行我的脚本并找到字符串.... xml元素键为空...

任何人都可以帮助我吗?感谢

Dim FSSys, Reference, corp, Account, intCorp, strPK, FS
Set FSSys = Wscript.CreateObject("Scripting.FileSystemObject")   
Set Reference = FSSys.OpenTextFile("C:\corpreferenz.txt")   
intCorp = Reference.ReadAll
Reference.Close

intCorp = intCorp + 1

Set corp = FSSys.CreateTextFile("C:\corpreferenz.txt")   
corp.Write intCorp
corp.Close

Const FORREADING = 1
Const FORWRITING = 2
Const FORAPPENDING = 8

Dim sToSearch: sToSearch = "Test"
Dim sFileName: sFileName = "C:\Account.txt" 
Dim sContent, Found, TxtFile

If Not FSSys.FileExists(sFileName) Then 
    MsgBox "File Not Found"
    WScript.Quit 0
End If

Set TxtFile = FSSys.OpenTextFile(sFileName,FORREADING)
sContent = TxtFile.ReadAll

If InStr(sContent,sToSearch) Then 
    Found = True 
    while not TxtFile.AtEndOfStream
        sTemp = TxtFile.ReadLine
        If Instr(1,sTemp,sToSearch)>0 then
            strPK = strPK + sTemp
            FS = Left(strPK, 5)
        End If
    Wend
End If


Set TxtFile = Nothing

If Not Found Then 
    Set PKNumber = FSSys.OpenTextFile("C:\Key.txt")
    intPKNumber = PKNumber.ReadAll
    PKNumber.Close
    intPKNumber = intPKNumber + 1
    Set PKNum = FSSys.CreateTextFile("C:\Key.txt")
    PKNum.Write intPKNumber
    PKNum.Close
    FS = intPKNumber
    Set TxtFile = FSSys.OpenTextFile(sFileName,FORAPPENDING)
    TxtFile.WriteLine intPKNumber & " " & sToSearch
End If


'Create XML
set xml = CreateObject("Microsoft.XMLDOM")
set encoding = xml.createProcessingInstruction("xml", "version='1.0'")
xml.insertBefore encoding, xml.childNodes.Item(0)
set foo = xml.createElement("XML")
set bar = xml.createElement("Table")
set corp = xml.createElement("Corp")
set cdata = xml.createCDATASection(intCorp)
set konto = xml.createElement("Key")
set cdata1 = xml.createCDATASection(FS)
corp.appendChild cdata
bar.appendChild corp
konto.appendChild cdata1
bar.appendChild konto
foo.appendChild bar
xml.appendChild(foo)

xmlSave xml, "C:BUP.xml"

'Function for XML
function xmlSave(xml, filename)
    set rdr = CreateObject("MSXML2.SAXXMLReader")
    set wrt = CreateObject("MSXML2.MXXMLWriter")
    Set oStream = CreateObject("ADODB.STREAM")
    oStream.Open
    oStream.Charset = "ISO-8859-1"

    wrt.indent = True
    wrt.encoding = "ISO-8859-1"
    wrt.output = oStream
    Set rdr.contentHandler = wrt
    Set rdr.errorHandler = wrt

    rdr.Parse xml
    wrt.flush

    oStream.SaveToFile filename, 2
end function

1 个答案:

答案 0 :(得分:0)

sContent = TxtFile.ReadAll

你(读指针)位于文件的末尾; TxtFile.AtEndOfStream为True,TxtFile.ReadLine将不会被调用/将失败。证据:

>> Set f = CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.ScriptFullName)
>> WScript.Echo 0, CStr(f.AtEndOfStream)
>> s = f.ReadAll()
>> WScript.Echo 1, CStr(f.AtEndOfStream)
>>
0 Falsch (False)
1 Wahr (True)

你可以遍历文件的行(在vbCrLf上的Split()sContent),或者 - 更好 - 在sContent上使用RegExp来提取所需的数据。