使用VBScript读取和写入文件

时间:2009-07-17 11:21:02

标签: vbscript

我们如何使用VBScript读取和写入一些字符串到文本文件中?我的意思是我有一个已经存在的文本文件,因此当我使用下面的代码时: -

Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\New\maddy.txt",1,1) 

这只会打开文件进行阅读,但我无法写任何内容 当我使用这段代码时: -

Set fso = CreateObject("Scripting.FileSystemObject" )            
Set file = fso.OpenTextFile("C:\New\maddy.txt",2,1)

我可以使用此文件进行写入但无法读取任何内容。无论如何我们只需要调用OpenTextFile方法就可以打开文件进行读写。

我对VBScript很陌生。我只熟悉C概念。 是否有任何链接可以让我开始使用VBScript?

我想我需要对对象和属性概念有很好的了解。

8 个答案:

答案 0 :(得分:22)

您可以创建临时文件,然后将其重命名为原始文件:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.GetFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Set ts = objFile.OpenAsTextStream(1,-2)
Do Until ts.AtEndOfStream
    strLine = ts.ReadLine
    ' do something with strLine 
    objOutFile.Write(strLine)
Loop
objOutFile.Close
ts.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile 

使用OpenTextFile的用法几乎相同:

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
strTemp = "c:\test\temp.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)    
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    ' do something with strLine 
    objOutFile.Write(strLine & "kndfffffff")
Loop
objOutFile.Close
objFile.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile 

答案 1 :(得分:12)

http://msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx查找有关FileSystemObject对象的更多信息。为了获得好的VBScript,我建议:

  • 选项明确,以帮助检测变量中的拼写错误。
  • 功能 Sub 以提高可读性和重复使用
  • Const ,以便众所周知的常量被赋予名称

以下是一些读取和写入文本文件的代码:

Option Explicit

Const fsoForReading = 1
Const fsoForWriting = 2

Function LoadStringFromFile(filename)
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename, fsoForReading)
    LoadStringFromFile = f.ReadAll
    f.Close
End Function

Sub SaveStringToFile(filename, text)
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename, fsoForWriting)
    f.Write text
    f.Close
End Sub

SaveStringToFile "f.txt", "Hello World" & vbCrLf
MsgBox LoadStringFromFile("f.txt")

答案 2 :(得分:6)

您可以打开两个文本流,一个用于阅读

Set filestreamIn = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,1)

和一个附加

Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt,8,true)

filestreamIN可以从文件的开头读取,filestreamOUT可以写入文件的末尾。

答案 3 :(得分:5)

不要这么认为......你只能使用openTextFile来阅读(1),写作(2)或追加(8)。参考here

如果你是VBScript的using VB6 instead,你可以这样做:

Open "Filename" [For Mode] [AccessRestriction] [LockType] As #FileNumber

使用Random模式。例如:

Open "C:\New\maddy.txt" For Random As #1

答案 4 :(得分:2)

你可以将它放在Excel工作表中,如果它对你有价值,如果它需要其他的东西,但在excel表中存储信息是非常好的,因为你可以很容易地读取和写入

 'this gives you an excel app
 oExcel = CreateObject("Excel.Application")

 'this opens a work book of your choice, just set "Target" to a filepath
 oBook = oExcel.Workbooks.Open(Target)

 'how to read
 set readVar = oExcel.Cell(1,1).value
 'how to write
 oExcel.Cell(1,2).value = writeVar

 'Saves & Closes Book then ends excel
 oBook.Save
 oBook.Close
 oExcel.Quit

对不起,如果这个答案没有帮助,第一次写一个答案,只是觉得这可能是一个更好的方式

答案 5 :(得分:1)

您还可以读取整个文件,并将其存储在数组中

Set filestreamIN = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",1)
file = Split(filestreamIN.ReadAll(), vbCrLf)
filestreamIN.Close()
Set filestreamIN = Nothing

以您选择的任何方式操作数组,然后将数组写回文件。

Set filestreamOUT = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Test.txt",2,true)

for i = LBound(file) to UBound(file)
    filestreamOUT.WriteLine(file(i))
Next

filestreamOUT.Close()
Set filestreamOUT = Nothing 

答案 6 :(得分:1)

无论您尝试做什么,都不应该同时读取和写入文件。它还会使用更多应该始终避免的内存。我建议使用.ReadAll方法读取整个文件,然后关闭它并执行您需要处理的数据(假设您将内容读入变量),然后写入同一文件并覆盖文件。如果你担心在覆盖当前文件时出现问题,你总是可以尝试将其写入不同的文件,如果在尝试覆盖原始文件之前没有工作则会抛出错误

答案 7 :(得分:0)

下面是一些执行此操作的简单代码:

rails destroy scaffold Profile
相关问题