You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
The statement, as posted, will delete the file you are currently in or, if you are in the "D:" drive, the "C:" partition currenty accessed by your program.
If the file is in the root directory, add a backslash, i.e., "KILL c:\ver8.txt".
There are no atheists in a fox hole or the morning of a math test.
If my flag offends you, I'll help you pack.
You could have figured out what's wrong by querying ERR after KILL. You should get a 53 file not found error, which surely would have pointed you in the right direction.
so I had the slash. I did the error trap and it returned error 70
"You tried to write to a write-protected disk. This error can also be generated as a result of network permission errors, such as accessing a locked file, or a locked record. It can also occur when attempting to open a subdirectory as a file."
Within the program I create and write to this file:
OPEN "c:\pclink4\nb_ver8.txt" FOR BINARY LOCK SHARED AS 9
and specifically close it
CLOSE #9
I later open it again:
OPEN "c:\pclink4\nb_ver8.txt" FOR INPUT LOCK SHARED AS 4
Steve,
If your program crashed while the file was still locked, it may not have been closed.
Either reboot your machine to clear the lock, or use something like "unlocker" to force the release of the file handle.
Cheers
Gary Barnes
The Control Key
If you are not part of the solution
then you are either a gas, solid, plasma or some other form of matter.
If CLOSE fails, that should return an ERR value, too. Worth checking anyway.
Also:
CLOSE is a "when I get around to it" operation with Windows; so the CLOSE may not have been done by the time the KILL was issued. Windows is "supposed" to handle this but maybe you need to code a delay/retry loop on the KILL, giving up after some number of tries and reporting inability to KILL the file. Heck, that's worth a try as well.
"Lazy Writes" (all file activity for that matter) have been a characteristic of the NT based OS's since 3.1 (the first version) and was always a characteristic of VMS, the "mother" of NT.
Written as a SUB to make checking result more likely and has optional timeout
Code:
SUB Killer(sFileName AS STRING, result AS LONG, OPTIONAL BYVAL Time_Out AS LONG)
LOCAL PausePerLoopInMilliseconds AS LONG
LOCAL TimeWaited AS LONG
PausePerLoopInMilliseconds = 100
DO
ERRCLEAR
KILL sFileName
IF ERR = 0 OR ERR = 53 THEN EXIT DO
IF TimeWaited => Time_Out THEN EXIT DO
SLEEP PausePerLoopInMilliseconds
TimeWaited = TimeWaited + PausePerLoopInMilliseconds
LOOP
result = ERR
END SUB
Create or use a file which you want deleted when you are done with it.
Not tested, not even compiled.
May be worth playing with for some applications... and can be played around with, a lot.
MCM
Code:
' ---------------------------------------------------
' OPEN A FILE Which will be deleted on close
' when closed with TempFile_Close
' ----------------------------------------------------
' returns: hFile, a PB handle ready for use in specified mode; %NULL on failure
' modes: "BINARY","RANDOM", "INPUT"
' opening for Output makes no sense since file will be deleted as soon as it's closed.
FUNCTION Tempfile_Open ( szFilename AS ASCIIZ, OpenMode AS STRING, OPTIONAL RecLen AS LONG) AS LONG
LOCAL dwAccess as DWORD, dwShare AS DWROD, lpSecurity as DWORD, dwCreate AS DWORD, hTemplate AS DWORD
LOCAL dwFlags AS DWORD, hSYS AS DWORD
dwFlags = %NULL ' initialize
SELECT CASE lcase$(OpenMode)
CASE "random"
dwAccess = %GENERIC_READ OR %GENERIC_WRITE
dwFlags = %FILE_FLAG_RANDOM_ACCESS
dwCreate = %OPEN_ALWAYS
CASE "binary"
dwAccess = %GENERIC_READ OR %GENERIC_WRITE
dwCreate = %OPEN_ALWAYS
CASE "input"
dwAccess = %GENERIC_READ
dwCreate = %OPEN_EXISTING
END SELECT
dwShare = %NULL ' temp/work file, no sharing needed.
lpSecurity = %NULL ' or VARPTR (Security_Attributes var)
dwFlags = dwFlags OR %FILE_FLAG_DELETE_ON_CLOSE ' so it's deleted on close
hTemplate = %NULL ' because it has to be %NULL
hSys = CreateFile (szFilename, dwAccess, dwShare,BYVAL lpSecurity, dwCreate, dwFlags, hTemplate)
IF hSys <> %INVALID_HANDLE_VALUE THEN ' open Succeeded
hFile = FREEFILE ' get PB handle and open
SELECT CASE lcase$ (OPenMOde)
CASE "binary"
OPEN HANDLE hSys FOR BINARY AS hFile
CASE "random"
OPEN HANDLE hSys FOR RANDOM AS hFile LEN=RecLen
CASE "input"
OPEN HANDLE hSys FOR INPUT AS hFile
END SELECT
ELSE
' Oops open failed!
hfile = %NULL
END IF
FUNCTION = hFile
END FUNCTION
FUNCTION TempFile_Close (BYVAL hFile AS LONG) AS LONG
LOCAL hSys AS LONG
hSys = FILEATTR (hFile, 2) ' get system handle for this PB handle
CLOSE hFile ' close PB handle
CloseHandle hSys ' close underlying system handle, which will delete the file
END FUNCTION
FUNCTION whatever...
LOCAL szPrefix AS ASCIIZ * 4 ' only three characters max are ever used
LOCAL szTempFile AS ASCIIZ * %MAX_PATH
szPrefix = "~mm" ' see support function below
CALL GetTempFileName(szPrefix) TO szTempFile
OpenMode = "BINARY"
hFile = TempFile_OPen (szTempFile, openmode)
....
' ----------------------------------------------------------
' Do stuff with the temp file using the supported verbs
' for BINARY opens: SEEK, PUT, PUT$, GET, GET$
' ---------------------------------------------------------
TempFile_Close hFile ' done with workfile, close with auto-delete
....
'' ----- FOR SEQUENTIAL WORKFILE -------
' Create a file ready to write to
szPrefix = "~sw"
CALL GetTempFileName (szPrefix) TO szTempFile
' -----------------------------------------
' cannot open for autodelete, since we won't be able to get our data back!
' so use standard PB open:
hFile = FREEFILE
OPEN szTempFile FOR OUTPUT AS hFIle
-------------------------------------
' write work records to file using supported verbs
' for OUTPUT opens: PRINT#, WRITE#
' ------------------------------------
CLOSE hFile ' will not be deleted
' Read back the file, opening for auto-delete
OpenMode = "INPUT"
hFile = TempFile_open (szTempFile, openMode)
'-----------------------------------------------
' read back the data using supported verbs
' for INPUT opens: LINE INPUT#, INPUT#
' -----------------------------------------------
TempFile_Close hFile ' close with auto-delete
END FUNCTION
' ------------------------------------------------------------------------
' Get a uniquely-named file in the user's designated temporary directory
' ------------------------------------------------------------------------
FUNCTION GetTempFileName (szPrefix AS ASCIIZ) AS STRING
' returns: Name of temp file to use (fully-qualified); in user's "temp" directory
' other = System error number
LOCAL szTempPath AS ASCIIZ * %MAX_PATH, sTempFile AS STRING
LOCAL iStat AS LONG
sTempFile = "" default
' get user's designated temp folder:
iStat = GetTempPath(SIZEOF(szTempPath), szTempPath)
IF ISTRUE iStat THEN ' GetTempPathSucceeded
sTempFile = STRING$(0, %MAX_PATH) ' create buffer for name
' get a unique file name in that folder
iStat = GetTempFileName (szTempPath, szPrefix, 0&, BYVAL STRPTR(sTempFile)
IF ISTRUE iStat THEN ' succeeded
FUNCTION = RTRIM$(sTempFile, $NULL) ' remove excess nulls from string
ELSE ' GetTempFilename failed
sTempFile = "" ' oops, reset!
END IF
END IF
FUNCTION = sTempFile ' return name (null string on failure)
END FUNCTION
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment