9

i have a variable named 'data' i need to write in to a textfile named "listfile.txt".Can you tell me the vbscript code to do that..And i need vbscript code for reading value from textfile "listfile.txt" also

peter
  • 8,158
  • 21
  • 66
  • 119
  • Does this answer your question? [Read and write into a file using VBScript](https://stackoverflow.com/questions/1142678/read-and-write-into-a-file-using-vbscript) – ndemarco Aug 20 '21 at 12:59

4 Answers4

41

To Write

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",2,true)
objFileToWrite.WriteLine(data)
objFileToWrite.Close
Set objFileToWrite = Nothing

OpenTextFile parameters:

<filename>, IOMode (1=Read,2=write,8=Append), Create (true,false), Format (-2=System Default,-1=Unicode,0=ASCII)

To Read the entire file

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\listfile.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Set objFileToRead = Nothing

To Read line by line

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
Loofer
  • 6,841
  • 9
  • 61
  • 102
Tester101
  • 8,042
  • 13
  • 55
  • 78
  • One little note about Win7: the Format parameter seems to be different from what you indicated in the answer: I have saved the same text content with VBScript using different encoding parameters from -2 to 2, than I opened in Notepad++ and verified the Format with the use of the homonym menu. These are the results I got from there for the File Encoding Format: **Utf16LeBom = -2 Utf16LeBom = -1 Utf8 = 0 Utf16LeBom = 1 Utf16LeBom = 2** So basically False = 0 = UTF-8 and everything else (that can be boolean True) is UTF 16 Little Endian with Bite Order Mark. Can someone else confirm? – willy wonka Aug 19 '23 at 23:32
  • So no ANSI encoding seems to be available. – willy wonka Aug 19 '23 at 23:44
2

Need help reading and writing text file using vbscript - Dev Shed

http://forums.devshed.com/asp-programming-51/need-help-reading-and-writing-text-file-using-vbscript-355967.html

VBScript - FileSystemObject

http://ezinearticles.com/?VBScript---FileSystemObject&id=294348

ratty
  • 13,216
  • 29
  • 75
  • 108
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
Zimba
  • 2,854
  • 18
  • 26
0

This script will read lines from large file and write to new small files. Will duplicate the header of the first line (Header) to all child files

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
Private
  • 9
  • 1