vbs读取文件

读取XML

'Get text from xml file according to file, node and item.
Function GetTextFromXMLByF(filename,node,item)
Dim xmlDoc
Dim colItem
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load(filename)
ValuePath = node&item
Set colItem = xmlDoc.selectSingleNode(ValuePath)
If (colItem is Nothing) then  
        Log1("Can not get the text from "& filename & " by the path:"&ValuePath)
        wscript.quit
Else
    GetTextFromXMLByF = colItem.text
End If
Set xmlDoc = Nothing
Set colItem = Nothing
End Function

'Get text from same name xml file
Function GetTextFromXML(node,item)
Dim filename, xmlname,scriptName
scriptName = wscript.scriptfullname
dataDir=left(scriptName,instrrev(scriptName,"\")-8)
shotname = wscript.scriptname
array1 = Split(shotname, ".")
xmlname = dataDir &"\xml\"&array1(0)&".xml"
GetTextFromXML = GetTextFromXMLByF(xmlname,node,item)
End Function

读取CSV
'Get text from csv file according to row and col
Function GetCellValueFromCSV(Row,Col)
dim cvs,objTextFile,returnValue
set cvs=CreateObject("Scripting.FileSystemObject")
dim lineText,arrStr
set objTextFile = cvs.OpenTextFile("DataSource.csv")
line = 0
Do while NOT objTextFile.AtEndOfStream
line = line + 1
lineText = objTextFile.ReadLine
if line = Row then
Col = Col - 1
arrStr = split(lineText,",")
returnValue = arrStr(Col)
Exit Do
end if
Loop
GetCellValueFromCSV = returnValue
objTextFile.Close
set objTextFile = Nothing
set cvs = Nothing
End Function

读取Excel

' open Excel file
Function OpenExcel(FileName)
Dim xlsSheet
Dim scriptName
scriptName = wscript.scriptfullname
dataDir=left(scriptName,instrrev(scriptName,"\")-1)
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = False
Set xlsWorkBook = xlsApp.Workbooks.Open(dataDir &"\"&FileName)
End Function

'Get value from Excel file according to sheet name, row and col
Function GetCellValueFromExcel(SheetName,Row,Col)
Dim xlsSheet, returnValue
Set xlsSheet = xlsWorkBook.Sheets(SheetName)
returnValue = xlsSheet.Cells(Row,Col)
GetCellValueFromExcel = returnValue
Set xlsSheet = Nothing
End Function

' Get the amount of rows in a worksheet
Function GetRow(SheetName)
Dim xlsSheet, returnValue
Set xlsSheet = xlsWorkBook.Sheets(SheetName)
returnValue = xlsSheet.usedRange.Rows.Count
GetRow = returnValue
Set xlsSheet = Nothing
End Function

' Get the row number of one key in a worksheet
Function GetRowNumber(SheetName,KeyName)
Dim resultRow
row = GetRow(SheetName)
For iLoop = 1 to row
    strValue = GetCellValueFromExcel(SheetName,iLoop,1)
        if StrComp(UCase(Trim(strValue)),UCase(Trim(KeyName)))=0 Then
        resultRow = iLoop
                Exit for
        End if
Next
GetRowNumber = resultRow
End Function

 

读取Txt

Function WriteToFile(byval value,byval path)
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(path, ForWriting, True)
   f.Write value
   Set f = fso.OpenTextFile(path, ForReading)
   WriteToFile =   f.ReadLine
End Function

Function ReadAllTextFile(byval path)
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(path, ForReading)
   ReadAllTextFile =   f.ReadAll
   set fso = nothing
End Function

posted on 2013-05-16 10:23  ibelieve  阅读(808)  评论(0编辑  收藏  举报

导航