View Full Version : unlock a file
Hi everybody,
My script is using a random acces file and sometimes it needs to be closed and deleted. But the problem is that sometimes it can't be deleted because the file is locked by windows for some reason. I have downloaded a little unlock-tool which works great. But I was wondering if it's also possible to unlock a file with ThinBasic code. Does anybody knows how to do that?
Martin
Charles Pegge
05-09-2009, 18:06
Hi Martin,
Do you use any locks on this file yourself or via a module?
Does the problem persist if you change the file extension name?
Charles.
Hi Charles,
First I close the file with File_Close, this works fine, no errors. So there's no lock from my script (as far as I know).
Then I use File_Kill to delete the file, it returns error code 70.
I have the same problem with renaming the file: error code 58.
If I unlock the file with "unlocker assistent" (freeware tool), File_Kill works fine.
I am going to do some tests to find out why it doesn't always work
Charles Pegge
05-09-2009, 21:38
The problem almost certainly lies with File_Close which does not report errors as far as I know.
Is your file handle still valid? You can check its value.
Charles
Michael Clease
05-09-2009, 23:46
Eros posted his file handling routines a while back and here they are they dont use the lock function that is in the PB help files but he did say the routines are quite old. But I suspect its something to do with antivirus though.
heres the PB info, look at the Open statement http://www.powerbasic.com/support/help/pbwin/index.htm
FUNCTION File_Exists(BYVAL FullFileName AS STRING) AS LONG
FUNCTION = %FALSE
IF DIR$(FullFileName, %NORMAL OR %READONLY OR %HIDDEN OR %SYSTEM) = "" THEN EXIT FUNCTION
FUNCTION = %TRUE
END FUNCTION
FUNCTION FILE_Load(BYVAL lFileName AS STRING) AS STRING
LOCAL hFile AS LONG
LOCAL lBuffer AS STRING
ON ERROR RESUME NEXT
IF FILE_EXISTS(lFileName) THEN
hFile = FREEFILE
OPEN lFileName FOR BINARY ACCESS READ AS hFile
GET$ hFile, LOF(hFile), lBuffer
CLOSE hFile
FUNCTION = lBuffer
END IF
END FUNCTION
FUNCTION FILE_Save(BYVAL lFileName AS STRING, BYVAL sBuffer AS STRING) AS LONG
LOCAL hFile AS LONG
ON ERROR RESUME NEXT
IF FILE_EXISTS(lFileName) THEN
KILL lFileName
END IF
hFile = FREEFILE
OPEN lFileName FOR BINARY ACCESS READ WRITE AS hFile
PUT$ hFile, sBuffer
CLOSE hFile
FUNCTION = ERRCLEAR
END FUNCTION
The problem almost certainly lies with File_Close which does not report errors as far as I know.
Is your file handle still valid? You can check its value.
Charles
I don't know, i had no problems anymore. Also I found a better way to do what I want (deleting records from random acces file) so this problem will not happen again hopefully.
Thanks for your comments!
Martin