我有一个名为'data'的变量我需要写入一个名为“listfile.txt”的文本文件。你能告诉我vbscript 这样做的代码。我需要vbscript代码从textfile“listfile.txt”读取值
答案 0 :(得分:30)
写作
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",2,true)
objFileToWrite.WriteLine(data)
objFileToWrite.Close
Set objFileToWrite = Nothing
OpenTextFile参数:
<filename>, IOMode (1=Read,2=write,8=Append), Create (true,false), Format (-2=System Default,-1=Unicode,0=ASCII)
阅读整个文件
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Set objFileToRead = Nothing
逐行阅读
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",1)
Dim strLine
do while not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine()
'Do something with the line
loop
objFileToRead.Close
Set objFileToRead = Nothing
答案 1 :(得分:2)
需要帮助使用vbscript - Dev Shed
读取和写入文本文件VBScript - FileSystemObject
http://ezinearticles.com/?VBScript---FileSystemObject&id=294348
答案 2 :(得分:1)
Dim obj : Set obj = CreateObject("Scripting.FileSystemObject")
Dim outFile : Set outFile = obj.CreateTextFile("listfile.txt")
Dim inFile: Set inFile = obj.OpenTextFile("listfile.txt")
' read file
data = inFile.ReadAll
inFile.Close
' write file
outFile.write (data)
outFile.Close
答案 3 :(得分:0)
此脚本将读取大文件中的行并写入新的小文件。将第一行(标题)的标题复制到所有子文件
Dim strLine
lCounter = 1
fCounter = 1
cPosition = 1
MaxLine = 1000
splitAt = MaxLine
Dim fHeader
sFile = "inputFile.txt"
dFile = LEFT(sFile, (LEN(sFile)-4))& "_0" & fCounter & ".txt"
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(sFile,1)
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(dFile,2,true)
do while not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine()
objFileToWrite.WriteLine(strLine)
If cPosition = 1 Then
fHeader = strLine
End If
If cPosition = splitAt Then
fCounter = fCounter + 1
splitAt = splitAt + MaxLine
objFileToWrite.Close
Set objFileToWrite = Nothing
If fCounter < 10 Then
dFile=LEFT(dFile, (LEN(dFile)-5))& fCounter & ".txt"
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(dFile,2,true)
objFileToWrite.WriteLine(fHeader)
ElseIf fCounter <100 Or fCounter = 100 Then
dFile=LEFT(dFile, (LEN(dFile)-6))& fCounter & ".txt"
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(dFile,2,true)
objFileToWrite.WriteLine(fHeader)
Else
dFile=LEFT(dFile, (LEN(dFile)-7)) & fCounter & ".txt"
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(dFile,2,true)
objFileToWrite.WriteLine(fHeader)
End If
End If
lCounter=lCounter + 1
cPosition=cPosition + 1
Loop
objFileToWrite.Close
Set objFileToWrite = Nothing
objFileToRead.Close
Set objFileToRead = Nothing