Computing.Net > Forums > Unix > How do I check if a directory exists?

How do I check if a directory exists?

Reply to Message Icon

Original Message
Name: Vikram
Date: March 6, 2001 at 07:34:32 Pacific
Subject: How do I check if a directory exists?
Comment:

I am writing a program in Java that creates directories in the UNIX platform (I am using the Runtime.exec("...") call, for wihch my parameter will be a UNIX command such as mkdir, rmdir etc.

Now waht I want to do is, first ask a user where he wishes to store the files, and check if that directory exists. If it does exist, then I just save those files there (I have the code for the saving bit), else I create the directory and then save the files in there. So I first need to check the validity and the existence of the directory the user wishes to create. IS there a UNIX command that I can use to see if a directory exists?


Report Offensive Message For Removal


Response Number 1
Name: Siddharth
Date: March 6, 2001 at 15:40:51 Pacific
Subject: How do I check if a directory exists?
Reply: (edit)

From withing Java , you can use java.io.File to check if the directory exists and create it if it doesnt.


Report Offensive Follow Up For Removal

Response Number 2
Name: Wolfram Kraft
Date: March 7, 2001 at 00:11:51 Pacific
Subject: How do I check if a directory exists?
Reply: (edit)

hi,

a simple unix-command would look like

test -d /directory && action

test checks, if /directory exist and if it is a directory (switch -d).
the construct "&& action" is executed, if the test results in ok (returncode 0)
see the man-page for test.

greet$
Wolfie


Report Offensive Follow Up For Removal

Response Number 3
Name: Glen Coakley
Date: March 20, 2001 at 15:15:41 Pacific
Subject: How do I check if a directory exists?
Reply: (edit)

Why check just
mkdir -p directory


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How do I check if a directory exists?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software