[comp.sys.mac.programmer] Deleting files from a disk

tim@hoptoad.uucp (Tim Maroney) (05/03/90)

In article <374@taurus.bucknell.edu> fikree@hydra.bucknell.edu (Mateen Fikree)
writes:
>I need to know how to delete a file from the disk whose vRefNum I know (i.e.
>more specifically I will be deleting the file from a hard disk whose
>vRefNum I determine while checking the volume queue to determine whether a
>hard disk is present).  The file that I need to delete is stored with its
>full path name (e.g. HardDisk:Think C:Development:filename )  in a array
>of size 255.  I need to delete the file with the name filename from the
>hard disk.  Anyone have any ideas as to how I can do this?
>
>Please reply by e-mail.  Thanks to all who reply.

I did respond to your other question by e-mail, but this code is probably
of interest to a wider crowd.  The answer is to call FullToTriplet on
your full file name, then pass the volume reference number, directory id,
and trailing path name to PBHDelete.

FullToTriplet(StringPtr f, StringPtr tail, short *volume, long *folder)
{	CInfoPBRec di; HVolumeParam hvp; short i; Str255 full;
	BlockMove(f, full, f[0] + 1);
	for (i = full[0]; i > 0 && full[i] != ':'; i--) ;
	if (i <= 0) return bdNamErr;
	BlockMove((Ptr)(full + i + 1), (Ptr)(tail + 1), full[0] - i);
	tail[0] = full[0] - i;
	full[0] = i;
	di.dirInfo.ioNamePtr = full;
	di.dirInfo.ioVRefNum = 0;
	/* di.dirInfo.ioVersNum = 0; */
	di.dirInfo.ioFDirIndex = 0;
	di.dirInfo.ioDrDirID = 0;
	PBGetCatInfo(&di, false);
	if (di.dirInfo.ioResult) return di.dirInfo.ioResult;
	*folder = di.dirInfo.ioDrDirID;
	for (i = 1; i < full[0] && full[i] != ':'; i++) ;
	full[0] = i;
	hvp.ioNamePtr = full;
	hvp.ioVRefNum = 0;
	hvp.ioVolIndex = -1;
	PBHGetVInfo((HParmBlkPtr)&hvp, false);
	*volume = hvp.ioVRefNum;
}

For convenience, you can call my front-end to PBHDelete, DeleteTriplet:

DeleteTriplet(StringPtr name, short volume, long folder)
{	HFileParam hfp;
	hfp.ioNamePtr = name;
	hfp.ioVRefNum = volume;
	hfp.ioDirID = folder;
	return PBHDelete((HParmBlkPtr)&hfp, false);
}
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com

"God must be a Boogie Man." -- Joni Mitchell