Page 1 of 1

How to find out if a directory exists?

Posted: Wed Aug 10, 2005 6:29 am
by SebMcClouth
I need a function to detect if a directory exists or not. It's needed for my qbinux.

grtz
Seb

Posted: Wed Aug 10, 2005 6:51 am
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 :-).

Posted: Wed Aug 10, 2005 6:53 am
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

Posted: Wed Aug 10, 2005 7:09 am
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 ;-).

Posted: Wed Aug 10, 2005 8:21 am
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

Posted: Wed Aug 10, 2005 9:08 am
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..

Posted: Wed Aug 10, 2005 9:27 am
by Seb McClouth
Okay, I'm gonna try those.

grtz
Seb

another way, just for fun

Posted: Wed Aug 10, 2005 8:55 pm
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.

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

Posted: Wed Aug 10, 2005 9:05 pm
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.

if that's not a typo, nothing is.

Posted: Wed Aug 10, 2005 9:08 pm
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.

Posted: Thu Aug 11, 2005 3:15 am
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

yes, i phrased that in a sloppy manner

Posted: Thu Aug 11, 2005 9:58 am
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.

Posted: Thu Aug 11, 2005 2:27 pm
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.
*****

Re: How to find out if a directory exists?

Posted: Fri Oct 30, 2015 2:42 am
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.