How could i convert the following Sequential File coding to FSO File Coding ?

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hello

If anyone could just help me to convert the following Sequential File coding to FSO coding.

Will the same logic apply as RipTutorial Link

If your Reply is "YES" for below coding which could be quite equivalent to above link code then will work on it and move ahead.
ElseIf your Reply is "NO" or Partly agreeing then will appreciate your help with FSO coding

What i need is to overwrite the txtFilename2 with details of txtFilename and some added new data but with FSO method
VBA Code:
Private Sub Sequential_Result()

Dim txtfilename As String, txtfilename2 As String
Dim strDataTxtFile As String, lineCount As Integer

txtfilename = "C:\NimishK\1-Trial.txt"
txtfilename2 = "C:\NimishK\1A-Trial.txt"

Open txtfilename For Input As #1
  Open txtfilename2 For Output As #2
      While Not EOF(1)
        Line Input #1, strDataTxtFile
        lineCount = lineCount + 1
         Print #2, strDataTxtFile 
      Wend
         Print #2, vbCrLf
         Print #2, txtBox4.Text
 Close #2
Close #1
End Sub

Thanks
NimishK
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Something like this

VBA Code:
Sub jec()
 c00 = "C:\NimishK\1-Trial.txt"
 c01 = "C:\NimishK\1A-Trial.txt"

 With CreateObject("scripting.filesystemobject")
   .createtextfile(c01).write .opentextfile(c00).readall
 End With
End Sub
 
Upvote 0
Thankx JEC

VBA Code:
Sub jec()
 c00 = "C:\NimishK\1-Trial.txt"
 c01 = "C:\NimishK\1A-Trial.txt"

 With CreateObject("scripting.filesystemobject")
   .createtextfile(c01).write .opentextfile(c00).readall
 End With
End Sub

Will Try as per yours.
NimishK
 
Upvote 0
For writing just one line, you could use .WriteLine
 
Upvote 0
JEC

Am afraid to inform you that the code which you provided has not taken into consideration the additional text to be added from txtBox4.text

with or without
For writing just one line, you could use .WriteLine

NimishK
 
Upvote 0
Try something like:

VBA Code:
Sub jec()
 c00 = "C:\NimishK\1-Trial.txt"
 c01 = "C:\NimishK\1A-Trial.txt"

 With CreateObject("scripting.filesystemobject")
  xx = .opentextfile(c00).readall
   With .createtextfile(c01)
     .write xx
     .writeline vbLf & "test"    'your textline
   End With
 End With
End Sub
 
Upvote 0
Solution
JEC Indeed Thanks. It is solved with your valuable inputs.

As per Variables defined in the Sequential method i've modified the code retaining the same variables keeping your Logic ;)

VBA Code:
Public Sub FSO_Method()

Dim txtfilename As String, txtfilename2 As String
Dim strDataTxtFile As String, lineCount As Integer

Dim fso As Object
 Set fso = CreateObject("Scripting.FileSystemObject")

Dim ts As Object

txtfilename = "C:\NimishK\1-Trial.txt"
txtfilename2 = "C:\NimishK\1A-Trial.txt"

With fso
Set ts = .OpenTextFile(txtfilename, ForReading)
    strDataTxtFile = ts.ReadAll
   With .CreateTextFile(txtfilename2)
        .Write strDataTxtFile
        .WriteLine vbCrLf & txtBox4.Text
   End With
    ts.Close
End With

End Sub
Thankx once more ?
NimishK
 
Upvote 0
Nice! You’re welcome!?
 
Upvote 0
All

I would like to correct the above code as mentioned in #7 below because the newly file created was not closed.
This was observed as errors started generating when moved on further
Inconvenience regretted
Below corrected version
VBA Code:
Public Sub FSO_Method()

Dim txtfilename As String, txtfilename2 As String
Dim strDataTxtFile As String, lineCount As Integer

Dim fso As Object
 Set fso = CreateObject("Scripting.FileSystemObject")

Dim ts As Object

txtfilename = "C:\NimishK\1-Trial.txt"
txtfilename2 = "C:\NimishK\1A-Trial.txt"

With fso
Set ts = .OpenTextFile(txtfilename, ForReading)
    strDataTxtFile = ts.ReadAll
   With .CreateTextFile(txtfilename2)
        .Write strDataTxtFile
        .WriteLine vbCrLf & txtBox4.Text
        .Close  ''''''Newly Added Command
   End With
    ts.Close
End With

NimishK.
 
Upvote 0
Hello,​
question : why using FSO as VBA has already all the necessary or I missed something ?‼​
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top