Sorry to be so out of date but I am upgrading and compling (Quick64) some GWbasic programs to run on 64bit machines. I need to find the presence or absence of a file. In a batch file it would program something like this ..., but I want this to execute within GWBasic.

If exist c:\progA then goto A else B
:A do something
:B do something else

Thanks for any help

Dani AI

Generated

Two practical approaches depending on which BASIC runtime/compiler the old GW‑BASIC code will actually run under:

If compiling with a QuickBASIC/QB64-style tool (QB64, FreeBASIC, QuickBASIC), prefer the built‑in helpers (simpler and safer). QB64 exposes _FILEEXISTS (returns true/-1 when the file is present) and QuickBASIC/QBASIC provide DIR$ for pattern matching; both avoid raising an I/O error just to test for presence. (qb64phoenix.com)

Example (QB64 / QuickBASIC style):

' QB64: preferred
IF _FILEEXISTS("C:\PROG\MYFILE.TXT") THEN
  ' file present: proceed to OPEN/READ
ELSE
  ' file missing: alternate flow
END IF

When stuck running the original GW‑BASIC interpreter (or when a compatibility layer lacks _FILEEXISTS/DIR$), use an error‑trapping OPEN-for‑INPUT test and a concise handler. This is the portable fallback and is the technique recommended — with a couple of hardening points: use the GW‑BASIC OPEN syntax, restore error trapping afterward, and use RESUME NEXT in the handler so the program continues cleanly. See the GW‑BASIC OPEN and ON ERROR behavior for exact syntax. (hwiegman.home.xs4all.nl)

Example (GW‑BASIC style):

10 ON ERROR GOTO 200
20 found = 0
30 OPEN "I", 1, "C:\PROG\MYFILE.TXT"
40 CLOSE 1
50 found = -1
60 ON ERROR GOTO 0
70 IF found THEN PRINT "Found" ELSE PRINT "Missing"
80 END
200 found = 0
210 RESUME NEXT

Caveats: a successful existence test does not guarantee the file can be opened (permissions, locks, removable media, or network shares may fail at OPEN time), so always follow an existence check with guarded OPEN/READ or keep the ON ERROR handler active around the actual open/read sequence. For QB64, the docs explicitly note _FILEEXISTS does not guarantee access — handle access errors regardless. (qb64phoenix.com)

Summary: prefer _FILEEXISTS or DIR$ when compiling with modern QB‑compat tools; otherwise use a short ON ERROR + OPEN test in GW‑BASIC and handle permission/drive errors explicitly.

Use error trapping with an error-handling routine: ERR code 53 [file not found], but you should account for pathname and disk errors : 70 [Disc access denied] and 71 [Disc is not ready]; also 55, 72, 75, 76 can pull you up - check the GWBasic error codes [Gurgle them]. If you don't pay attention to these other error possibilities then... well, computers are unforgiving.
So, set up your error handler, then open and close the file to test it..

ON ERROR GOTO errorfileopen 'enables the error handler errorfileopen
OPEN C:ProgA for INPUT as #1 'tests you can actually read the file
CLOSE
ON ERROR GOTO 0 'terminates the error handler

errorfileopen:
'here you would use SELECT CASE ERR [I prefer this] or a simple IF THEN [ELSE] statement: [IF ERR=53 THEN ... ELSE ....]
RESUME 'takes you back to the CLOSE statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.