Written by

Sr Application Development Analyst at The Ohio State University Wexner Medical Center
Question Scott Roth · Nov 22, 2016

Cache Syntax - Directory Exists

I am trying to write some code to check to see if a directory exists before creating a new directory.

When I do the following I am not getting a response, but the directory exists..

do ##class(%File).DirectoryExists("/ensemble/")

am I missing something?

Comments

Dmitry Maslennikov · Nov 22, 2016

How do you expect to get the response in your command?

If you read documentation carefully, you will find:

classmethod DirectoryExists(filename As %String) as %Boolean

Tests if filename is a directory. returns 1 if it is a directory, otherwise, returns 0.

So, you should set the result of this function to some value.

set result=##class(%File).DirectoryExists("/ensemble/")
0
Scott Roth  Nov 22, 2016 to Dmitry Maslennikov

if I run it from the command line I was expecting for it to return me a 1. Is that not possible?

0
Pete Greskoff  Nov 22, 2016 to Scott Roth

You aren't actually writing a response. To get the result in the terminal, you'd need to 'write' the output instead of 'do'.

0
Scott Roth  Nov 22, 2016 to Pete Greskoff

I found by using %Library.File it worked better.

w ##class(%Library.File).DirectoryExists("/ensemble/")
1
w ##class(%Library.File).DirectoryExists("/ensemble/Scott/")
1
w ##class(%Library.File).DirectoryExists("/joe")
0
w ##class(%Library.File).DirectoryExists("/joe/")
0

Thanks for your help.

0
Alexey Maslov  Nov 22, 2016 to Scott Roth

Did it work better than what?

Both classes are really the same one, as %Library is the default package name for %-classes.

0