read-only file

Discussion in 'AutoCAD' started by Joe, Feb 24, 2004.

  1. Joe

    Joe Guest

    is there a way to check if a file is read-only or locked from autolisp?
     
    Joe, Feb 24, 2004
    #1
  2. Joe

    ECCAD Guest

    Joe,
    If you have the doslib2k.arx loaded..

    One way is to do:
    (setq z (cdr (car (dos_attrib "c:\\acad\\test.txt"))))
    ..
    where "c:\\acad\\test.txt" is the filename you want to check.
    ..
    (if (= z 0); file is 'normal' mode
    (if (> z 0); file has some attrib set other than normal
    (if (= z 8); file is 'read-only'
    (if (= z 9); file is 'archive' and 'read-only'

    This should get you started.

    Bob
     
    ECCAD, Feb 25, 2004
    #2
  3. Hi, You should use
    a) activeX extension of visual lisp
    b) go to ACADX.com (very good) there are some examples
    for using windows scripting host with Visual Lisp.

    Using scripting host it's easy to read the file properties

    Regards
    Dieter


    Example based upon the work from acadx.com (it's now time to return
    something)


    ;;; test fileproperties
    ;;; returns T if
    ;;; a) file exists
    ;;; b) file can be accessed -> not read only
    ;;; PARAMETER:
    ;;; fso -> filesystem object created by calling function
    ;;; fname -> filename
    ;;; show-errmessage -> see below
    (defun fs:test-fileproperties ( fso fname show-errmessage /
    GetFileName
    GetFile file-attributes
    file-exist
    getfile-obj
    FileOK readonly
    )
    (setq file-exist (vlax-invoke-method fso 'FileExists fname)
    FileOK nil)
    (if (= file-exist :vlax-true)
    (progn
    (setq getfile-obj (vlax-invoke-method fso 'GetFile fname )
    file-attributes (vlax-get-property getfile-obj 'attributes)
    FileOK T)

    (if (= 1 (check_bit bit_test file-attributes 0))
    (progn
    (setq FileOK nil
    readonly T)
    ))

    (vlax-release-object getfile-obj)
    ))

    ;;; show-errmessage = 0 -> no errormessage at all
    ;;; 1 -> errormessage in commandwindow
    ;;; 2 -> errormessage by menue
    (cond
    (( = 1 show-errmessage)
    (or FileOK
    (and readonly
    (princ (strcat "\n file " fname " is write protected!"))
    )
    );;; or FileOK
    )
    (( = 2 show-errmessage)
    (or FileOK
    (and readonly
    ;;; use acad error message or write your own
    (princ "\nFile is readonly")

    )
    );;; or FileOK
    )

    )
    FileOK
    );;; end defun

    ;;; utility function to test which bit is set
    ;;; I use it for testing even or odd number testing as well
    ;;; bit zero = 0 == even
    ;;; Number must be of type integer

    (defun check_bit (bit_test Maske bit / tmp_bit_test)
    (if (/= 0 (logand Maske (expt 2 bit)))
    ;; bit set, return 1
    (setq tmp_bit_test 1)
    (setq tmp_bit_test 0)
    )

    tmp_bit_test
    );;---- end defun --------
     
    Dieter Berger, Feb 27, 2004
    #3
  4. Why not vl-file-systime?
     
    Marc'Antonio Alessi, Feb 27, 2004
    #4
  5. sorry Joe, I forgot in that posted code that you have to get the
    file-system object by calling

    (setq fso (vlax-create-object "scripting.filesystemobject"))

    Thats the parameter "fso" passed in
    (fs:test-fileproperties fso ....)
    Don't forget to check if fso was created.

    More help about the file object and related information under ACAD may
    be obtained by starting Extras-> Macros-> Visual-Basic-Editor

    Start Help within VBA-Editor
    Search for "file" and read....

    Best regards
    Dieter
     
    Dieter Berger, Feb 27, 2004
    #5
  6. If you are talking about the current DWG that's loaded, then check out
    the WRITESTAT system variable.

    If you are talking about any file on disk, looks like you've got enough
    other replies to keep you busy so I won't bother to add my $0.02 there.

    --
    Darren J. Young
    CAD/CAM Systems Developer

    Cold Spring Granite Company
    202 South Third Avenue
    Cold Spring, Minnesota 56320

    Email:
    Phone: (320) 685-5045
    Fax: (320) 685-5052
     
    Darren J. Young, Mar 2, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.