How to find out if a directory exists?
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
How to find out if a directory exists?
I need a function to detect if a directory exists or not. It's needed for my qbinux.
grtz
Seb
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.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
-
- Veteran
- Posts: 703
- Joined: Sun Nov 14, 2004 7:36 am
- Contact:
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
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
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
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.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
-
- Veteran
- Posts: 703
- Joined: Sun Nov 14, 2004 7:36 am
- Contact:
EDITED By me, MystikShadows
Since the post clearly showed nothing useful ...Follow anthoni's answer below, it's the right way to do it .
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
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
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
Non ERROR function:
You can check up on DIR parameters by using: DIR /?
Additionally, you could just alter the DIRCMD paramter:
Before you call the IsDIR 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
Additionally, you could just alter the DIRCMD paramter:
Code: Select all
SHELL "Set DIRCMD=/ogn /b /ad"
I have left this dump.
another way, just for fun
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:
(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.
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"
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
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.
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.
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.
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.
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
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 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.
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.
yes, i phrased that in a sloppy manner
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.
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.
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$.
*****
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.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Re: How to find out if a directory exists?
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.
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.