wsx63 发表于 2011-6-1 13:36:58

vbs读取文本文件的问题,急

我这里有一个文本文件,大概内容和格式如下:lang=en

####
a=1

b=2
c=3我想取=号两边的内容,但是文件里有空格行也有其它不相关的行
我用readline似乎无法办到,请大家帮个忙,谢谢了啊

17800455 发表于 2011-6-1 18:15:00



'*************************Rfile()**************************
' 功能概述:读文件内容
' 传入参数:1>FPath文件路径
'                   2>intVal值为0取等号左边,1取等号右边
' 返 回 值:返回数组
' 调用举例:1>tmpAry = Rfile("c:\test.txt",0)
'                   2>tmpAry = Rfile("c:\test.txt",1)
'***********************************************************
Public Function Rfile(FPath,intVal)

      Set fso = CreateObject("Scripting.FileSystemObject")
      
      If fso.FileExists(FPath) Then

                Set fopen = fso.OpenTextFile(Fpath,1)
      
                While Not fopen.AtEndOfStream
               
                        RC = fopen.ReadLine
                        
                        If Left(RC,1) <> "" Then
                        
                              tmparry = Split(RC,"=",-1,1)
                        
                              ReDim Preserve tmpAry1(i)
                              
                              tmpAry1(i) = tmparry(CInt(intVal))
                              
                              i = i + 1
                        
                        End if
               
                Wend
               
                fopen.Close
               
      End If
      
      Rfile = tmpAry1

      Set fso = nothing


End Function



要下班了就没有进行测试...........

gezhirong 发表于 2011-6-2 00:19:22

Function TxtReadLines(txtPath,delimiter)
Dim objDictionary,objFso,objTxtFile
Dim strReadLine,splitReadLine
Dim lineCount
Set objDictionary=CreateObject("Scripting.Dictionary")
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTxtFile=objFso.OpenTextFile(txtPath,1,true)
Do while not(objTxtFile.AtEndOfStream)
strReadLine=objTxtFile.ReadLine
objTxtFile.SkipLine
If instr(strReadLine,delimiter)>0 Then
lineCount=lineCount+1
splitReadLine=split(strReadLine,delimiter)
objDictionary.Add "left"&cstr(lineCount),trim(splitReadLine(0))
objDictionary.Add "right"&cstr(lineCount),trim(splitReadLine(1))
End If
loop
Set objTxtFile=nothing
Set objFso=nothing
Set TxtReadLines=objDictionary
Set objDictionary=nothing
End Function
'传递文件的路径和分隔符,比如你说的等号
Set txtDic=TxtReadLines("d:\text.txt","=")
For i=1 to txtDic.count/2
'等号左边的值
msgbox txtDic.Item("left"&cstr(i))
'等号右边的值
msgbox txtDic.Item("right"&cstr(i))
Next

gezhirong 发表于 2011-6-2 00:19:39

Function TxtReadLines(txtPath,delimiter)
        Dim objDictionary,objFso,objTxtFile
        Dim strReadLine,splitReadLine
        Dim lineCount
        Set objDictionary=CreateObject("Scripting.Dictionary")
        Set objFso=CreateObject("Scripting.FileSystemObject")
        Set objTxtFile=objFso.OpenTextFile(txtPath,1,true)
        Do while not(objTxtFile.AtEndOfStream)
                strReadLine=objTxtFile.ReadLine
                objTxtFile.SkipLine
                If instr(strReadLine,delimiter)>0 Then
                        lineCount=lineCount+1
                        splitReadLine=split(strReadLine,delimiter)
                        objDictionary.Add "left"&cstr(lineCount),trim(splitReadLine(0))
                        objDictionary.Add "right"&cstr(lineCount),trim(splitReadLine(1))
                End If
        loop
        Set objTxtFile=nothing
        Set objFso=nothing
        Set TxtReadLines=objDictionary
        Set objDictionary=nothing
End Function
'传递文件的路径和分隔符,比如你说的等号
Set txtDic=TxtReadLines("d:\text.txt","=")
For i=1 to txtDic.count/2
'等号左边的值
msgbox txtDic.Item("left"&cstr(i))
'等号右边的值
msgbox txtDic.Item("right"&cstr(i))
Next

gezhirong 发表于 2011-6-2 00:20:10

Function TxtReadLines(txtPath,delimiter)
Dim objDictionary,objFso,objTxtFile
Dim strReadLine,splitReadLine
Dim lineCount
Set objDictionary=CreateObject("Scripting.Dictionary")
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTxtFile=objFso.OpenTextFile(txtPath,1,true)
Do while not(objTxtFile.AtEndOfStream)
strReadLine=objTxtFile.ReadLine
objTxtFile.SkipLine
If instr(strReadLine,delimiter)>0 Then
lineCount=lineCount+1
splitReadLine=split(strReadLine,delimiter)
objDictionary.Add "left"&cstr(lineCount),trim(splitReadLine(0))
objDictionary.Add "right"&cstr(lineCount),trim(splitReadLine(1))
End If
loop
Set objTxtFile=nothing
Set objFso=nothing
Set TxtReadLines=objDictionary
Set objDictionary=nothing
End Function
'传递文件的路径和分隔符,比如你说的等号
Set txtDic=TxtReadLines("d:\text.txt","=")
For i=1 to txtDic.count/2
'等号左边的值
msgbox txtDic.Item("left"&cstr(i))
'等号右边的值
msgbox txtDic.Item("right"&cstr(i))
Next

gezhirong 发表于 2011-6-2 00:21:24

Function TxtReadLines(txtPath,delimiter)
        Dim objDictionary,objFso,objTxtFile
        Dim strReadLine,splitReadLine
        Dim lineCount
        Set objDictionary=CreateObject("Scripting.Dictionary")
        Set objFso=CreateObject("Scripting.FileSystemObject")
        Set objTxtFile=objFso.OpenTextFile(txtPath,1,true)
        Do while not(objTxtFile.AtEndOfStream)
                strReadLine=objTxtFile.ReadLine
                objTxtFile.SkipLine
                If instr(strReadLine,delimiter)>0 Then
                        lineCount=lineCount+1
                        splitReadLine=split(strReadLine,delimiter)
                        objDictionary.Add "left"&cstr(lineCount),trim(splitReadLine(0))
                        objDictionary.Add "right"&cstr(lineCount),trim(splitReadLine(1))
                End If
        loop
        Set objTxtFile=nothing
        Set objFso=nothing
        Set TxtReadLines=objDictionary
        Set objDictionary=nothing
End Function
'传递文件的路径和分隔符,比如你说的等号
Set txtDic=TxtReadLines("d:\text.txt","=")
For i=1 to txtDic.count/2
'等号左边的值
msgbox txtDic.Item("left"&cstr(i))
'等号右边的值
msgbox txtDic.Item("right"&cstr(i))
Next

43528782 发表于 2011-6-2 09:27:57

顶~

gezhirong 发表于 2011-6-2 09:35:59

Function TxtReadLines(txtPath,delimiter)
Dim objDictionary,objFso,objTxtFile
Dim strReadLine,splitReadLine
Dim lineCount
Set objDictionary=CreateObject("Scripting.Dictionary")
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTxtFile=objFso.OpenTextFile(txtPath,1,true)
Do while not(objTxtFile.AtEndOfStream)
strReadLine=objTxtFile.ReadLine

If instr(strReadLine,delimiter)>0 Then
lineCount=lineCount+1
splitReadLine=split(strReadLine,delimiter)
objDictionary.Add "left"&cstr(lineCount),trim(splitReadLine(0))
objDictionary.Add "right"&cstr(lineCount),trim(splitReadLine(1))
End If
loop
Set objTxtFile=nothing
Set objFso=nothing
Set TxtReadLines=objDictionary
Set objDictionary=nothing
End Function
'传递文件的路径和分隔符,比如你说的等号
Set txtDic=TxtReadLines("d:\text.txt","=")
For i=1 to txtDic.count/2
'等号左边的值
msgbox txtDic.Item("left"&cstr(i))
'等号右边的值
msgbox txtDic.Item("right"&cstr(i))
Next

wsx63 发表于 2011-6-2 09:43:42

今天看到了,谢谢大家,非常感谢

wendy.wang 发表于 2011-6-7 13:29:05

学习学习学习
页: [1]
查看完整版本: vbs读取文本文件的问题,急