How to find out if a directory exists?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

How to find out if a directory exists?

Post by SebMcClouth »

I need a function to detect if a directory exists or not. It's needed for my qbinux.

grtz
Seb
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Depending on which QB compiler you use. the PDS and VB-DOS have the DIR$ function you can use to find files and/or folders. you can look it up in the online help to see how it works :-).
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth »

Currently I'm stuck with 4.5. In order to get PDS on my laptop (which only has a diskdrive) I need to get my network back up under dos.

grtz
Seb
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

EDITED By me, MystikShadows

Since the post clearly showed nothing useful ;-)...Follow anthoni's answer below, it's the right way to do it ;-).
Last edited by MystikShadows on Wed Aug 10, 2005 8:24 am, edited 1 time in total.
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni »

You can use CHDIR too

Code: Select all

DIM SHARED myerr%

ON ERROR GOTO errproc
CHDIR "FOO"
IF myerr% = 72 THEN PRINT "Path not found": myerr% = 0
ON ERROR GOTO 0

 
end

errproc: myerr% = ERR: RESUME NEXT
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Non ERROR function:

Code: Select all

function IsDIR(dirname$) as integer
SHELL "Dir "+dirname$+" /ogn /b /-p /ad /-d /-w > Dirlist.dmy"
f = freefile
open "DirList.dmy" for append as #f
l = LOF(f)
close #f
if l then IsDIR = -1
end function
You can check up on DIR parameters by using: DIR /?
Additionally, you could just alter the DIRCMD paramter:

Code: Select all

SHELL "Set DIRCMD=/ogn /b /ad"
Before you call the IsDIR function..
I have left this dump.
Seb McClouth

Post by Seb McClouth »

Okay, I'm gonna try those.

grtz
Seb
mennonite

another way, just for fun

Post by mennonite »

i think antoni's way is best, but if you want one without using "on error", i'd probably use this for my own program:

Code: Select all

shell "if exist iffile del iffile"
'to check a directory in a dos batch file use if exist folder\nul:
shell "if exist \pathname\foldername\nul echo. > iffile"
(i'd much rather use <pre> than the code tag, but it doublespaces on the forum. i might know a way around this but i won't bother right now.)

now if "iffile" doesn't exist you can open "iffile" for binary and it lof(1) will equal 0, which means the folder doesn't exist either.

if lof(1) is not 0, the folder exists.

don't forget to close #1 of course :) and, if you really want to clean up after yourself, use shell "if exist iffile del iffile" again.
mennonite

by the way, you can't set the dircmd in dos 7.x or earlier

Post by mennonite »

z!re's version will probably work in xp, which doesn't have real dos anyway, but i believe in earlier implementations (and emulations) of dos, shell "set..." won't "stick."

while you can shell "set dircmd=..." before running your program (then it will be available) using SHELL in dos (and earlier versions of windows) is akin to starting command.com, running set, then closing command com.

try this:

SHELL "set flag="
SHELL "set"

you'll notice the flag isn't set. in dos 7 the only way around this is to write both the set command and the dir command to a .bat file and shell "batfile.bat"

then z!re's version will work in vanilla dos, because by the time you're done SHELLing, you'll have already redirected the dir output to a file.
mennonite

if that's not a typo, nothing is.

Post by mennonite »

shell "set flag=" was supposed to be shell "set flag=1" but if you fix that, it still won't stick. that's the point.

of course (only relevent to the typo) set flag= won't stick, that's how you remove a variable from the environment. again, not the point though.
User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth »

As a matter a fact, Set does work in Pre-Windows-DOS. I've used it for ages for a certain DIR-view.

I currently use SET for my temp folder, so DOS knows where it is, and location of mousedriver.

I've worked with DOS for over 3 or 4 year, before I actually started working windows 3.1 or 3.11 (which I still have). In those days, I made batch-files for a lot of things. I once had a cool program called BEN, which added several commands to DOS, and made me able to build a ASCII-graphic menu.

Anywayz, I'm gonna test the programs, and the one which works best, will be implented in QBinux.

grtz
Seb
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
mennonite

yes, i phrased that in a sloppy manner

Post by mennonite »

i did go on to clarify though.
it's possible to use set, and even make it work, but in vanilla dos and windows 9x, shell "set..." will not keep the parameters.
the next time you shell the setting will be unavailable:
shell "set flag=1"
shell "set"
the second command will display the environment but flag will not be there. (maybe in xp it will)
i showed a way the way around this problem using a .bat file.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Seb,

Taking some ideas from both Z!re and mennonite, a very simple approach for not using On Error is the following:

Assuming that the full path name that you want to see if exists is in FULLPATH$.

Code: Select all

SHELL "IF EXIST "    +FULLPATH$+"/NUL ECHO Y>YESNO.FIL"
SHELL "IF NOT EXIST "+FULLPATH$+"/NUL ECHO N>YESNO.FIL"
OPEN "YESNO.FIL" FOR INPUT AS #1
LINE INPUT #1,YN$
CLOSE #1
IF UCASE$(YN$)="N" THEN ............. go do logic for path does not exist.
' At this point the path exists.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
cobol30
Newbie
Posts: 1
Joined: Fri Oct 30, 2015 2:13 am

Re: How to find out if a directory exists?

Post by cobol30 »

In VBDOS the DIR$ command is mostly used for usage with files, but you able to see if a directory exists using in this way.

Print DIR$("C:\GAMES/nul")

If the directory does exist the text that is outputted to the screen should be 'C:\GAMES', and would be blank if the directory does not exist. This method works on all Windows and DOS versions, however it tends to be problematic when used with NT platforms. The reason is that Windows and DOS used a FAT file system, and all directories including the root contain a file called nul. You can also use the method to see whether or not a drive exists also. Below I have included two functions that perform this task, the first checks whether or not a directory exists, and the second checks whether or not a drive exists. The error checking routines that come included with the functions, is only needed when trying to check a drive that is not available or ready. When the functions return -1 that means that directory or drive does not exist, and if they return 0 that means that directory or drive does exist.

FUNCTION DirectoryExists (DirectoryPath AS STRING) AS DOUBLE
DirectoryExists = -1
ON ERROR RESUME NEXT
IF (DIR$(DirectoryPath + "/nul") <> "") THEN DirectoryExists = 0
IF (ERR <> 0) THEN DirectoryExists = -1
END FUNCTION

FUNCTION DriveExists (DriveLetter AS STRING) AS DOUBLE
DriveExists = -1
ON ERROR RESUME NEXT
IF (DIR$(DriveLetter + "/nul") <> "") THEN DriveExists = 0
IF (ERR <> 0) THEN DriveExists = -1
END FUNCTION

I hope you find this helpful.
Post Reply