Results 1 to 15 of 15

Thread: Well, DOES it exist?

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Well, DOES it exist?

    How can I tell if a file exists in QBasic? It looks like FILES is not the answer:

    Code:
    The following statements illustrate the use of FILES.
                                    ----- Note -----
      Execution halts if you try to run this example without a disk in drive B,
      or if the specified files cannot be found.
                                    ----------------
    
    FILES              'Shows all files on the current directory.
    FILES "*.BAS"      'Shows all files with the extension .BAS.
    FILES "B:*.*"      'Shows all files on drive B.
    FILES "B:"         'Equivalent to "B:*.*".
    FILES "TEST?.BAS"  'Shows all five-letter files whose names
                       'start with "TEST" and end with the .BAS
                       'extension.
    FILES "SALES"     'If SALES is a directory, this statement
                       'displays all files in SALES; if SALES is
                       'a file in the current directory, this
                       'statement displays the name SALES.

  2. #2
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Well, this works, but for the life of me I can't figure out what QB is groaning about...
    Code:
    DECLARE FUNCTION Exists% (strFileName AS STRING)
    
    FUNCTION Exists% (strFileName AS STRING)
       Exists = 0
       ON ERROR GOTO NoFile
       OPEN strFileName FOR INPUT AS #1
       CLOSE #1
       Exists = 1
       EXIT FUNCTION
    NoFile:
    END FUNCTION
    When I use that, it will highlight the "ON" of the on error line, and say "label not defined". I have no clue why it's doing that. When I do that in the main module, it works fine. Moving it into a function makes it give me this cryptic error. Anyhow, the process itself works fine though. Just try to open the file for input with an error trap. No error means the file is there.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Can you do named labels in QB, or was it so old that you had to do numbers (like GOTO 10)?

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Originally posted by Kaverin
    When I use that, it will highlight the "ON" of the on error line, and say "label not defined". I have no clue why it's doing that. When I do that in the main module, it works fine. Moving it into a function makes it give me this cryptic error.
    Yes, it allows for labels. But for some reason it doesn't like it in this instance, even though I don't see anything syntactically wrong, and I don't think I've missed anything.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I remember something like this a few years ago: I think the problem was that you can't use labels within functions.

  6. #6
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    It should work. There's nothing that says it won't.
    Taken from QB's help

    GOTO Statement Details

    Syntax
    GOTO {linelabel | linenumber}

    The GOTO statement provides a way to branch unconditionally to another
    line (linelabel or linenumber). A GOTO statement can branch only to
    another statement at the same level of a program. You cannot use GOTO
    to enter or exit a SUB, FUNCTION, or multiline DEF FN function. You
    can, however, use GOTO to control program flow within any of these
    program structures.
    I made up some dummy subs and functions where I used goto, and they worked fine. I don't see why exactly it's not working here. It's acting like error trapping can't be done in a sub/function like this (but if you can't do it like this, how in the world do you do it at all)?
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  7. #7

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    I'll try playing with it tomorrow.

  8. #8
    jim mcnamara
    Guest
    Kaverin - Lose the first 'EXIT FUNCTION' statement, then the goto will work and so will your function.

  9. #9
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    That was something I tried a short time after I wrote the function, but it didn't work at all. I eventually did figure out why it didn't work (I think), but I never replied since I had other things to do. Apparently error handling labels have to be in the main module for some reason. I have no clue why. It's really counter intuitive.
    Lifted from QB
    HELP: Label Not Defined
    The statement refers to a label that QuickBASIC cannot
    find:
    GOTO, GOSUB, and single-line IF statements can only
    refer to labels in the current procedure.
    Labels that begin error-handling and event-handling
    routines must be in the main-module level of the
    program.
    Last edited by Kaverin; Sep 18th, 2001 at 08:35 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  10. #10

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    It's actually pretty stupid of QB to do that. But oh well.

  11. #11
    Fanatic Member jian2587's Avatar
    Join Date
    Aug 2000
    Location
    I bet u need a fusion powered shuttle to reach my place...
    Posts
    963
    QB45 is not PDS71, any ON error will goto the main module
    so you must put ur err handler there.

    To check a file existence, I think of a way, try it
    function FileExist(filename as string)
    FF = Freefile
    open filename for binary as #FF
    if lof(FF)=0 then close #FF:exit function
    close #ff
    FileExist=1
    end function
    ASM,C,C++,BASIC,VB,JAVA,VBS,HTML,ASP,PHP,mySQL,VB.NET,MATLAB
    Programming is fun, but only if you're not on a tight deadline
    So I consider all those working engineers sad people

    VB FTP class
    3 page PHP crash course
    Crash Course on DX9 Managed with VB.NET covering basics till terrain creation

  12. #12
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    What about a truly existing 0 byte file? Your function says it doesn't exist. It's not really a big deal, but it is one thing that particular method lets slip by.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  13. #13
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    On one of my old basics once (HiSoft Power Basic I think) there was a function called FEXISTS().

    That was the bees nipples, that was!
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  14. #14
    Banned Motxopro's Avatar
    Join Date
    Dec 2001
    Posts
    57
    no it doesn't. go away.

  15. #15

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Meh, punctuation, not the real one

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