|
-
Sep 16th, 2001, 03:51 PM
#1
Thread Starter
Member
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.
-
Sep 16th, 2001, 06:18 PM
#2
Fanatic Member
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 
-
Sep 17th, 2001, 02:10 PM
#3
Thread Starter
Member
Can you do named labels in QB, or was it so old that you had to do numbers (like GOTO 10)?
-
Sep 17th, 2001, 06:58 PM
#4
Fanatic Member
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 
-
Sep 17th, 2001, 07:19 PM
#5
Thread Starter
Member
I remember something like this a few years ago: I think the problem was that you can't use labels within functions.
-
Sep 17th, 2001, 08:27 PM
#6
Fanatic Member
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 
-
Sep 17th, 2001, 10:04 PM
#7
Thread Starter
Member
I'll try playing with it tomorrow.
-
Sep 18th, 2001, 06:16 AM
#8
Kaverin - Lose the first 'EXIT FUNCTION' statement, then the goto will work and so will your function.
-
Sep 18th, 2001, 08:26 PM
#9
Fanatic Member
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 
-
Sep 20th, 2001, 12:46 PM
#10
Thread Starter
Member
It's actually pretty stupid of QB to do that. But oh well.
-
Nov 22nd, 2001, 12:36 PM
#11
Fanatic Member
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
-
Nov 22nd, 2001, 02:13 PM
#12
Fanatic Member
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 
-
Dec 11th, 2001, 03:33 AM
#13
PowerPoster
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...
-
Dec 11th, 2001, 07:38 PM
#14
Banned
-
Dec 11th, 2001, 07:44 PM
#15
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|