CreateResFile checks to see if a resource file with a given name exists, and if it does, returns a dupFNErr (ΓÇô48) error. Unfortunately, to do this check, CreateResFile uses a call that follows the Poor ManΓÇÖs Search Path (PMSP).
CreateResFile checks to see if a resource file with a given name exists, and if it does, returns a dupFNErr (ΓÇô48) error. Unfortunately, to do the check, CreateResFile calls PBOpenRF, which uses the Poor ManΓÇÖs Search Path (PMSP). For example, if we have a resource file in the System folder named ΓÇÿMyFileΓÇÖ (and no file with that name in the current directory) and we call CreateResFile('MyFile'), ResError will return a dupFNErr, since PBOpenRF will search the current directory first, then search the blessed folder on the same volume. This makes it impossible to use CreateResFile to create the resource file ΓÇÿMyFileΓÇÖ in the current directory if a file with the same name already exists in a directory thatΓÇÖs in the PMSP.
To make sure that CreateResFile will create a resource file in the current directory whether or not a resource file with the same name already exists further down the PMSP, call _Create (PBCreate or Create) before calling CreateResFile:
err := Create('MyFile',0,myCreator,myType);
{0 for VRefNum means current volume/directory}
CreateResFile('MyFile');
err := ResError; {check for error}
In MPW C:
err = Create("\pMyFile",0,myCreator,myType);
CreateResFile("\pMyFile");
err = ResError();
This works because _Create does not use the PMSP. If we already have ΓÇÿMyFileΓÇÖ in the current directory, _Create will fail with a dupFNErr, then, if ΓÇÿMyFileΓÇÖ has an empty resource fork, CreateResFile will write a resource map, otherwise, CreateResFile will return dupFNErr. If there is no file named ΓÇÿMyFileΓÇÖ in the current directory, _Create will create one and then CreateResFile will write the resource map.
Notice that we are intentionally ignoring the error from _Create, since we are calling it only to assure that a file named ΓÇÿMyFileΓÇÖ does exist in the current directory.
Please note that SFPutFile does not use the PMSP, but that FSDelete does. SFPutFile returns the vRefNum/WDRefNum of the volume/folder that the user selected. If your program deletes a resource file before creating one with the same name based on information returned from SFPutFile, you can use the following strategy to avoid deleting the wrong file, that is, a file that is not in the directory specified by the vRefNum/WDRefNum returned by SFPutFile, but in some other directory in the PMSP:
VAR
wher : Point;
reply : SFReply;
err : OSErr;
oldVol : Integer;
...
wher.h := 80; wher.v := 90;
SFPutFile(wher,'','',NIL,reply);
IF reply.good THEN BEGIN
err := GetVol(NIL,oldVol); {So we can restore it later}
err := SetVol(NIL,reply.vRefNum);{for the CreateResFile call}
{Now for the Create/CreateResFile calls to create a resource file that
CreateResFile(&reply.fName);/*weΓÇÖll use the ResError from this ...*/
switch (ResError())
{
case noErr:;/*the create succeeded, go ahead and work with the new resource file -- NOTE: at this point, we donΓÇÖt know whatΓÇÖs in the data fork of the file!!*/
break; /* case noErr*/
case dupFNErr: /*duplicate file name error*/
/*the file already existed, so, letΓÇÖs delete it. WeΓÇÖre now sure that weΓÇÖre deleting the file in the current directory*/
err= FSDelete(&reply.fName,reply.vRefNum);
/*now that weΓÇÖve deleted the file, letΓÇÖs create the new one, again, we know this will be in the current directory*/