![]() |
DISK I/OManage the temporary folder
Here is some FBII-code I adopted following the recommendations of Apple-IM.
Please note that FINDFOLDER(...) works for all volumes, not only for the system volume!
' -- FileSystSpecification record (IM/Files/2-86):
DIM RECORD fsSpec
DIM fsVRefNum%
DIM fsParID&
DIM 63 fsName$
DIM END RECORD .fsSize
' -- 70 bytes
' FSS record of temporary file:
DIM tmp.fsSize
'**********************************************************
LOCAL MODE
' (IM/Files/2-166)
LOCAL FN FSMakeFSSpec(vRefNum%,dirID&,@fileName&,specPtr&)
'
` clr.w -(sp) ; room for result:
osErr%
` move.w ^vRefNum%,-(sp) ; volume reference number
` move.l ^dirID&,-(sp) ; (parent) directory ID (of
FILE)
` move.l ^fileName&,-(sp) ; Ptr to file or pathname
` move.l ^specPtr&,-(sp) ; Ptr to FileSystemSpecification
RECORD
` dc.w $7001,$AA52 ; selector, A-trap
` move.w (sp)+,D0 ; D0 := osErr%
` ext.l D0 ; make long integer (FB
convention)
END FN
' result codes 0,-35,-43
'***********************************************************
'Code Snippet:
'(1) Give a unique name to the temporary file:
' file name longer than maximum?
LONG IF LEN(origName$)>26
' calculate ellipsis:
origName$ = LEFT$(origName$,13)+" --- "+RIGHT$(origName$,12)
END IF
' temporary file name:
tmpName$ = origName$ + ".temp"
'(2) Find temp-folder on your file's volume with vRefNum%
'or create it if necessary (details: IM/Files/1-25):
err%=FN FINDFOLDER(vRefNum%,_"temp",_kCreateFolder,tmpVRefNum%,tmpParID&)
' FSS of temp-file:
IF err%=_noErr THEN err%=FN FSMakeFSSpec(tmpVRefNum%,tmpParID&,tmpName$,@tmp)
' fnfErr: file must be created:
LONG IF err%=_noErr OR err%=_fnfErr
' reset error:
err% = _noErr
' create temp-file:
err% = FN FSpCreate(tmp,_"trsh",_"trsh",_smSystemScript)
END IF
'If this code snippet does not work reliable,
'try _pTrue instead of _kCreateFolder.
'Do not forget to delete the temporary file when your finished!
'***********************************************************
I intensively use the above code without any problems.
osErr% = FN FINDFOLDER (_kOnSystemDisk,_KtemporaryFolderType,0, prefVRefNum%, prefDirID&)Wait a minute. You are specifying 0 for the createFolder parameter. That equates to false - meaning, "Don't create the folder I am looking for if it doesn't exist". If you want to always find the folder, you should be using _true (or non-zero) for that parameter. David (the other one) was mostly right. I had tried the above line with the createFolder set to _kcreateFolder (which the constant tool says equates to 1) and it made no difference. However, if you set the createFolder parameter to _zTrue (-1) then the above call always works. It appears that there is some problem here. IM says the createFolder parameter should be set to true, to create a folder if one does not exist. http://developer.apple.com/techpubs/macos8/Files/FolderManager/FolderMgrRef/frameset.html All that matters however, is that if you want be be certain that a temp folder will be found - set createFolder to _zTrue not _true or _kcreatefolder PS These OS 8 documents indicate some really neat stuff is now possible with FINDFOLDER and related calls. The problem is that different development environments or languages evaluate their true constant differently. FutureBasic evauates _true to 1 (I believe), whereas Pascal and C evaluate true to -1. _ztrue in FB II is -1, which is why it works. Good catch, David! =) Glad you got it figured out. This is not quite right - from IM and a LOT of testing, it appears that with OS 8.5.1 there is not necessarilly a Temporary Items folder straight after start-up. If you search for one with say DiskTools (thanks Tedd) you wont always find one - this is why FINDFOLDER when it is set to "dont create a folder if you dont have one" might return an error -43. It is true that you should not delete the folder in your app, and everything else Mark said is correct. Thus if any application creates a Temporary items folder, it will be available to all other apps, you however cannot assume that one has been created when your app starts. I suspect this behaviour has only recently changed, and it is possible that some extensions and/or previous systems do create the folder at start-up (or never remove it). The problem is the old Pascal true = -1 Basic true = 1. The constant _kcreateFolder defined in FB is incorrect, use _zTrue instead, and you can happily use this feature. FINDFOLDER should not not create a folder if one exists (though I think in my testing I created a phantom system folder - which was very bad news). You will run into problems, as Mark suggests, if you try to create the folder by any means other than a call to FINDFOLDER. David
|