How to create temporary text file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 7425 Views - Last Post: 09 November 2011 - 07:45 PM Rate Topic: -----

#1 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

How to create temporary text file

Posted 07 November 2011 - 06:41 PM

Hello. I want to create temporary text file because after I use the text file, I want to delete it. But I just create the text file and write into th text file and at the end of the process, I want to delete it. However, the system won't allow me to delete it as it being use my another process. So I think, maybe by creating temporary file will solve this problem..Hope someone can help me.. This is my code for creating the text file and write into it.

         Dim objWriter As New System.IO.StreamWriter("C:\Blind Holes.tmp")
                                objWriter.WriteLine(lineP(1))
                                Do
                                    textline = sr.ReadLine()
                                    temp = temp + textline + vbCrLf

                                    objWriter.WriteLine(textline)
                                Loop Until textline = "M30"
                                objWriter.Close()


Im sorry typing error here. *However, the system won't allow me to delete it as it being use by another process*.

Is This A Good Question/Topic? 0
  • +

Replies To: How to create temporary text file

#2 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: How to create temporary text file

Posted 07 November 2011 - 08:27 PM

Your ObjWriter isn't being disposed by the time you are trying to delete it. You'll want to enclose the StreamWriter in a Using block to ensure garbage collection.

Using objWriter as  new New System.IO.StreamWriter("C:\Blind Holes.tmp")
  objWriter.WriteLine(lineP(1))
  Do
    textline = sr.ReadLine()
    temp = temp + textline + vbCrLf
    objWriter.WriteLine(textline)
  Loop Until textline = "M30"
  
objWriter.Close()
End Using



You may want to consider concatenating the string to a StringBuilder object instead since you know you'll be deleting the file anyway. Writing to the HD is a very expensive task.
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: How to create temporary text file

Posted 07 November 2011 - 08:27 PM

I don't think temp files are the way to go. Too many and you help to fragment the system and poss some memory leaks if not handled correctly. Can you explain better what your trying to accomplish? The error means you are not closing/disposing the file your working on.
Was This Post Helpful? 0
  • +
  • -

#4 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 08:36 PM

Actually I have create the text file name "Blind Holes.tmp". And Then I want to create another text file name ("drill.txt"). After I read the content from ("Blind Holes.tmp"), I will write the content into "drill.txt". After that, I want to delete the ("Blind Holes.tmp"). Hope u can understand. Its hard to explain because the process is complicated..
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: How to create temporary text file

Posted 07 November 2011 - 08:42 PM

We like complicated here, explain away...

I like XML as it is an in memory cashe of your data.
Was This Post Helpful? 0
  • +
  • -

#6 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 08:48 PM

I'm using vb 2008.. I have never use XML..I have to read the text file "drill1.txt" , and write the specific string to another text file "drill2.txt". And after I write to the new text file, I have to read the new created text file ("drill2.txt" to create another text file"drill3.txt" to manipulate the content. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#7 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: How to create temporary text file

Posted 07 November 2011 - 09:18 PM

Please tell me this is an assignment from another dickhole professor ...

I always just use the File.ReadAllText(strFilePath) and File.WriteAllText(strFilePath, strContentsToWrite) methods for file reading & writing. In my experience the WriteAllText method doesn't maintain a handle on the file, so you can delete it on the next line if that suits your fancy. However, I see people using StreamReaders & StreamWriters more often than I see these being used, and I don't know if these methods have any advantages (aside from ease of use) or disadvantages over StreamReaders & StreamWriters. If anyone does know, I'm all ears.

This post has been edited by Tryparse: 07 November 2011 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 09:58 PM

View PostTryparse, on 08 November 2011 - 12:18 PM, said:

Please tell me this is an assignment from another dickhole professor ...

I always just use the File.ReadAllText(strFilePath) and File.WriteAllText(strFilePath, strContentsToWrite) methods for file reading & writing. In my experience the WriteAllText method doesn't maintain a handle on the file, so you can delete it on the next line if that suits your fancy. However, I see people using StreamReaders & StreamWriters more often than I see these being used, and I don't know if these methods have any advantages (aside from ease of use) or disadvantages over StreamReaders & StreamWriters. If anyone does know, I'm all ears.

Thanks for suggestion, but I can't use File.ReadAllText because I have to read the file line by line..
Was This Post Helpful? 0
  • +
  • -

#9 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: How to create temporary text file

Posted 07 November 2011 - 10:12 PM

View Postyusmaiza, on 07 November 2011 - 10:58 PM, said:

Thanks for suggestion, but I can't use File.ReadAllText because I have to read the file line by line..

File.ReadAllLines(strFilePath) can help with that .. it returns a string array of the lines in the file. You could store it in a variable, or just call it when declaring a loop:
For Each s As String In File.ReadAllLines("...\drill1.txt")
    ...
Next

Was This Post Helpful? 0
  • +
  • -

#10 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 10:43 PM

Why I can't delete this new file?
Dim AppPath As String = "C:\Drill blind2.txt"
        AppPath = Environment.CurrentDirectory()
        Dim line As String = ""
        Dim s As String = ""
        Dim fileName As String = TextBox1.Text
        Dim nextStep As Boolean = False
        Dim sr As StreamReader = New StreamReader(fileName)

        While sr.Peek <> -1 'try while you have not reached end of the line
            line = sr.ReadLine() ' get each line and store it
            Dim lineR() As String = line.Split(",")
            If line.Contains("T") AndAlso line.Contains("Scanner") AndAlso line.Contains("Pin") AndAlso nextStep = False Then

                While sr.Peek <> -1 'try while you have not reached end of the line
                    line = sr.ReadLine() ' get each line and store it
                    Dim lineP() As String = line.Split(",")
                    If line.Contains("T") AndAlso line.Contains("Mil") AndAlso line.Contains("Ctr") AndAlso nextStep = False Then

                        Using sw As StreamWriter = File.CreateText("C:\Drill blind2.txt")
                            sw.WriteLine(lineP(1))
                            nextStep = True
                            If File.Exists("C:\Blind Holes.txt") = True Then
                                Dim file As String = ("C:\Blind Holes.txt")
                                Dim rs As StreamReader = New StreamReader(file)

                                Do

                                    line = rs.ReadLine()

                                    If line <> Nothing Then

                                        If line = lineP(1) Then
                                            Do
                                                line = rs.ReadLine()
                                                sw.WriteLine(line)


                                            Loop Until line.Contains(lineR(1)) Or line.Contains("M16")


                                        End If
                                    End If
                                Loop Until line = "M30"
                                'End If
                            End If
                        End Using
                        Exit While
                        'End If
                    End If
                   
                End While
            End If
            
        End While



After I use the file for this new text file
 Dim textline As String = ""
        Dim s As String = ""

        Dim fileName As String = TextBox1.Text
        Dim nextStep As Boolean = False

        If File.Exists("C:\Drill blind2.txt") Then

            Dim sr As New StreamReader("C:\Drill blind2.txt")
            'Dim NumberOfLines As Integer
            Dim line As String

            Dim rs As StreamReader = New StreamReader(fileName)
            While rs.Peek <> -1 'try while you have not reached end of the line
                textline = rs.ReadLine() ' get each line and store it
                Dim lineP() As String = textline.Split(",")
                If textline.Contains("T") AndAlso textline.Contains("Mil") AndAlso textline.Contains("Ctr") AndAlso lineP(3) = "100" AndAlso nextStep = False Then
                    Do While sr.Peek >= 1
                        line = sr.ReadLine()
                        If line.Contains("T") AndAlso line.Length <= 3 Then
                            If line = lineP(1) Then
                              Dim swriter As StreamWriter
                              swriter = File.AppendText("DrillBlind.drl")
                              swriter.WriteLine(lineP(1) & "=" &lineP(3))
                              swriter.Close()
                            End If

                        End If
                    Loop

                    Return

                    sr.Close()
                    sr.Dispose()

                    Exit While
                End If

            End While
           
        Else
        End If

Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: How to create temporary text file

Posted 07 November 2011 - 10:46 PM

Cant you just use IO.File.Delete? Or look in My.Computer.FileSystem?
Was This Post Helpful? 0
  • +
  • -

#12 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 10:50 PM

View PostjimmyBo, on 08 November 2011 - 01:46 PM, said:

Cant you just use IO.File.Delete? Or look in My.Computer.FileSystem?

How to use IO.File.Delete? Can u show me
Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How to create temporary text file

Posted 07 November 2011 - 11:28 PM

As _HAWK_ said above, creating a file in that way and then use it as a temp file is not a good idea. If you have decided to create a temp file, then you have to make sure that you delete it after using, and if fails to do so then next Disc Cleanup will. But how? using Path.GetTempFileName() method.
The method creates a temporal file and returns a full path of the file. Then use the file while 100% sure that it will be deleted. This will reduce unused files if you forget to delete it, and so make system clear.
Look at this example also to see how to use that method.
Was This Post Helpful? 0
  • +
  • -

#14 yusmaiza  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 15-September 11

Re: How to create temporary text file

Posted 07 November 2011 - 11:35 PM

View Postsmohd, on 08 November 2011 - 02:28 PM, said:

As _HAWK_ said above, creating a file in that way and then use it as a temp file is not a good idea. If you have decided to create a temp file, then you have to make sure that you delete it after using, and if fails to do so then next Disc Cleanup will. But how? using Path.GetTempFileName() method.
The method creates a temporal file and returns a full path of the file. Then use the file while 100% sure that it will be deleted. This will reduce unused files if you forget to delete it, and so make system clear.
Look at this example also to see how to use that method.


I have try the given code before but how to create the text file?

' Writes text to a temporary file and returns path
    Dim strFilename As String = System.IO.Path.GetTempFileName()
    Dim objFS As New System.IO.FileStream(strFilename, _
    System.IO.FileMode.Append, _
    System.IO.FileAccess.Write)
    ' Opens stream and begins writing
    Dim Writer As New System.IO.StreamWriter(objFS)
    Writer.BaseStream.Seek(0, System.IO.SeekOrigin.End)
    Writer.WriteLine(Data)
    Writer.Flush()
    ' Closes and returns temp path
    Writer.Close()
    Return strFilename


Should I put the file name here
   Dim strFilename As String = System.IO.Path.GetTempFileName("C:\test.txt")

Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How to create temporary text file

Posted 07 November 2011 - 11:52 PM

It creates a name by itself, you dont need to care about the name because that line gives you a complete file path with name and everything :) Just use that name returned and it will simplify the life than.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3