Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How-to Confirm if variable directory exists

Status
Not open for further replies.

steve4king

IS-IT--Management
Feb 6, 2007
154
US
I am writing a simple little utility, and in this utility, I am giving the user the option to enter a directory. It works fine unless the user selects a non-existent directory.

I was hoping I could just use code similar to t-sql "if exists.." but apparently that doesnt work.

I tried using "If DIRECTORY(G:\foldername)..." and it returned "Function name is missing"

I tried "dDir = DIRECTORY(G:\foldername)" also did not work.

What am I doing wrong? is there a better way to do this check? I just want the utility to exit gracefully if a nonexistant directory is entered.
 
Code:
local lcDir
lcDir = "g:\FolderName"
if directory(m.lcDir)
   * The directory exists
else
   md (m.lcDir)
endif

Note, that you may find that directory exists, but you may not have rights to write into it.
 
Check in Help DIRECTORY( ) function and nFlags parameter.

You can use the ADIR( ) function to determine specific attributes for the directory.


 
Did check help, and I am using directory() as specified I think..

I don't know how I would utilize ADIR() to accomplish my needs. (isn't directory() exactly what I want?)

Getdir actually looks good Mike!, really prefer text entry(faster than navigating) but great second option!

Ilyad, the only differences between what I was doing and what you did, was "local lcdir" and for testing purposes, I was not using a variable. example:

Code:
if directory("g:\foldername")
   *whatever functions
else
   messagebox("Directory does not exist, retry")
endif

Why should this code not work? vs yours ilyad?

Thanks,
Steve
 
Your code should work as is, I don't see a problem. It may be a problem, if you try to access non-existing drive, but I think directory() function is smart enough to catch it too.

What is your VFP version?
 
Steve,

I tried using "If DIRECTORY(G:\foldername)..." and it returned "Function name is missing"

You need to change G:\foldername to a variable that contains the folder name. Either that, or pass the actual folder name in quotes. Either:

lcFolder = <whatever the user entered>
IF DIRECTORY(lcFolder) ...

or:

IF DIRECTORY("G:\MyFolder")

really prefer text entry(faster than navigating)

Agreed, but why not do both. Give them a text box for typing the folder, plus a button that does GETDIR(). That way, they get the choice.

Better still, use Barbara Peisch's cmpSelDir class, which does the whole thing for you (see
Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
version 9, ugh.. I dont know what I was doing wrong earlier.. The code I just typed in is working fine.

By the way.. what does "local lcDir" do?
 
local lcDir simply declares local variable named lcDir. Check LOCAL in Help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top