Operating System - OpenVMS
1748136 Members
3551 Online
108758 Solutions
New Discussion юеВ

Re: Checking if filename contains a directory specification

 
SOLVED
Go to solution
Jeff Bath
Advisor

Checking if filename contains a directory specification

I am wondering if there is a simpler way to determine if a filename contains a device and/or directory specificaton from DCL. Currently I use something similiar to what you see in the attachment but it doesn't work for all cases. For instance if I want to check if the filename has a device name specified.

I would like to use it to determine if an input parameter include a device or directory if not then it will look in a default directory defined by some logical name.

Thanks
J Bath
9 REPLIES 9
Steven Schweda
Honored Contributor
Solution

Re: Checking if filename contains a directory specification

If I neded to do it, I might use SYNTAX_ONLY
with an unlikely default file specification:

$ ws = "WRITE SYS$OUTPUT"
$ f = f$edit(p1,"COLLAPSE,UPCASE")
$ nsdev = "NO_SUCH_DEV:"
$ nsdir = "[no_such_dir]"
$ nsdevdir = nsdev+ nsdir
$ dev = f$parse( f, nsdevdir, , "DEVICE", "SYNTAX_ONLY")
$ dir = f$parse( f, nsdevdir, , "DIRECTORY", "SYNTAX_ONLY")
$ if (dev .eqs. nsdev)
$ then
$ dev = "(none)"
$ endif
$ ws "Device = ''dev'"
$ if (dir .eqs. nsdir)
$ then
$ dir = "(none)"
$ endif
$ ws "Directory = ''dir'"
$!

But it may be that you could simply use
F$PARSE with your logical name as the default
file spec.
Hein van den Heuvel
Honored Contributor

Re: Checking if filename contains a directory specification

The script you suggests fails if it is provided a filespace with version number, or just a ";".

Can we safely ignore ODS-5 extended file naming?

If not then you really should go the F$PARSE route. Else, You could just do

$test = f - "]" - ">" - ":"
$if test.nes.t then ws "Device or Directory present".

But I would like to echo Steven's closing suggestion: Just let F$PARSE do all the work. Let it combine the filespec with the missing pieces provided as 'default name'.
That's what it is there for.

Now if you wanted to do this in a program, not DCL, and wanted to roll your own, then you should look at the $FILESCAN system service.


Hein.

Robert Gezelter
Honored Contributor

Re: Checking if filename contains a directory specification

Jeff,

Please use the F$PARSE lexical function. It is guaranteed to work (you can limit the processing as noted by using the SYNTAX_ONLY parameter).

You can also use multi-level defaulting when using F$PARSE. This allows you to specify default elements (e.g. logical names, directories) for things in a manner consistent with all of the other system utilities.

It is NEVER recommended to parse things yourself. On occasion, changes appear that can break manual filename parsing (e.g. ODS-5). The lexical function is guaranteed to work correctly. Manual code is guaranteed to have to be maintained over the years.

A good general rule is that it is worth the time and effort to use a provided facility (e.g. F$PARSE) than to write one's own code.

- Bob Gezelter, http://www.rlgsc.com
EdgarZamora_1
Respected Contributor

Re: Checking if filename contains a directory specification

Hey Jeff. The following simple code will tell you if the device or directory are specified and are valid. That's all it's checking for.

$ IF F$PARSE(P1,"BOGUS:[BOGUS]",,,) .EQS. "" THEN WRITE SYS$OUTPUT "INVALID"

Change BOGUS to something else if BOGUS is a valid device/dir at your place.
Hein van den Heuvel
Honored Contributor

Re: Checking if filename contains a directory specification

Edgar,

That will not work if filespec does provide a device or directory, but one which is not valid (yet).
It will also return a blank string.

The question was:
" Checking if filename contains a directory specification"

It did not indicate whether that directory needed to be valid, allthough admittedly that typically is expected.
- the final file spec may need to be use in a different context, maybe a different system.
- the file, directory or logical name for the device might not yet exist
Agreed, unlikely.


I normally solve it like Steven already suggested.
Provide a recognizable bogus directory / device as default name and see if they come back after a SYNTAX_ONLY' parse.

Hein.
EdgarZamora_1
Respected Contributor

Re: Checking if filename contains a directory specification

Hein, I agree with what you said. That's why I said it would work only for valid (and by valid I meant "existing") device and directory.
Hoff
Honored Contributor

Re: Checking if filename contains a directory specification

I posted up some brief comments on the parsing of file specs yourself about a month or so back, over in http://64.223.189.234/node/71 and I've added a few more cases where this gets "fun".

It isn't trivial to get all the cases, and the effort becomes increasing non-trivial when the potential exists for changes in the parsing rules in future releases.
Jeff Bath
Advisor

Re: Checking if filename contains a directory specification

Thanks for all of your help. I guess I never took the time to really understand all of the features of f$parse. Now I can see how simple this really should be.
Jan van den Ende
Honored Contributor

Re: Checking if filename contains a directory specification

Jeff,

please review

http://forums1.itrc.hp.com/service/forums/helptips.do?#33

on how to express thanks in these forums.

Proost.

Have one on me.

jpe
Don't rust yours pelled jacker to fine doll missed aches.