Author Topic: WhoHas (open) for Excel files or other types  (Read 5677 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: WhoHas (open) for Excel files or other types
« Reply #15 on: February 02, 2018, 10:17:09 AM »
Try this .. does not tell you who has it open though. Since the files are on your computer I'd imagine you already know that :P

Code - Auto/Visual Lisp: [Select]
  1. (defun _isfileopen (file / o r sf)
  2.   ;; RJP - 2.1.2018
  3.   ;; Idea translated from here: https://goo.gl/gK3UuX
  4.   ;; Use with caution NOT heavily tested
  5.   (if (and (findfile file) (setq sf (vlax-get-or-create-object "Scripting.FileSystemObject")))
  6.                      (setq o (vl-catch-all-apply 'vlax-invoke (list sf 'opentextfile file 8 0)))
  7.                    )
  8.            )
  9.            (vl-catch-all-apply 'vlax-invoke (list o 'close))
  10.            (and sf (vlax-release-object sf))
  11.     )
  12.   )
  13.   r
  14. )
  15. ;; (_isfileopen "C:\\myfile.xlsx")
Code: [Select]
Comando: (setq SFsObj (vlax-get-or-create-object "Scripting.FileSystemObject"))
#<VLA-OBJECT IFileSystem3 000000000bd0eed0>

Comando: (setq FilObj (vl-catch-all-apply 'vlax-invoke (list SFsObj 'opentextfile "C:\\Asso\\assoita.xlsx" 8 0)))
#<%catch-all-apply-error%>

Comando: (setq ErrFlg (vl-catch-all-error-p FilObj))
T

Comando: !FilObj
#<%catch-all-apply-error%>

Comando: (vl-catch-all-apply 'vlax-invoke (list FilObj 'close))         
#<%catch-all-apply-error%>                                                     <<<<<<<<<<<<<<<<<

Comando: (and SFsObj (vlax-release-object SFsObj))
T
Is this line >>> (vl-catch-all-apply 'vlax-invoke (list FilObj 'close)) better in this form if the file is open:
(or r (vl-catch-all-apply 'vlax-invoke (list o 'close)))

ChrisCarlson

  • Guest
Re: WhoHas (open) for Excel files or other types
« Reply #16 on: February 02, 2018, 11:04:48 AM »
I've got an odd question: how often is it that someone else has a local file open on your own computer?  That should be so rare as to have a "whohas" type function just returning the active user.
What is your solution to know who opened an Excel file on the net? (with lisp/visuallisp)

I don't believe Excel will directly release that information. You can check if its open (read-only) or closed (read/write) but to return the user would require domain information.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: WhoHas (open) for Excel files or other types
« Reply #17 on: February 02, 2018, 11:20:08 AM »
Try this .. does not tell you who has it open though. Since the files are on your computer I'd imagine you already know that :P

Code - Auto/Visual Lisp: [Select]
  1. (defun _isfileopen (file / o r sf)
  2.   ;; RJP - 2.1.2018
  3.   ;; Idea translated from here: https://goo.gl/gK3UuX
  4.   ;; Use with caution NOT heavily tested
  5.   (if (and (findfile file) (setq sf (vlax-get-or-create-object "Scripting.FileSystemObject")))
  6.            (setq o (vl-catch-all-apply 'vlax-invoke (list sf 'opentextfile file 8 0)))
  7.          )
  8.       )
  9.       (vl-catch-all-apply 'vlax-invoke (list o 'close))
  10.       (and sf (vlax-release-object sf))
  11.     )
  12.   )
  13.   r
  14. )
  15. ;; (_isfileopen "C:\\myfile.xlsx")
Code: [Select]
Comando: (setq SFsObj (vlax-get-or-create-object "Scripting.FileSystemObject"))
#<VLA-OBJECT IFileSystem3 000000000bd0eed0>

Comando: (setq FilObj (vl-catch-all-apply 'vlax-invoke (list SFsObj 'opentextfile "C:\\Asso\\assoita.xlsx" 8 0)))
#<%catch-all-apply-error%>

Comando: (setq ErrFlg (vl-catch-all-error-p FilObj))
T

Comando: !FilObj
#<%catch-all-apply-error%>

Comando: (vl-catch-all-apply 'vlax-invoke (list FilObj 'close))         
#<%catch-all-apply-error%>                                                     <<<<<<<<<<<<<<<<<

Comando: (and SFsObj (vlax-release-object SFsObj))
T
Is this line >>> (vl-catch-all-apply 'vlax-invoke (list FilObj 'close)) better in this form if the file is open:
(or r (vl-catch-all-apply 'vlax-invoke (list o 'close)))
I would agree with that .. my logic was a little flawed  :oops:  .. I'll update my earlier post.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: WhoHas (open) for Excel files or other types
« Reply #18 on: February 03, 2018, 11:47:55 AM »
I've got an odd question: how often is it that someone else has a local file open on your own computer?  That should be so rare as to have a "whohas" type function just returning the active user.
What is your solution to know who opened an Excel file on the net? (with lisp/visuallisp)

I don't believe Excel will directly release that information. You can check if its open (read-only) or closed (read/write) but to return the user would require domain information.
Did you open the file in my replay #12?
(setq FilPtr (open "z:\\Renamed__$test.xlsx" "r"))
(substr (read-line FilPtr) 2 20) =>> "Marco               "

rkmcswain

  • Swamp Rat
  • Posts: 978
Re: WhoHas (open) for Excel files or other types
« Reply #19 on: February 05, 2018, 08:56:08 AM »
Quote from: Lee Mac
However, since most other filetypes are "single-user" files, I don't think they will generate their own accompanying lock files.

Slight correction, those are not "lock" files in the sense that the O/S uses them to determine if the file is in use.
At least with Windows.

ChrisCarlson

  • Guest
Re: WhoHas (open) for Excel files or other types
« Reply #20 on: February 05, 2018, 10:15:45 AM »
I've got an odd question: how often is it that someone else has a local file open on your own computer?  That should be so rare as to have a "whohas" type function just returning the active user.
What is your solution to know who opened an Excel file on the net? (with lisp/visuallisp)

I don't believe Excel will directly release that information. You can check if its open (read-only) or closed (read/write) but to return the user would require domain information.


Did you open the file in my replay #12?
(setq FilPtr (open "z:\\Renamed__$test.xlsx" "r"))
(substr (read-line FilPtr) 2 20) =>> "Marco               "

It returns jibberish on AutoCAD 2018 and Excel 2013

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: WhoHas (open) for Excel files or other types
« Reply #21 on: February 05, 2018, 12:09:44 PM »
I've got an odd question: how often is it that someone else has a local file open on your own computer?  That should be so rare as to have a "whohas" type function just returning the active user.
What is your solution to know who opened an Excel file on the net? (with lisp/visuallisp)
I don't believe Excel will directly release that information. You can check if its open (read-only) or closed (read/write) but to return the user would require domain information.


Did you open the file in my replay #12?
(setq FilPtr (open "z:\\Renamed__$test.xlsx" "r"))
(substr (read-line FilPtr) 2 20) =>> "Marco               "

It returns jibberish on AutoCAD 2018 and Excel 2013
OK, thanks for test.  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: WhoHas (open) for Excel files or other types
« Reply #22 on: February 15, 2018, 08:26:18 AM »
In regard to the functions that investigate the status of a file open/closed, I tried three types of functions but I get very different results if the file is open or closed.
Why is _isfileopen much slower if the file is open?
Code: [Select]
(defun its_ok (file / dwg)
  (and (findfile file)
       (setq dwg (open file "a"))
    (not (close dwg))
  )
)
; Lee Mac
(defun _FileOpen-p ( filename )
  (not (vl-file-rename filename filename))
)
(defun _isfileopen (file / o r sf)
  ;; RJP - 2.1.2018
  ;; Idea translated from here: https://goo.gl/gK3UuX
  ;; Use with caution NOT heavily tested
  (if (and (findfile file) (setq sf (vlax-get-or-create-object "Scripting.FileSystemObject")))
    (progn (setq r (vl-catch-all-error-p
     (setq o (vl-catch-all-apply 'vlax-invoke (list sf 'opentextfile file 8 0)))
   )
   )
   (or r (vl-catch-all-apply 'vlax-invoke (list o 'close)))
   (and sf (vlax-release-object sf))
    )
  )
  r
)
Code: [Select]
FILE OPENED
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_FILEOPEN-P "z:\\temp\\LspTest.lsp")......1312 / 13.2 <fastest>
    (_FILEOPEN-P "z:\\temp\\LspTest.lsp")......1328 / 13.04
    (ITS_OK "z:\\temp\\LspTest.lsp")...........4281 / 4.04
    (ITS_OK "z:\\temp\\LspTest.lsp")...........4453 / 3.89
    (_ISFILEOPEN "z:\\temp\\LspTest.lsp").....17141 / 1.01
    (_ISFILEOPEN "z:\\temp\\LspTest.lsp").....17313 / 1 <slowest>


FILE CLOSED
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (ITS_OK "z:\\temp\\LspTest.lsp")..........1406 / 2.08 <fastest>
    (ITS_OK "z:\\temp\\LspTest.lsp")..........1422 / 2.05
    (_ISFILEOPEN "z:\\temp\\LspTest.lsp").....1718 / 1.7
    (_ISFILEOPEN "z:\\temp\\LspTest.lsp").....1734 / 1.69
    (_FILEOPEN-P "z:\\temp\\LspTest.lsp").....2891 / 1.01
    (_FILEOPEN-P "z:\\temp\\LspTest.lsp").....2922 / 1 <slowest>


FILE OPENED
Elapsed milliseconds / relative speed for 32768 iteration(s):
    (_FILEOPEN-P "z:\\temp\\XlsTest.xlsx")......1328 / 13.06 <fastest>
    (_FILEOPEN-P "z:\\temp\\XlsTest.xlsx")......1359 / 12.76
    (ITS_OK "z:\\temp\\XlsTest.xlsx")...........4110 / 4.22
    (ITS_OK "z:\\temp\\XlsTest.xlsx")...........4156 / 4.17
    (_ISFILEOPEN "z:\\temp\\XlsTest.xlsx").....17281 / 1
    (_ISFILEOPEN "z:\\temp\\XlsTest.xlsx").....17344 / 1 <slowest>


FILE CLOSED
lapsed milliseconds / relative speed for 8192 iteration(s):
    (ITS_OK "z:\\temp\\XlsTest.xlsx")..........1390 / 2.07 <fastest>
    (ITS_OK "z:\\temp\\XlsTest.xlsx")..........1421 / 2.02
    (_ISFILEOPEN "z:\\temp\\XlsTest.xlsx").....1719 / 1.67
    (_ISFILEOPEN "z:\\temp\\XlsTest.xlsx").....1734 / 1.66
    (_FILEOPEN-P "z:\\temp\\XlsTest.xlsx").....2875 / 1
    (_FILEOPEN-P "z:\\temp\\XlsTest.xlsx").....2875 / 1 <slowest>