Results 1 to 8 of 8

Thread: How to Rename/Delete .TXT file ??

  1. #1

    Thread Starter
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Arrow How to Rename/Delete .TXT file ??

    how can a rename .txt file and delete it ?
    Muhammad Furqan Attari.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to Rename/Delete .TXT file ??

    VB Code:
    1. 'to rename file:
    2. Private Sub Command1_Click()
    3. On Error Resume Next
    4.     Name "c:\temp\test.txt" As "c:\temp\test1.txt"
    5. End Sub
    6.  
    7. 'to delete file:
    8. Private Sub Command1_Click()
    9. On Error Resume Next
    10.     Kill "c:\temp\test.txt"
    11. End Sub
    Note: you will need "proper" error handler in place to check for file existance first (perhaps using Dir() function).

  3. #3
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Re: How to Rename/Delete .TXT file ??

    I'm not sure if this function is good practice or not, but I use it

    VB Code:
    1. If FileExists(App.Path & "\filename") Then Kill (App.Path & "\filename")
    2.  
    3. Private Function FileExists(ByVal File As String) As Boolean
    4. Dim fLen As Integer
    5.    
    6.     On Error Resume Next
    7.     fLen = Len(Dir$(File))
    8.     If Err Or fLen = 0 Then
    9.         FileExists = False
    10.     Else
    11.         FileExists = True
    12.     End If
    13.  
    14. End Function

    Only use App.Path if you want to search for the file where your exe resides. Obviously ByVal or ByRef is a choice you will make.

  4. #4
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: How to Rename/Delete .TXT file ??

    My method:

    Code:
    Dim CHKFL as string
    
    ' Check if folder exists, first.
    If Dir("C:\Dir", vbDirectory) = vbNullString Then
        Exit Sub
    end if
    
    ' Check if file exists
    CHKFL = Dir$("C:\Dir\File.exe")
    if CHKFL = "File.exe" then
        Kill("C:\Dir\File.exe")
    end if

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to Rename/Delete .TXT file ??

    That won't kill the file, as you aren't checking to see if CHKFL="C:\Dir\File.exe"

  6. #6
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: How to Rename/Delete .TXT file ??

    Weell... You're the expert, but I've always used this code myself (VB6) and tested it with both the file existing and not existing, and it performs perfectly for me. Actually, I learnt that syntax from my college textbook, which expresses it exactly like i've put there...

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to Rename/Delete .TXT file ??

    Oops
    I just tried it again, and you're right. I don't know what I was thinking.
    Sorry.

  8. #8
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: How to Rename/Delete .TXT file ??

    No problem ^_^ I cant talk anyway.. I'm a real noob at VB, but thanks to the lovely people here on the forums (Actually, you and Joacim are my biggest heros XD) I'm slowly learning more. I thank you for that

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