Results 1 to 12 of 12

Thread: [2005] Deleting contents of a file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    [2005] Deleting contents of a file

    How do I delete the contents of a file?

    I have been using streamWriter to make files and print text, however i've changed it instead of erasing the file, it adds text to it, however, I have about 60x of the same text... so how can I erase the content of a file?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Deleting contents of a file

    If youre looking to erase certain parts of a textfile, the streamreader/streamwriter is the way to go.

    Heres an example of removing all lines that doesnt begin with the word 'hey'
    VB.NET Code:
    1. Dim sr As New IO.StreamReader("c:\hey.txt")
    2.         Dim line As String
    3.         Dim LinesToKeep As New List(Of String)
    4.         Do Until sr.EndOfStream
    5.             line = sr.ReadLine
    6.             If line.StartsWith("hey") Then
    7.                 LinesToKeep.Add(line)
    8.             End If
    9.         Loop
    10.         sr.Close()
    11.         Dim sw As New IO.StreamWriter("c:\hey.txt")
    12.         For Each str As String In LinesToKeep
    13.             sw.WriteLine(str)
    14.         Next
    15.         sw.Close()
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    Thanks, however I am slightly confused

    1: Is that VB 2005 code? (by ur signature I assume so)

    2: I want to erase the entire thing (I cant actually erase the file)

    something like this would do

    Code:
    Dim sw As New StreamReader("path")
    Do Until sw.EndOfStream
    //Delete this line
    Loop
    sw.Close()
    something simple like that?
    Last edited by Icyculyr Fr0st; Jun 10th, 2007 at 05:35 AM.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Deleting contents of a file

    Could you delete the file and then create it again?

    2
    Code:
    1. My.Computer.FileSystem.DeleteFile("Path & file Name")
    2. IO.File.Create("Path & file here")
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    I could, but i'd have to do it alot, so it'd be best to just erase the text documents contents, can't I select each line of it? and delete it?

  6. #6
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Deleting contents of a file

    Let me get this right, you don't want to keep any of the contents, you just want an empty file? Nothing else?
    vb Code:
    1. Using SR As New IO.StreamWriter("myfile.txt", False)
    2.      SR.Write(String.Empty)
    3.      SR.Close()
    4. End Using
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    1: I am using vb express 2005, I was just asking Athiest if his code was in vb2005,

    2:

    Code:
    Using SR As New IO.StreamWriter("myfile.txt", False)
    that code will delete myfile.txt, then put an empty string into it (I belive)

    everytime I try and 'erase' over a file it says

    Code:
    'The process cannot access the file 'C:\Documents and Settings\Craig\Desktop\Mythic Fr0st\Visual Basic (WIN32)\Round Robin Pro (NEW)\Round Robin Pro\Round Robin Pro\bin\Debug\Variables\PlayerNames\player0.txt' because it is being used by another process.'
    Last edited by Icyculyr Fr0st; Jun 10th, 2007 at 06:54 PM.

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Deleting contents of a file

    Sounds like the file has been opened or used previously and you haven't put a close statement somewhere. The code I posted wouldn't really cause that error by itself so it's showing a problem that has its roots earlier in the process.

    Only you can really figure that out. Look where the file is used or created previously. Have you closed it as you should? That's where the answer will most likely be.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    I've fixed that now, which im getting the error I was originally getting from the get go (when I started trying to fix it)

    Code:
    Access to the path 'C:\Documents and Settings\Craig\Desktop\Mythic Fr0st\Visual Basic (WIN32)\New\Round Robin Pro (NEW)\Round Robin Pro\Round Robin Pro\bin\Debug\Variables\PlayerNames\player0.txt' is denied.
    My code for saving is
    the specific error it points at is in red, soon as I press Ctrl + S (Shortcut for save)

    Code:
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
            If user.locked = False Then
                If user.tmplock = False Then
                    label_41741.Text = "Saving... Please wait..."
                    System.Threading.Thread.Sleep(1000)
                    pleaseWaitMessage(True)
                    Application.DoEvents()
                    If Not (System.IO.Directory.Exists("Variables")) Then
                        System.IO.Directory.CreateDirectory("Variables")
                        SetAttr("Variables", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerNames")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerNames")
                        SetAttr("Variables\PlayerNames", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerNotes")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerNotes")
                        SetAttr("Variables\PlayerNotes", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerHP")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerHP")
                        SetAttr("Variables\PlayerHP", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerMP")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerMP")
                        SetAttr("Variables\PlayerMP", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerMO")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerMO")
                        SetAttr("Variables\PlayerMO", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerMO_FOR")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerMO_FOR")
                        SetAttr("Variables\PlayerMO_FOR", FileAttribute.Hidden)
                    End If
                    If Not (System.IO.Directory.Exists("Variables\PlayerLastHere")) Then
                        System.IO.Directory.CreateDirectory("Variables\PlayerLastHere")
                        SetAttr("Variables\PlayerLastHere", FileAttribute.Hidden)
                    End If
                    'System.IO.File.OpenWrite
                    For i As Int32 = 0 To playerGroup.n
                        Dim tmp_dir As String = "Variables\PlayerNames\player" & i & ".txt"
                        Dim sw As New System.IO.StreamWriter(tmp_dir, False)
                        sw.Close()
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.name(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerNotes\notes" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.notes(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerHP\HP" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.hp(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerMP\MP" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.mp(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerMO\MO" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.mo(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerMO_FOR\MO_FOR" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.mo_for(i))
                        sw.Close()
                        tmp_dir = "Variables\PlayerLastHere\LastHere" & i & ".txt"
                        sw = New System.IO.StreamWriter(tmp_dir, True)
                        SetAttr(tmp_dir, FileAttribute.Hidden)
                        sw.Write(playerGroup.lastHere(i))
                        sw.Close()
                    Next i
                    If Not (System.IO.File.Exists("Variables\hasSaved.txt")) Then
                        System.IO.File.Create("Variables\hasSaved.txt")
                        SetAttr("Variables\hasSaved.txt", FileAttribute.Hidden)
                    End If
                    pleaseWaitMessage(False)
                    label_41741.Text = "Loading... Please wait..."
                End If
            Else
                MessageBox.Show("Program is locked, go to View - Options - Unlock Program")
            End If
    End Sub
    Last edited by Icyculyr Fr0st; Jun 10th, 2007 at 08:34 PM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    Bump, anyone know?

  11. #11
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Deleting contents of a file

    Since it's an access denied message, do you have create/write rights to that folder? (i.e. is it ReadOnly). Also, does the folder path of tmp_dir actually exist?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Location
    Australia
    Posts
    237

    Re: [2005] Deleting contents of a file

    Just checked, yes it does exist, I see them all the files and folders i've made

    And... its not read only, just hidden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width