-1
   Dim sFile
    sFile = "File Name"

    Dim RecCount
    Set objFSO = CreateObject("Scripting.FileSystemObject")


    RecCount = 1
    Set objFileRead = objFSO.OpenTextFile(sFile,1)
    Do Until objFileRead.AtEndOfStream
    'Set objFileRead = objFSO.OpenTextFile(sFile,RecCount)
     'RecCount = RecCount + 1
    text = objFileRead.ReadLine
     set objFileWrite = objFSO.OpenTextFile(sFile,2)
    objFileWrite.WriteLine(replace(text, "|", ","))
  '  objFileRead.close
      RecCount = RecCount + 1
     loop
      objFileRead.close
      objFileWrite.Close
      Msgbox " files processed"

This code only changes the first line and erases rest of the 2000 lines. Anything to be changed on the loop? Please help

2
  • Any reason why you want to do this line by line? Commented Jul 1, 2020 at 12:47
  • 1
    If I execute that code I'm getting an error "Permission denied" as was to be expected if you are reading from a file and at the same time try to be writing to it. Please make sure you make an actual minimal reproducable sample demonstrating the problem stackoverflow.com/help/minimal-reproducible-example Commented Jul 1, 2020 at 12:57

1 Answer 1

1

You can't replace entire file content using ReadLine and WriteLine methods. Create another output file as follows:

Option Explicit
Dim sFile, sFileOut
sFile    = "D:\pshell\SO\Journals\test2.csv"
sFileOut = "D:\pshell\SO\Journals\test2Comma.csv"

Dim RecCount, objFSO, objFileRead, objFileWrite, text
Set objFSO = CreateObject("Scripting.FileSystemObject")
RecCount = 0

Set objFileRead  = objFSO.OpenTextFile(sFile,1)
Set objFileWrite = objFSO.OpenTextFile(sFileOut,2,true)

Do Until objFileRead.AtEndOfStream
    text = objFileRead.ReadLine
    objFileWrite.WriteLine(replace(text, "|", ","))
    RecCount = RecCount + 1
Loop
objFileRead.Close
objFileWrite.Close
Wscript.Echo "records processed " & CStr( RecCount)

Output:

cscript //NoLogo D:\bat\SO\62676742.vbs && type "D:\pshell\SO\Journals\test2*.csv"
records processed 8

D:\pshell\SO\Journals\test2.csv


"Name"|"AtBats"|"Hits"
"Ken Myer Jr."|"43"|"13"
"Pilar Ackerman"|""|"11"
"Jonathan Haas"|"37"|"17"

D:\pshell\SO\Journals\test2Comma.csv


"Name","AtBats","Hits"
"Ken Myer Jr.","43","13"
"Pilar Ackerman","","11"
"Jonathan Haas","37","17"

As an alternative, use ReadAll and Write methods to replace entire file content as follows:

Option Explicit
Dim sFile
sFile    = "D:\pshell\SO\Journals\test62676742.csv"

Dim RecCount, objFSO, objFileRead, objFileWrite, text
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFileRead  = objFSO.OpenTextFile(sFile,1)
text = objFileRead.ReadAll
objFileRead.Close

Set objFileWrite = objFSO.OpenTextFile(sFile,2,true)
objFileWrite.Write(replace(text, "|", ","))
objFileWrite.Close

Wscript.Echo "file processed"

Output (the initial copy re-establishes input file with ˙|˙ vertical lines):

copy /Y "D:\pshell\SO\Journals\test2.csv" "D:\pshell\SO\Journals\test62676742.csv"
        1 file(s) copied.
cscript //NoLogo D:\bat\SO\62676742b.vbs && type "D:\pshell\SO\Journals\test6*.csv"
file processed

D:\pshell\SO\Journals\test62676742.csv


"Name","AtBats","Hits"
"Ken Myer Jr.","43","13"
"Pilar Ackerman","","11"
"Jonathan Haas","37","17"

Not the answer you're looking for? Browse other questions tagged or ask your own question.