10 Replies - 622 Views - Last Post: 27 August 2013 - 06:43 PM Rate Topic: -----

#1 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 13-August 11

Deleting strings from text file then resaving it.

Posted 26 August 2013 - 08:36 PM

As the title says, I want to open a file, delete all of the comas and "DATA"'s from it then resave it in a different directory. I feel like the code below should work but it does not. It resaves the file in a new directory with all the "DATA" and "," still in it.
Dim tr As IO.TextReader = New IO.StreamReader(ofd.FileName)
        Dim fark As String
        fark = tr.ReadToEnd
        fark.Replace("DATA", "")
        fark.Replace(",", "")
        tr.Close()
        Dim objfile As New StreamWriter("C:\Users\xilith117\Documents\artest.map")
        objfile.Write(fark)
        objfile.Close()


Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting strings from text file then resaving it.

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Deleting strings from text file then resaving it.

Posted 26 August 2013 - 09:16 PM

You need to assign the replacement like so.

fark = fark.Replace(",", "").Replace("DATA", "")

This post has been edited by _HAWK_: 26 August 2013 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 201
  • View blog
  • Posts: 1,257
  • Joined: 11-May 12

Re: Deleting strings from text file then resaving it.

Posted 26 August 2013 - 09:17 PM

Are you getting an error? Is it not working as expected? I'm assuming it isn't working as expected, because the tr.ReadToEnd I believe returns an Array. So your fark As String should be fark() As String

Then instead of fark.Replace("DATA", "")
you should be assigning it to itself .Replace("DATA", "")

like so

  Dim tr As IO.StreamReader = New IO.StreamReader(ofd.FileName)
  Using tr
    Dim fark() As String = tr.ReadToEnd.Split(vbNewLine)
    fark = fark.Replace("DATA", "")
    fark = fark.Replace(",", "")
  End Using

  Dim objFile As New StreamWriter("C:\Users\xilith117\Documents\artest.map")
  Using objFile
    For Each s As String in fark
      objFile.WriteLine(s)
    Next
  End Using



I apologize for kind of doing this work for you, but I just kinda started writing. If you have any questions on the modifications I made to your code feel free to ask and I'll do my best to explain everything I did. I won't be on for a while though, because it is midnight here and I work at 7:00 am, so I'll check back again at work. Hope this works for you :)/> keep in mind it's untested.

This post has been edited by ybadragon: 27 August 2013 - 05:06 AM

Was This Post Helpful? 1
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Deleting strings from text file then resaving it.

Posted 26 August 2013 - 09:21 PM

StreamReader.ReadToEnd does not return an array, it is a String.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1055
  • View blog
  • Posts: 4,085
  • Joined: 02-July 08

Re: Deleting strings from text file then resaving it.

Posted 26 August 2013 - 09:38 PM

Here is the reason why it did not work. Replace is a Function and it returns a value which did not have an assignment. You can call a function without assigning the return value - which does nothing except what happens inside the function itself. Example:

Private Function isTrue() As Boolean
   Return True
End Function



Usage(normal):

Private Sub DoCode()
 Dim bln As Boolean = isTrue()' now the variable is assigned the return value
End Sub


Usage(not normal):

Private Sub DoCode()
  isTrue()'legal to call, but now there is no variable assigned the return value
End Sub

Was This Post Helpful? 1
  • +
  • -

#6 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 201
  • View blog
  • Posts: 1,257
  • Joined: 11-May 12

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 05:05 AM

_HAWK_ is right StreamReader doesn't return an array, I'm just so used to splitting what I read from a file into an array that it becomes second nature to me to think that. So I added a .Split argument into the code above.
Was This Post Helpful? 0
  • +
  • -

#7 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 13-August 11

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 03:58 PM

:helpsmilie: I'm confused... where does the .split go? Thanks for the help so far everyone.
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 05:03 PM

View Postxilith117, on 27 August 2013 - 04:58 PM, said:

:helpsmilie:/> I'm confused... where does the .split go? Thanks for the help so far everyone.

You don't need .Split unless you want to split the String into pieces, delimited by a character. All ybadragon was saying is that he usually does split the file, probably on NewLine characters, giving him an array with each line of the file in its own string. If you want to split the file in that way, his example is the way to do it.
Was This Post Helpful? 0
  • +
  • -

#9 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 13-August 11

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 06:07 PM

Almost exactly from ybadragons example -
 Dim tr As IO.StreamReader = New IO.StreamReader(ofd.FileName)
        Using tr
            Dim fark() As String = tr.ReadToEnd.Split(ofd.FileName)
            fark = fark.Replace("DATA", "")
            fark = fark.Replace(",", "")
        End Using

        Dim objFile As New StreamWriter("C:\Users\xilith117\Documents\artest.map")
        Using objFile
            For Each s As String In fark()
                objFile.WriteLine(s)
            Next
        End Using

the compiler has issues with fark. cant use .replaces in arrays. if i make fark a string thats no good either because the end seems to rely on it?
Was This Post Helpful? 0
  • +
  • -

#10 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 201
  • View blog
  • Posts: 1,257
  • Joined: 11-May 12

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 06:34 PM

If you are wanting it to be in an array, and the .Replace isn't working, you could do something similar to the bottom loop at the top like so:

Dim fark() As String = tr.ReadToEnd.Split(ofd.FileName)
For Each s As String in fark
  s = s.Replace("DATA", "")
  s = s.Replace(",", "")
Next



This way you are looping through each item in the array and replacing any instance of "DATA" or "," in the current string in the loop.

This post has been edited by ybadragon: 27 August 2013 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 13-August 11

Re: Deleting strings from text file then resaving it.

Posted 27 August 2013 - 06:43 PM

I ended up doing this and it worked exactly as I wanted.
 Dim tr As IO.StreamReader = New IO.StreamReader("C:\Users\x\Desktop\YES.map")
        Dim fark As String = tr.ReadToEnd.ToString


        fark = fark.Replace("DATA", "")
        fark = fark.Replace(",", "")


        My.Computer.FileSystem.WriteAllText("C:\Users\x\Desktop\fixed.map", fark, False)


Posted Imagefor everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1