|
-
Jun 10th, 2007, 05:09 AM
#1
Thread Starter
Addicted Member
[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?
-
Jun 10th, 2007, 05:15 AM
#2
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:
Dim sr As New IO.StreamReader("c:\hey.txt")
Dim line As String
Dim LinesToKeep As New List(Of String)
Do Until sr.EndOfStream
line = sr.ReadLine
If line.StartsWith("hey") Then
LinesToKeep.Add(line)
End If
Loop
sr.Close()
Dim sw As New IO.StreamWriter("c:\hey.txt")
For Each str As String In LinesToKeep
sw.WriteLine(str)
Next
sw.Close()
-
Jun 10th, 2007, 05:27 AM
#3
Thread Starter
Addicted Member
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.
-
Jun 10th, 2007, 06:11 AM
#4
Re: [2005] Deleting contents of a file
Could you delete the file and then create it again?
2
Code:
My.Computer.FileSystem.DeleteFile("Path & file Name")
IO.File.Create("Path & file here")
-
Jun 10th, 2007, 06:55 AM
#5
Thread Starter
Addicted Member
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?
-
Jun 10th, 2007, 07:11 AM
#6
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:
Using SR As New IO.StreamWriter("myfile.txt", False)
SR.Write(String.Empty)
SR.Close()
End Using
-
Jun 10th, 2007, 06:48 PM
#7
Thread Starter
Addicted Member
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.
-
Jun 10th, 2007, 07:28 PM
#8
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.
-
Jun 10th, 2007, 08:30 PM
#9
Thread Starter
Addicted Member
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.
-
Jun 12th, 2007, 06:45 PM
#10
Thread Starter
Addicted Member
Re: [2005] Deleting contents of a file
-
Jun 12th, 2007, 07:28 PM
#11
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?
-
Jun 12th, 2007, 07:58 PM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|