After I sent the message this morning I realized that it could (should?) be enhanced with one more return code to distinguish between the case where the directory already existed and where it did not exist but was created. This is very simple:
-- first test for existence
SET VAR pexist INTEGER = (CHKFILE(.pdirname))
IF pexist = 1 THEN
-- directory already exists
CLEAR VAR p%
RETURN 1
ENDIF
In the above block change the return to 2 and update the documentation.
Cool - thanks for this present, Emmitt!
David
David Blocker
[EMAIL PROTECTED]
781-784-1919
Fax: 781-784-1860
Cell: 339-206-0261
----- Original Message -----
From: Emmitt Dove
To: RBG7-L Mailing List
Sent: Tuesday, February 22, 2005 8:32 AM
Subject: [RBG7-L] - Re: Reliable Way to Check for a Network Folder?
Bob,
Here is a stored procedure I wrote that will check for the existence of a folder, create it if it does not exist, or tell you if it cannot create it. UNCs, relative paths and full mapped drive paths all work. It is self-documenting. You can also call it without a filename or with /? for help.
--- dchkdir.prc stored procedure to check for the existence of a directory,
--- and make it if not found
--- 05/17/04 Emmitt Dove/Blue Ridge Paper Products, Inc.
---
--- usage: set var vx INTEGER = (call dchkdir(.vdirname)) where vdirname is
--- the name of the directory to create if it does not exist
---
--- note: unc, absolute and relative pathnames acceptable
---
--- note: if only checking for the existence of a directory, use (chkfile())
--- built-in function
---
--- to create the stored procedure do this:
---
--- PUT dchkdir.prc AS dchkdir pdirname TEXT RETURN INTEGER +
--- COMMENT 'Directory check/make'
---
--- directory name will be in variable pdirname
---
--- procedure will return 1 if successful or directory already exists, 0 if
--- directory does not exist and could not be created
-- if help was requested or invalid input:
IF pdirname = (CHAR(32)) OR pdirname = '/?' OR pdirname IS NULL OR pdirname = '' THEN
SET VAR pmsg ='Procedure: dchkdir - check for existence of a directory'
SET VAR pmsg = (CTXT(.pmsg)&CTXT('and create')+CHAR(013))
SET VAR pmsg = (CTXT(.pmsg)&CTXT(' it if not found')+
+CHAR(013)+CHAR(013))
SET VAR pmsg = (CTXT(.pmsg)+CTXT('Usage: SET VAR vx INTEGER = (CALL')+
&CTXT('dchkdir(dirname))')+CHAR(013))
SET VAR pmsg = (CTXT(.pmsg)&CTXT(' where "dirname" is the name')+
&CTXT('of the directory to create')+CHAR(013)+CHAR(013))
SET VAR pmsg = (CTXT(.pmsg)+CTXT('Return: 0 = directory not found')+
&CTXT('and could not be created')+CHAR(013))
SET VAR pmsg = (CTXT(.pmsg)+CTXT('Return: 1 = directory found or')+
&CTXT('created'))
-- which version of R:Base are we running?
SET VAR pversion = (CVAL('version'))
SET VAR pversion = (SSUB(.pversion,3))
IF (SGET(.pversion,1,9)) = '7' THEN
PAUSE 2 USING .pmsg +
CAPTION 'Stored Procedure dchkdir Help' BUTTON 'OK' +
OPTION ICON_FILE icons\information.bmp|BACK_COLOR 8404992|+
MESSAGE_COLOR 8404992|MESSAGE_FONT_COLOR 16777215|+
BUTTON_COLOR 16747804|BUTTON_FONT_COLOR 16777215
ELSE
PAUSE 2 USING .pmsg=80
ENDIF
CLEAR VAR p%
RETURN 1
ENDIF
-- first test for existence
SET VAR pexist INTEGER = (CHKFILE(.pdirname))
IF pexist = 1 THEN
-- directory already exists
CLEAR VAR p%
RETURN 1
ENDIF
SET VAR pcmd = (CTXT('mkdir')&CTXT(.pdirname))
&pcmd
SET VAR pexist INTEGER = (CHKFILE(.pdirname))
IF pexist = 1 THEN
-- directory successfully created
CLEAR VAR p%
RETURN 1
ENDIF
-- directory not created; are there backslashes in the path other than
-- the first two characters? If so, pick the path apart and make each
-- subsequent directory
SET VAR pexist INTEGER = (SLOC(.pdirname,'\'))
IF pexist = 0 THEN
-- no backslashes; cannot do
RETURN 0
ENDIF
-- are the first or third characters a backslash?
IF pexist = 1 THEN
-- first is a backslash; is the second?
SET VAR ptest = (SGET(.pdirname,1,2))
IF ptest = '\' THEN
-- pdirname starts as a unc name; need to find position of second
-- following backslash since first part of unc name is server name and
-- second part is share name; cannot create directory which is server name
-- or share name
SET VAR ploc = 2
SET VAR plen = (SLEN(.pdirname))
SET VAR psuccess INTEGER = 1
SET VAR pcount INTEGER = 0
WHILE ploc < .plen THEN
SET VAR ploc = (.ploc + 1)
SET VAR ptest = (SGET(.pdirname,1,.ploc))
IF ptest = '\' THEN
IF pcount = 0 THEN
-- found first; this delimts server name
SET VAR pcount = 1
ELSE
-- found second occurrence after leading backslashes; this delimts
-- share name; set starting position one more
SET VAR pstartpos = (.ploc + 1)
BREAK
ENDIF
ENDIF
IF ploc = .plen THEN
-- got through entire unc without another backslash; invalid
-- entry
SET VAR psuccess INTEGER = 0
BREAK
ENDIF
ENDWHILE
IF psuccess = 0 THEN
RETURN 0
ENDIF
ELSE
-- only first character is a backslash;
SET VAR pstartpos = 2
ENDIF
ELSE
IF pexist = 3 THEN
-- third character is a backslash; is second a colon?
SET VAR ptest = (SGET(.pdirname,1,2))
IF ptest = ':' THEN
-- we apparently have a drive spec included
SET VAR pstartpos = 4
ELSE
SET VAR pstartpos = 1
ENDIF
ELSE
SET VAR pstartpos = 1
ENDIF
ENDIF
SET VAR ploc INTEGER = (.pstartpos - 1)
SET VAR plen = (SLEN(.pdirname))
SET VAR psuccess INTEGER = 1
WHILE ploc < .plen THEN
SET VAR ploc = (.ploc + 1)
SET VAR ptest = (SGET(.pdirname,1,.ploc))
IF ptest = '\' OR ploc = .plen THEN
-- backslash found; try creating directory to one less than position
-- unless loc = len
IF ploc = .plen THEN
SET VAR pdir = .pdirname
ELSE
SET VAR pdir = (SGET(.pdirname,(.ploc - 1),1))
ENDIF
SET VAR pexist INTEGER = (CHKFILE(.pdir))
IF pexist = 0 THEN
SET VAR pcmd = (CTXT('mkdir')&CTXT(.pdir))
&pcmd
SET VAR pexist INTEGER = (CHKFILE(.pdir))
IF pexist = 0 THEN
-- couldn't make directory
SET VAR psuccess = 0
BREAK
ENDIF
ENDIF
ENDIF
ENDWHILE
IF psuccess = 0 THEN
-- could not make directory
CLEAR VAR p%
RETURN 0
ENDIF
CLEAR VAR p%
RETURN 1
Can someone suggest a reliable way to check for the existence of a folder on a network?
I have been using (checkfile(filename)) to perform this test on the local machine. In this scenario, if filename is the name of a folder (such as C:\BobFolder) then the checkfile command will return 1 if the folder already exists.
However, when using this technique against a server folder using UNC (such as \\Server1\BobFolder) the checkfile command always seems to return 0 indicating that the folder does not exist.
Thanks.
Bob
Emmitt Dove
Manager, DairyPak Business Systems
Blue Ridge Paper Products, Inc.
40 Lindeman Drive
Trumbull, CT 06611
(203) 673-2231
[EMAIL PROTECTED]
[EMAIL PROTECTED]
Manager, DairyPak Business Systems
Blue Ridge Paper Products, Inc.
40 Lindeman Drive
Trumbull, CT 06611
(203) 673-2231
[EMAIL PROTECTED]
[EMAIL PROTECTED]
