Reading and writing value from a textfile by using vbscript code

后端 未结 4 1185
太阳男子
太阳男子 2021-01-05 01:30

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 v

4条回答
  •  伪装坚强ぢ
    2021-01-05 01:56

    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
    

提交回复
热议问题