Results 1 to 8 of 8

Thread: kill file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    kill file

    Code:
    Private Sub mnuFileList_Click(Index As Integer)
        If Index = 1 Then
            Kill App.Path & "/Data.txt"
            CommonDialog1.ShowOpen
            Picture1.Picture = LoadPicture(CommonDialog1.FileName)
        End If
    End Sub

    i want the program always kill file "data.txt" when press the menu if the "data.txt" is present.
    But, i got the error "file not found"(because data.txt not present) with the code above. How to improve the code above to let it kill file when the data.txt is present.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: kill file

    Code:
    Private Sub mnuFileList_Click(Index As Integer)
        If Index = 1 Then
           If Dir$(App.Path & "/Data.txt") <> vbNullString Then 'it exists - kill it
              Kill App.Path & "/Data.txt"
              CommonDialog1.ShowOpen
              Picture1.Picture = LoadPicture(CommonDialog1.FileName)
           End If
        End If
    End Sub

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: kill file

    Check the directory to see if DIR$("FileName") is a not a null. If it is not, then you can safely kill the file. If it is a null and you then try to kill it, that generates the error. The correct statement in one line is thus

    Code:
    IF Dir$(FilePathName$)<> "" Then Kill FilePathName$
    Doctor Ed

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: kill file

    What difference between vbNullString and ""?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: kill file

    Quote Originally Posted by junlo
    What difference between vbNullString and ""?
    Memory allocation.

    "" has a length of 0 but still takes up memory. vbNullString requires no memory.

    Here is a brief tutorial.

  6. #6
    Addicted Member
    Join Date
    Mar 2007
    Posts
    177

    Re: kill file

    dim nPath as string
    nPath = "c:\folder\"
    If Len(Dir(nFolder & "\filename.txt")) <> 0 Then

    kill

    End If

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: kill file

    In this case none, and you can use > "", since it can't ever be < "".
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  8. #8
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: kill file

    This might also work:

    Code:
    If Len(Dir$(PathFileName$)) THEN KILL PathFileName$
    'implication not zero and no string comparison required
    Doctor Ed

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