Operating System - OpenVMS
1823471 Members
2376 Online
109660 Solutions
New Discussion юеВ

How do you determine if a directory is empty or not?

 
SOLVED
Go to solution
Kenneth Toler
Frequent Advisor

How do you determine if a directory is empty or not?

I am using a DCL COM file.

Is there a way to determine if files exist in a dirctory and return a "TRUE" or "FALSE" string?

If not, how can this be accomplished?
9 REPLIES 9
Robert Gezelter
Honored Contributor

Re: How do you determine if a directory is empty or not?

Kenneth,

Use the lexical function F$SEARCH("[directory]*.*") to locate any file in the directory. If it comes back with an empty string ("") then there were no files in the directory.

- Bob Gezelter, http://www.rlgsc.com
Kenneth Toler
Frequent Advisor

Re: How do you determine if a directory is empty or not?

Can you show me an example of how to use an IF statement with the F$SEARCH so that if there is a NULL string then it will execute a list of commands and if it is NOT a NULL string then it will execute another list of commands?
Robert Gezelter
Honored Contributor
Solution

Re: How do you determine if a directory is empty or not?

Kenneth,

$ IF F$SEARCH("[sssss]*.*) .EQS. ""
$ THEN
$ ...
$ ENDIF

There are also equivalent ways to express the IF using the F$LENGTH lexical function wrapping the F$SEARCH.

- Bob Gezelter, http://www.rlgsc.com
Kenneth Toler
Frequent Advisor

Re: How do you determine if a directory is empty or not?

What do you mean by "equivalent ways to express the IF using the F$LENGTH lexical function wrapping the F$SEARCH"? Could you give an example?.
Robert Gezelter
Honored Contributor

Re: How do you determine if a directory is empty or not?

Kenneth,

$ IF F$LENGTH(F$SEARCH(...)) .EQ. 0
$ THEN
$ ...
$ ENDIF

- Bob Gezelter, http://www.rlgs.com
Wim Van den Wyngaert
Honored Contributor

Re: How do you determine if a directory is empty or not?

If you execute the f$sea in a loop (e.g. every 5 minutes), you must initialize the f$sea context before each f$sea.
E.g.
x=f$sea("qqqqqq.qqqqqq")
x=f$sea("dir:*.*")
if x .eqs. "" ...

If not, f$sea will return each file of the directory (1 in each loop), then a blank ("") and then restart returning the files.

Wim
Wim
Antoniov.
Honored Contributor

Re: How do you determine if a directory is empty or not?

Kenneth,
if you like boolean value this is your example:
$ flag_empty=f$search("dir:*.*").eqs.""
$ if flag_empty
$ then
$ ... Directory empty
$ endif
$ ...

Antonio Vigliotti

Antonio Maria Vigliotti
John Gillings
Honored Contributor

Re: How do you determine if a directory is empty or not?

As some have pointed out here, there's a potential problem with search context. To make your code more robust, prevent it from interfering with other searches, and prevent resource leaks, it's best to use an unique search ID context for the search, and clear context both before and after use. Here's an example:

$ check_dir="disk:[mydir]"

$ search_context=='search_context'+1
$ ctx=search_context
$ tmp=F$SEARCH("clear",ctx) ! Clear to start
$ dir_empty=F$SEARCH("''check_dir'*.*",ctx).EQS.""
$ IF dir_empty
$ THEN
$ ! directory is empty
$ ELSE
$ tmp=F$SEARCH("clear",ctx) ! Clear at end
$ ENDIF

The global symbol "search_context" is used to generate a stream-id for the search. This code can safely execute within another F$SEARCH loop.
A crucible of informative mistakes
Hein van den Heuvel
Honored Contributor

Re: How do you determine if a directory is empty or not?


Ken wrote

"Is there a way to determine if files exist in a dirctory..."

yes, many

"... and return a "TRUE" or "FALSE" string?"

Yes, but returning a those strings is a little bit strange request. Most applications would look for a symbol set to true or false or (or perhaps 1 and 0 )

Anyway, just to give a completely different solution:

$directory = "XXX.DIR.1"
$string = "TRUE"
$open/read/share=write dir 'directory
$read/end=empty
$string = "FALSE"
$empty:
$close dir
$write sys$output "It is ", string, "that directory ", directory, " is empty.

grins,
Hein.

$write