Your SAS programs, embedded in web apps and elsewhere

How to check if a directory is writblae or not?

Reply
N/A
Posts: 1

How to check if a directory is writblae or not?

Does anybody know if there is a way in SAS coding to check a given
directory is writable or not ? I used Fopen( ) and Fwrite( ) functions, but they seem only work on a file not a directory.
Thanks.
N/A
Posts: 0

Re: How to check if a directory is writable or not?

The "writability" of a directory may be limited by a ReadOnly flag which is a file system attribute, or the permissions of the user, which is a security system attribute.

It is possible to use Windows APIs to retrieve security attributes, but the method requires a little work and may not work on all implementations of Enterprise Guide. I found my API calls transferred transparently, but I didn't expect it to be as easy since this is a client / server model and server implementations are often limited for very good reasons.

If you are able to create a file in a directory with the SCL functions you specified, then surely that means you can write to the directory, or have I missed something obvious? Can you not create a dummy file name, get a listing of files in the directory, verify the file does not exist, then create and open it to show whether you can or cannot write in the directory? It sounds like an eminently worthwhile candidate for a macro once you get the code right.

Kind regards

David
N/A
Posts: 0

Re: How to check if a directory is writable or not?

I once wrote a macro to prove if a path was "writeable". It would try writing to the path using FOPEN(), FWRITE() etcetera functions and test return codes ~but I can't find that macro now!

Anyway, that was before SAS9 introduced the DCREATE function.
This snip from online doc indicates a simpler approach is possible.

new-directory=DCREATE(directory-name<,parent-directory>)
Arguments
new-directory
contains the complete pathname of the new directory, or contains an empty string if the directory cannot be created.

full details at http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a002332693.htm.

If "new-directory" is not blank, then you can create a directory, and that folder is writeable. (Just make sure the "new-directory" does not exist before executing that function ;-) )


PeterC
N/A
Posts: 0

Re: How to check if a directory is writable or not?

That is a great function Peter.

On first execution:

14 Data _NULL_;
15 RC = DCreate( "Test1", "C:\Temp");
16 Put "Return code from DCreate is " RC;
17 Run;

Return code from DCreate is C:\Temp\Test1


On second execution:

14 Data _NULL_;
15 RC = DCreate( "Test1", "C:\Temp");
16 Put "Return code from DCreate is " RC;
17 Run;

Return code from DCreate is


So the existence of the directory doesn't seem to put the directory at risk of being replaced which is a robust solution. I especially like the return code which lends itself to being stored in a macro symbol for resolution into a path or file reference elsewhere in the code. I am especially thinking of the situation where the path is derived from some date or calculated parameter, and one need then only calculate it once.


All my path creations are done using a macro that invokes the Windows API CreateDirectoryA which has the benefit of return codes from the Windows OS. These are richer than the Go / NoGo result you get from the SAS SCL functions, including "Already exists" and "Path not found". It also permits defining a security descriptor if you want to. However, it does require some infrastructure.

Kind regards

David
Ask a Question
Discussion stats
  • 3 replies
  • 297 views
  • 0 likes
  • 2 in conversation