The following are three subroutines you can use whenever
you need to create, update, uppend to or delete files
and/or directories in the environment where end user rights
are restricted to Read-Only:
Therefore, if you need to, say, create a table
and fill it with some data, here's what you can do:
I hope this would help you to get around the user rights
restriction issue.
Regards,
Ilya
you need to create, update, uppend to or delete files
and/or directories in the environment where end user rights
are restricted to Read-Only:
Code:
************************************************************
FUNCTION GetFileAttribInfo(tcFilePath)
************************************************************
** Function Name : Get File Attribute Info
** Purpose : Get file's in ? attributes.
** Description : Wrapper around Win32API function GetFileAttributes()
** Parameter(s) : Full path to the file/directory in ? as string.
** Return : File attribute as Numeric (short integer).
** Side Effect(s): None.
** Notes : FILE_ATTRIBUTE_READONLY 0x00000001 1
** FILE_ATTRIBUTE_HIDDEN 0x00000002 2
** FILE_ATTRIBUTE_SYSTEM 0x00000004 4
** FILE_ATTRIBUTE_DIRECTORY 0x00000010 16
** FILE_ATTRIBUTE_ARCHIVE 0x00000020 32
** FILE_ATTRIBUTE_ENCRYPTED 0x00000040 64
** FILE_ATTRIBUTE_NORMAL 0x00000080 128
** FILE_ATTRIBUTE_TEMPORARY 0x00000100 256
************************************************************
IF TYPE('tcFilePath') # 'C'
RETURN -1
ENDIF
IF !FILE(tcFilePath) .OR. !DIRECTORY(tcFilePath)
RETURN -1
ENDIF
DECLARE INTEGER GetFileAttributes IN WIN32API String @lpFileName
RETURN GetFileAttributes(tcFilePath)
ENDFUNC
************************************************************
************************************************************
FUNCTION SetFileReadOnly(tcFile)
************************************************************
** Function Name: Set File Attribute Read-Only
** Purpose : Sets file's attribute to Read-Write
** Parameters : Full path to the file or directory as string.
** Return : Success as Boolean
** Note : API Function declaration :
** DECLARE SHORT SetFileAttributes IN win32api ;
** STRING lpFileName, ;
** DOUBLE dwFileAttributes
**
************************************************************
LOCAL llRet
tcFile = ALLTRIM(tcFile)
DECLARE SHORT SetFileAttributes IN win32api STRING
lpFileName, DOUBLE dwFileAttributes
IF FILE(tcFile) .OR. DIRECTORY(tcFile)
IF SetFileAttributes(tcFile, 1) = 1
llRet = .T.
ELSE
llRet = .F.
ENDIF
ELSE
llRet = .F.
ENDIF
RETURN llRet
ENDFUNC
************************************************************
************************************************************
FUNCTION SetFileReadWrite(tcFile)
************************************************************
** Function Name: SetFileReadWrite
** Purpose : Sets file's attribute to Read-Write
** Parameters : Full path to the file or directory as string.
** Return : Success as Boolean.
** Note : API Function declaration (case-sensitive toward API function's name):
** DECLARE SHORT SetFileAttributes IN win32api ;
** STRING lpFileName, ;
** DOUBLE dwFileAttributes
************************************************************
LOCAL llRet
DECLARE SHORT SetFileAttributes IN win32api STRING lpFileName, DOUBLE dwFileAttributes
tcFile = ALLTRIM(tcFile)
IF FILE(tcFile) .OR. DIRECTORY(tcFile)
IF SetFileAttributes(tcFile, 0) = 1
llRet = .T.
ELSE
llRet = .F.
ENDIF
ELSE
llRet = .F.
ENDIF
RETURN llRet
ENDFUNC
************************************************************
Therefore, if you need to, say, create a table
and fill it with some data, here's what you can do:
Code:
****************************************************************
FUNCTION TransferData2NetworkDrive(tcDBF1, tcDBF2, tcCondition)
****************************************************************
&& Imagine that tcDBF1 = "C:\My Data\Table1.DBF",
&& and tcDBF2 = "Y:\Data For Users\Table2.DBF",
&& and tcCondition is "Field1 = [Condition]"
LOCAL llReadOnly
llReadOnly = .F. && Initially
IF BITAND(GetFileAttribInfo(ADDBS(JUSTPATH(tcDBF2)), 1) > 0
llReadOnly = .T.
IF !SetFileReadWrite(ADDBS(JUSTPATH(tcDBF2)))
= MESSAGEBOX([Can't reset folder ] + JUSTPATH(tcDBF2)
+ [ to Read-Write!], ;
16, "User Rights Issue")
RETURN .F.
ENDIF (!SetFileReadWrite(ADDBS(JUSTPATH(tcDBF2))))
ENDIF (BITAND(GetFileAttribInfo(ADDBS(JUSTPATH(tcDBF2)), 1) > 0)
USE (tcDBF1) ALIAS DBF1 IN 0 SHARED
COPY STRUCTURE TO (tcDBF2) WITH CDX
IF !(FILE(tcDBF2) .AND. FILE(FORCEEXT(tcDBF2), "CDX")
= MESSAGEBOX([Can't create file ] + tcDBF2 + ;
[ due to insufficient user rights or system
malfunction!], ;
16, "User Rights Issue")
RETURN .F.
ENDIF (!(FILE(tcDBF2) .AND. FILE(FORCEEXT(tcDBF2), "CDX"))
IF BITAND(GetFileAttribInfo(tcDBF2), 1) > 0
IF !SetFileReadWrite(tcDBF2) .OR. !SetFileReadWrite(FORCEEXT(tcDBF2, "CDX"))
= MESSAGEBOX([Can't reset ] + tcDBF2 + [ file to Read-
Write ] + ;
[due to insufficient user rights or
system malfunction!], ;
16, "User Rights Issue")
RETURN .F.
ENDIF (!SetFileReadWrite(tcDBF2) .OR. !SetFileReadWrite
(FORCEEXT(tcDBF2, "CDX")))
ENDIF (BITAND(GetFileAttribInfo(tcDBF2), 1) > 0)
USE (tcDBF2) ALIAS DBF2 IN 0 EXCLUSIVE
SELECT DBF2
APPEND FROM DBF1 FOR (tcCondition)
FLUSH
IF _TALLY = 0
= MESSAGEBOX([Can't update ] + tcDBF2 + [ table file ] + ;
[due to insufficient user rights or system
malfunction!], ;
16, "User Rights Issue")
RETURN .F.
ENDIF
DO WHILE USED("DBF2")
USE IN DBF2
ENDDO
IF llReadOnly
= SetFileReadOnly(tcDBF2)
= SetFileReadOnly(ADDBS(JUSTPATH(tcDBF2)))
ENDIF
RETURN .T.
ENDFUNC
I hope this would help you to get around the user rights
restriction issue.
Regards,
Ilya