Author Topic: Testing Directory Access  (Read 24 times)

Jeff

  • Newbie
  • *
  • Posts: 32
Testing Directory Access
« on: November 05, 2018, 09:03:03 am »
I am wanting to test Read/Write/Delete access to specific directories. The sample code below works fine, unless I do not have NTFS permissions to the directory. The response I get depends on if I have ErrorMode remarked out or not. "3089: FileWrite: File not Open for writing" or "1077: FileOpen: Open failed". I was hoping to come up with somthing that says the directory exist, but you don't have access or access denied.

The code below

If WinMetrics(-2) == 3 Then AddExtender("wwsop64i.dll") ; 64-bit
Else AddExtender("wwsop34i.dll") ; 32-bit

;C:\Log 
MYLOGPATH=StrCat("C:\LOG")
MYLOGPATHEXISTS=""
MYLOGPATHREADWRITE=""
MYLOGPATHDELETE=""
str_attr =""

If DirExist(MYLOGPATH)
   MYLOGPATHEXISTS="Yes"
   ErrorMode(@OFF)
   testhandle = FileOpen(StrCat(MYLOGPATH,"\MYLOGPATH.txt"), "WRITE")
   FileWrite(testhandle, StrCat("Test Path:",MYLOGPATH))
   FileClose(testhandle)
   ErrorMode(@CANCEL)
   If FileExist(StrCat(MYLOGPATH,"\MYLOGPATH.txt"))
      MYLOGPATHREADWRITE="Yes"
      MYLOGTestFileDelete = aFileDelete(StrCat(MYLOGPATH,"\MYLOGPATH.txt"),1024)
      If MYLOGTestFileDelete == @True
         MYLOGPATHDELETE="Yes"
      Else
         MYLOGPATHDELETE="No"
      End If

   Else
      MYLOGPATHREADWRITE="No"
   End If
   SelectedDir = MYLOGPATH
Else
   MYLOGPATHEXISTS="No"

End If

Message (MyLogPath, StrCat("MY LOG PATH EXISTS: ",MYLOGPATHEXISTS,@CRLF,"MY LOG PATH READ WRITE: ",MYLOGPATHREADWRITE,@CRLF,"MY LOG PATH DELETE: ",MYLOGPATHDELETE ))
Jeff

td

  • Tech Support
  • *****
  • Posts: 2618
    • WinBatch
Re: Testing Directory Access
« Reply #1 on: November 05, 2018, 09:51:12 am »
You may want to spend a little time learning how to properly use the ErrorMode function.  The Consolidated WIL Help file states in the function's documentation that "ONLY put ErrorMode(@OFF) around SINGLE statements where you are handling the errors yourself."  Also, keep in mind that ErrorModes does not suppress 3000 level errors.

So given your current solution, you need to handle the error right after the FileOpen line and not attempt to call FileWrite when an error state exists.   See the LastError function documentation for more information on detecting errors with LastError.

Alternatives are to use IntControl 73 instead of ErrorMode or actually check the access permissions on the targeted files or folder using Network Extender functions like wntAccessGet or wntAcessList.
 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates