Vb6: need help with a loop

So I have a loop that needs to do three things, loop through the text file line by line, the text file contains the path names and file names (C: \ Folder \ file1.txt) If the line contains a specific line, it then copies the file to that location , renames it to whatever it is named in the text file and then replaces the line in the copied file (still with me?). If not, go to the next line. I thought it would be pretty straight forward, but I don't seem to work, I currently can't even compile as I get errors saying the loop syntax is incorrect. Any help would be appreciated, here's the complete function code:

Private Sub Command2_Click()

Dim LineData As String 
Dim FileHandle As Integer


FileHandle = FreeFile
Open "C:\textfile.txt" For Input As #FileHandle

Do While Not EOF(FileHandle)
Line Input #FileHandle, LineData
If InStr(LineData, ".log") Then
FileCopy "C:\thefile.log",LineData
Open LineData For Input As #3
 #3 = Replace$(#3, "abc", "xyz")
Else
End If  
Loop
Close #FileHandle
Close #3






 MsgBox "Copy, Replace, Complete!"

End Sub

      

Thank you Advance!

0


source to share


4 answers


I do not believe

#3 = Replace$(#3, "abc", "xyz")

      

... You will need to read the contents of that file, preferably one at a time (or some reasonable chunk at a time), do your replacement, and then write it back to a new file. Since you are copying the file anyway, I would suggest reading it from the original location and writing the new version to the folder where the file would be copied.



eg...

Private Sub Command2_Click()
 Dim LineData As String
 Dim FileHandle As Integer
 Dim sourceHandle as Integer
 Dim destHandle as Integer
 dim temp as string
 FileHandle = FreeFile
 Open "C:\textfile.txt" For Input As #FileHandle
 Do While Not EOF(FileHandle)
  Line Input #FileHandle, LineData
  If InStr(LineData, ".log") Then
   sourceHandle=FreeFile
   Open "C:\thefile.log" For Input as #sourceHandle
   destHandle=FreeFile
   Open LineData For Output as #destHandle
   Do while Not EOF(sourceHandle)
    Line Input #sourceHandle,temp
    temp=replace$(temp,"abc","xyz")
    Print #destHandle,temp
   Loop
   Close #destHandle
   Close #sourceHandle
  End If
 Loop
Close #FileHandle
MsgBox "Copy, Replace, Complete!"
End Sub

      

Sorry if there are any bugs, this is untested as I don't have VB IDE installed on this machine.

+1


source


# 3 = Replace $ ... your problem ... fixing this and other things results in this:

Private Sub Command2_Click()
  Dim LineData As String
  Dim FileHandle As Integer
  Dim Buffer As String
  FileHandle = FreeFile
  Open "C:\thefile.log" For Binary Access Read As #FileHandle
  Buffer = Space(LOF(FileHandle))
  Get #FileHandle, , Buffer
  Buffer = Replace(Buffer, "abc", "xyz")
  Close #FileHandle
  FileHandle = FreeFile
  Open "C:\textfile.txt" For Input As #FileHandle
  Do Until EOF(FileHandle)
    Line Input #FileHandle, LineData
    If InStr(LineData, ".log") Then
      Open LineData For Output As #3
      Print #3, Buffer;
      Close #3
    End If
  Loop
  Close #FileHandle
  MsgBox "Copy, Replace, Complete!"
End Sub

      



If you prefer minimalism and shortcode, you can go with this:

Private Sub Command2_Click()
  ''// This code requires a reference to Microsoft Scripting runtime (Project -> References)
  Dim FSO As New Scripting.FileSystemObject
  Dim Files() As String
  Dim File As String
  Dim Data As String
  Data = Replace(FSO.OpenTextFile("C:\thefile.log").ReadAll(), "abc", "xyz")
  Files = Split(FSO.OpenTextFile("C:\textfile.txt").ReadAll(), vbCrLf)
  For Each File In Files
    If InStr(File, ".log") > 0 Then FSO.CreateTextFile(File, True).Write Data
  Next
  MsgBox "Copy, Replace, Complete!"
End Sub

      

+1


source


One problem I see is that you open a file descriptor and then # 3, but you close them in the same order and you have to close # 3 and then a file descriptor.

What I see is that the file descriptor is opened, then it looks for the line containing ".log" and then it does something - so good so far. However, he opens # 3, does something else, and then loops. He should close # 3 when done with him.

Moving "Close # 3" up 4 lines (to the next statement after "# 3 = Replace ..." should do what you expect and should compile.

0


source


I would like to suggest you a different approach. I did something like this, except that I knew I was making a copy and my method had to open the source and a new target (copy). I then read the original file line by line, making the necessary changes to the variable that I read in line before writing it back to the copy. My files were no more than a couple k, and that was fast enough and fixed one of your loops. You will need to have a variable inside your loop to tell you to rename the file to do this, or to delete the file if you don't find the line you want and don't need a copy.

0


source







All Articles