Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Checking for a locked file
Message
From
26/04/2005 02:45:41
 
 
To
25/04/2005 10:53:54
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 7 SP1
OS:
Windows 2000 SP4
Miscellaneous
Thread ID:
01008092
Message ID:
01008349
Views:
21
>Hello,
>I was wondering if anybody had a good way to check for a locked file? I am moving a list of files, and can't have the error message pop up when I don't have access to one of them. Thanks for the help!
>
>MAC

Mike,

If a file is any file
if locked/no access means you cannot move it

then fopen() will not work (if a file is readonly)

then fopen() will work for excel/word files that are open, you can copy them, but not move/delete them

The only realiable method I have found is to try opening a file for exclusive readonly access
*--------------------------------------------------------------------------
#define TRUE .t.
#define FALSE .f.
#define T_HANDLE	Integer
#define LPCTSTR	String @
#define	DWORD	integer

#define	LPSECURITY_ATTRIBUTES	string @

declare T_HANDLE CreateFile in Kernel32.dll ;
		LPCTSTR FileName, ;
		DWORD dwDesiredAccess, ;
		DWORD dwShareMode, ;
		LPSECURITY_ATTRIBUTES lpSecurityAttributes, ;
		DWORD dwCreationDisposition, ;
		DWORD dwFlagsAndAttributes, ;
		T_HANDLE hTemplateFile

declare integer CloseHandle in win32api long Handle
declare Integer GetFileAttributes in win32api string @

#define	FILE_ATTRIBUTE_READONLY			0x00000001
#define	FILE_ATTRIBUTE_HIDDEN			0x00000002
#define	FILE_ATTRIBUTE_SYSTEM			0x00000004
#define	FILE_ATTRIBUTE_DIRECTORY		0x00000010
#define	FILE_ATTRIBUTE_ARCHIVE			0x00000020
#define	FILE_ATTRIBUTE_ENCRYPTED		0x00000040
#define	FILE_ATTRIBUTE_NORMAL			0x00000080
#define	FILE_ATTRIBUTE_TEMPORARY		0x00000100
#define	FILE_ATTRIBUTE_SPARSE_FILE		0x00000200
#define	FILE_ATTRIBUTE_REPARSE_POINT		0x00000400
#define	FILE_ATTRIBUTE_COMPRESSED		0x00000800
#define	FILE_ATTRIBUTE_OFFLINE			0x00001000
#define	FILE_ATTRIBUTE_NOT_CONTENT_INDEXED	0x00002000

#define	GENERIC_READ	(0x80000000)
#define	GENERIC_WRITE	(0x40000000)
#define	GENERIC_EXECUTE	(0x20000000)
#define	GENERIC_ALL	(0x10000000)

#define	CREATE_NEW		1
#define	CREATE_ALWAYS		2
#define	OPEN_EXISTING		3
#define	OPEN_ALWAYS		4
#define	TRUNCATE_EXISTING	5

#define INVALID_HANDLE_VALUE		(-1)
#define INVALID_FILE_SIZE			(-1)
#define INVALID_SET_FILE_POINTER	(-1)
#define INVALID_FILE_ATTRIBUTES		(-1)

function FileOpened( FileName )

	local Handle
	
	Handle = CreateFile( ;
				@FileName, ;
				GENERIC_READ, ;
				0, ;				&& excl access
				0, ;
				OPEN_EXISTING, ;
				FILE_ATTRIBUTE_NORMAL, ;
				0 ;
			)
	
	do case
	case (m.Handle = INVALID_HANDLE_VALUE )
		return FileExists(m.FileName)
		
	otherwise
		=CloseHandle(m.Handle)
		return FALSE
	
	endcase
	
	assert FALSE
endfunc
*--------------------------------------------------------------------------
*--------------------------------------------------------------------------
Function	FileExists(FileName)
	return (GetFileAttributes(@FileName) <> INVALID_FILE_ATTRIBUTES)
endfunc
*--------------------------------------------------------------------------
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform