QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 


  << Previous TopicReturn to Index  

Using the DOS CD command to find the current path.

March 29 2010 at 5:11 PM
Clippy  (Login burger2227)
R

 
Use the DOS "CD" command and "pipe" the display info to a file:

SHELL "CD > DOS-INFO.inf"

OPEN "DOS-INFO.inf" FOR INPUT AS #1
IF LOF(1) THEN LINE INPUT #1, CurrentPath$
CLOSE #1

'This works in QB and QB64! QB64 returns long path names.

 
 Respond to this message   
AuthorReply
Jon
(Login davonuk)

checking to see if a file exists

January 24 2014, 3:31 PM 

You may find it helpful to use DOS to find the path, but I use DOS to see if a data file exists. This relies on a few common variables I set beforehand:
I set direc$ as the data directory, i.e. "C:DATA", include the final .
temp$ would be the name of a temporary file, i.e. "TEMP.TMP"
I also set true and false as constants.
I would set ext$ as the extension of data files, i.e. "DAT" or maybe "HSC" if I am looking at a high score file.
Setting these as common variables, or constants I use the following function to check if a file exists.

So, setting all as constants I would say
CONST false = 0, true = NOT false
CONST direc$="C:DATA", ext$="HSC", temp$="TEMP.TMP"

FUNCTION filefound
CLOSE : OPEN direc$ + temp$ FOR OUTPUT AS #1: PRINT #1, "NOTHINGHERE!": CLOSE
SHELL "if exist " + direc$ + "*.* dir " + direc$ + "*.* > " + direc$ + temp$
OPEN direc$ + temp$ FOR INPUT AS #1: found = false
IF EOF(1) <> 0 THEN GOTO endfilefound
DO: INPUT #1, a$
IF LEFT$(a$, LEN(hifile$) + 1) = hifile$ + " " AND INSTR(a$, " " + ext$ + " ") > 0 THEN found = true
LOOP UNTIL EOF(1) <> 0
endfilefound:
CLOSE : KILL direc$ + temp$
filefound = found
END FUNCTION

I can extend this and use error handling to change direc$ if the folder doesn't exist.
This works in QB but not in QB64 as QB64 does not support ON LOCAL ERROR

This function is taken from a monopoly game I wrote in QB. In this case direc$ is a common variable, not a constant as it can be changed by the program. I also used two types of file exension, signfified as extd$ and extg$, and the 'x' varieable determined which applied.

FUNCTION filefound (f$, x)
IF x = 1 THEN e$ = extg$ ELSE e$ = extd$
ON LOCAL ERROR GOTO errorhandling
CLOSE : OPEN direc$ + temp1$ FOR OUTPUT AS #1: PRINT #1, "NOTHINGHERE!": CLOSE
SHELL "if exist " + direc$ + "*.* dir " + direc$ + "*.* > " + direc$ + temp1$
OPEN direc$ + temp1$ FOR INPUT AS #1: found = false
IF EOF(1) <> 0 THEN GOTO endfilefound
DO: INPUT #1, a$
IF LEFT$(a$, LEN(f$) + 1) = f$ + " " AND INSTR(a$, " " + e$ + " ") > 0 THEN found = true
LOOP UNTIL EOF(1) <> 0: GOTO endfilefound

errorhandling:
direc$ = "C:"
COLOR 13: PRINT : PRINT "DATA DIRECTORY NOT FOUND - CHANGED TO " + CHR$(34) + direc$ + CHR$(34)
RESUME 0

endfilefound:
CLOSE : KILL direc$ + temp1$
filefound = found
END FUNCTION

 
 Respond to this message   
JT
(no login)

What about _FILEEXISTS?

July 9 2014, 12:30 PM 

There is a way to check for existing files within QB64 that is super easy
the function _FILEEXISTS()
Look it it, its super!

 
 Respond to this message   
Luke
(Login flukiluke)
R

In QB64, you can use _CWD$

September 21 2014, 10:00 PM 

It's much easier than messing around with temporary files, and it's cross-platform too.

 
 Respond to this message   
 
  << Previous TopicReturn to Index  
 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums