Results 1 to 16 of 16

Thread: File Exist

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Location
    Vb Forums
    Posts
    226

    File Exist

    Ok in my program I have a log file that gets displayed in list box when you click a command button but if the log file isnt there it has error file not found so what I want to do is find if the log file exits or not and if it does exist it gets displayed in list box but if its not in the directory of the program then in the lasit box it says No file found... So code/help/ideas anything for this would be GREATLY appreciated
    Thanks For The Help Guys

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    Function FileExists(filename as str) as Boolean
             FileExists = (Dir(filename) > "")
    
    End Function
    Usage:
    Code:
    If FileExists("C:\myfile.log") then 
            ' open the file
    Else
            msgbox "File not Found", VbOkOnly
    End if

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Location
    Vb Forums
    Posts
    226
    Originally posted by jim mcnamara
    Code:
    Function FileExists(filename as str) as Boolean
             FileExists = (Dir(filename) > "")
    
    End Function
    Usage:
    Code:
    If FileExists("C:\myfile.log") then 
            ' open the file
    Else
            msgbox "File not Found", VbOkOnly
    End if
    That seems pretty sweet but with the code above:
    VB Code:
    1. Function FileExists(filename as str) as Boolean
    2.          FileExists = (Dir(filename) > "")
    3.  
    4. End Function
    that gives me an error saying"Compile error:
    User-defined type not defined"

    Whats wrong with it and how do I fix it??? Well thanks and all help is very much welcome
    Thanks For The Help Guys

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Location
    Vb Forums
    Posts
    226
    Could someone please state what the problem in this is? Thanks for your time and help in avance
    Thanks For The Help Guys

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    VB Code:
    1. Function FileExists(filename As [b]String[/b]) As Boolean
    2.   FileExists = (Dir(filename) > "")
    3. End Function
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Location
    Vb Forums
    Posts
    226
    Ok well thanks man but jim had:
    If FileExists("C:\myfile.log") then
    ' open the file
    Else
    msgbox "File not Found", VbOkOnly
    End if

    But I dont want it C:\myfile.log I want it LogFile which is located in the programs directory? Thanks for the help everyone
    Thanks For The Help Guys

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. If FileExists(App.Path & "\myfile.log") Then '...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    51

    WHAT if

    hey, what if you just want to check if there are any file's with a certain extension ?? ( .txt ?) or any file that starts with a certain character ( as001.txt )

    Thanks everyone

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You should just be able to use the '*' as a wildcard...
    VB Code:
    1. If FileExists(App.Path & "\a*.txt") Then '...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    51

    app.path

    hey , thanks for the help btw

    one more question how can i change the path of the APP.PATH ?

    Thanks
    Paolo

  12. #12
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    App.Path is a variable within VB that hold the current directory path to your app. You want to change it? Install your app someplace else. If you need to set the path, just use something like :

    VB Code:
    1. If FileExists("c:\MYPATH\a*.txt") Then '...

    App.Path is not necessary...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  13. #13
    Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    51
    hey thanks, dont know why i didnt think of that...i am such a newbie...but anyway.. i have another question ...how can i put that path into a string....

    i would like to open that file ?


    thanks

  14. #14
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    VB Code:
    1. Dim STRFILENAME as String
    2.  
    3. STRFILENAME = "C:\TEST.TXT"
    4.  
    5. If FileExistsSTRFILENAME ) Then '...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  15. #15
    Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    51

    sorry

    thanks , but this is what i really want to do,and i apologize for not explaining it right...

    1. i need to make sure that a file exist on a folder with a .txt extension.

    2. after verifying that the fileexist, i would like to open it

    3. last is to append some data to that text file.

    4.save it as the same filename as number 1


    thank you

  16. #16
    Member
    Join Date
    Oct 2002
    Location
    New York
    Posts
    51

    Talking anybody ??

    hello ?

    Thanks

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