Jump to content
Sambuddy

FOREACH - ARGUMENT

Recommended Posts

Sambuddy

I am trying to include every dwg extenstion in a list, only then to use for each to set them to read-only.

(defun Read_only ( file / fso f )

(setq fso (vlax-create-object "Scripting.FileSystemObject"))
(if (not (zerop (vlax-invoke fso 'FileExists file)))
(progn
(setq f (vlax-invoke fso 'GetFile file))
(vlax-put-property f 'Attributes (boole 7 1 (vlax-get f 'Attributes)))
(vlax-release-object f)
) ;END PROGN
) ;END IF

(vlax-release-object fso)
)

(setq drwnClist (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))

how do I use a foreach statement to include all files to undergo (Read_only drwnClist)?

 

Thanks

Share this post


Link to post
Share on other sites
dlanorh
17 minutes ago, Sambuddy said:

I am trying to include every dwg extenstion in a list, only then to use for each to set them to read-only.


(defun Read_only ( file / fso f )

(setq fso (vlax-create-object "Scripting.FileSystemObject"))
(if (not (zerop (vlax-invoke fso 'FileExists file)))
(progn
(setq f (vlax-invoke fso 'GetFile file))
(vlax-put-property f 'Attributes (boole 7 1 (vlax-get f 'Attributes)))
(vlax-release-object f)
) ;END PROGN
) ;END IF

(vlax-release-object fso)
)

(setq drwnClist (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))

how do I use a foreach statement to include all files to undergo (Read_only drwnClist)?

 

Thanks

 

Not great at file manipulation, so I'm unsure what (vl-directory-files...) returns

 

Try this

 

(defun Read_only ( file_list / fso f )

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (if (not (zerop (vlax-invoke fso 'FileExists file)))
    (foreach file file_list
      (setq f (vlax-invoke fso 'GetFile file))
      (vlax-put-property f 'Attributes (boole 7 1 (vlax-get f 'Attributes)))
      (vlax-release-object f)
    ) ;END FOREACH
  ) ;END IF

  (vlax-release-object fso)
)

;To call
(setq drwnClist (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))
(Read_only drwnClist)

or

(Read_only (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))

 

Share this post


Link to post
Share on other sites
Sambuddy
15 hours ago, dlanorh said:

 

Not great at file manipulation, so I'm unsure what (vl-directory-files...) returns

 

Try this

 


(defun Read_only ( file_list / fso f )

  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (if (not (zerop (vlax-invoke fso 'FileExists file)))
    (foreach file file_list
      (setq f (vlax-invoke fso 'GetFile file))
      (vlax-put-property f 'Attributes (boole 7 1 (vlax-get f 'Attributes)))
      (vlax-release-object f)
    ) ;END FOREACH
  ) ;END IF

  (vlax-release-object fso)
)

;To call
(setq drwnClist (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))
(Read_only drwnClist)

or

(Read_only (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1))

 

@dlanorh

(vl-directory-files...) returns list of files in a directory: in my case I wanted all *.dwg to be listed.

But I believe I have to do a "foreach" for my list or else it will not work.

I have 10 dwg files on Block folder that I could simply hardcode but if in the future I rename the files or remove or add other files, I would have like it to take care of setting them all to read-only without me modifying the routine.

(Read_only (vl-directory-files "C:\\TX Cad Config\\Block\\" "*.dwg" 1)) would simply not work!

I have problems with "foreach" item on the list execute "Read_only"

 

Share this post


Link to post
Share on other sites
Lee Mac

The obvious answer given the existing code provided by the OP would be to simply iterate over the list of files returned by the vl-directory-files function using a foreach loop, and evaluate the read_only user-defined function for every file within the loop, e.g.:

(setq dir "C:\\TX Cad Config\\Block\\")
(foreach dwg (vl-directory-files dir "*.dwg" 1)
    (read_only (strcat dir dwg))
)

However, this approach is highly inefficient, as the code is instantiating the File System Object (FSO) and then testing the existence of every file processed, when only one instance of the FSO is required, and the files are already known to exist given that they are being returned by the vl-directory-files function.

 

With this in mind, a slightly more efficient approach might be:

(defun c:test ( / dir fob fso lst )
    (if (and (setq dir "C:\\TX Cad Config\\Block\\"
                   lst (vl-directory-files dir "*.dwg" 1)
             )
             (setq fso (vlax-create-object "scripting.filesystemobject"))
        )
        (progn
            (foreach dwg lst
                (setq fob (vlax-invoke fso 'getfile (strcat dir dwg)))
                (vlax-put-property fob 'attributes (logior 1 (vlax-get fob 'attributes)))
                (vlax-release-object fob)
            )
            (vlax-release-object fso)
        )
    )
    (princ)
)

You could alternatively use the FSO for all of the file manipulation, including obtaining the set of files within the given folder, e.g.:

(defun c:test ( / dir flc fld fso )
    (if (setq dir "C:\\TX Cad Config\\Block"
              fso (vlax-create-object "scripting.filesystemobject")
        )
        (progn
            (vl-catch-all-apply
               '(lambda ( )
                    (setq fld (vlax-invoke fso 'getfolder dir)
                          flc (vlax-get fld 'files)
                    )
                    (vlax-for fob flc
                        (if (wcmatch (strcase (vlax-get fob 'name) t) "*.dwg")
                            (vlax-put-property fob 'attributes (logior 1 (vlax-get fob 'attributes)))
                        )
                    )
                )
            )
            (if flc (vlax-release-object flc))
            (if fld (vlax-release-object fld))
            (vlax-release-object fso)
        )
    )
    (princ)
)

However, the following simple solution is likely to be far more efficient, especially for large folders of files:

(defun c:test ( )
    (command "_.shell" "attrib +r \"C:\\TX Cad Config\\Block\\*.dwg\"")
    (princ)
)

 

Aside -

18 hours ago, dlanorh said:

Not great at file manipulation, so I'm unsure what (vl-directory-files...) returns

 

I can't understand why you replied knowing that the advice offered was inaccurate?

  • Like 1

Share this post


Link to post
Share on other sites
dlanorh
53 minutes ago, Lee Mac said:

Aside -

 

I can't understand why you replied knowing that the advice offered was inaccurate?

 

I can learn about something I'm not completely au fait with, and It gives me something to do to try and take my mind off the passing of my cousin/godmother.

Share this post


Link to post
Share on other sites
rlx
5 hours ago, dlanorh said:

 

I can learn about something I'm not completely au fait with, and It gives me something to do to try and take my mind off the passing of my cousin/godmother.

 

@dlanorh my sincere condolences... sadly my own mother (85) has advanced cancer so she runnig on 'happy pills' right now , could be weeks , could be months and because of covid and my own wife's health issues must be extremely careful not to come to near or touch ... difficult times for everyone all over the world 😪  😷

Share this post


Link to post
Share on other sites
dlanorh
8 hours ago, rlx said:

 

@dlanorh my sincere condolences... sadly my own mother (85) has advanced cancer so she runnig on 'happy pills' right now , could be weeks , could be months and because of covid and my own wife's health issues must be extremely careful not to come to near or touch ... difficult times for everyone all over the world 😪  😷

Thanks @rlx she was 88 and it wasn't unexpected as she had a serious heart condition exacerbated by her dementia, but it was still a shock to get the phone call. I have power of attorney so lots to set in motion, just sad I couldn't visit the last three weeks because of the current situation.:cry:

Share this post


Link to post
Share on other sites
Sambuddy

@Lee Mac Thanks for your help AGAIN! You the man! You the man!

 

@dlanorh sorry to hear about your situation - I wish you all the best and I wish her some comfort at the time she needs it the most.

 

Thank you guys for replying to my half-but requests on this forum.

  • Like 1

Share this post


Link to post
Share on other sites
Sambuddy

@Lee Mac

Hey Lee? Is it possible for the routine to let me that a files under that Block directory is open with a message so I have to close it before it is set to read-only?

 

Thanks

Share this post


Link to post
Share on other sites
Lee Mac
2 hours ago, Sambuddy said:

@Lee Mac

Hey Lee? Is it possible for the routine to let me that a files under that Block directory is open with a message so I have to close it before it is set to read-only?

 

The easiest way to do this is to check for the existence of a corresponding & undeletable .dwl file with the same filename as that of your drawing file - you can refer to lines 923-949 of my Batch Attribute Editor for an existing example demonstrating this technique in an application.

 

You could define a general function such as:

;; Drawing Open-p  -  Lee Mac
;; Returns the owner of an open drawing, else nil if the drawing is unopen.
;; dwg - [str] Drawing filename

(defun LM:dwgopen-p ( dwg / dwl tmp usr )
    (if (and (setq dwl (findfile (strcat (substr dwg 1 (- (strlen dwg) 3)) "dwl")))
             (null (vl-file-delete dwl))
        )
        (if (setq tmp (open dwl "r"))
            (progn
                (setq usr (read-line tmp)
                      tmp (close tmp)
                )
                usr
            )
            "<Unknown>"
        )
    )
)

And then call it in your code in the following way:

(defun c:test ( / dir lst opn )
    (setq dir "C:\\TX Cad Config\\Block")
    (cond
        (   (null (setq lst (vl-directory-files dir "*.dwg" 1)))
            (princ "\nNo drawing files found.")
        )
        (   (setq opn (vl-some '(lambda ( dwg / own ) (if (setq own (LM:dwgopen-p (strcat dir "\\" dwg))) (cons dwg own))) lst))
            (alert (strcat "Please close drawing " (car opn) " currently open by " (cdr opn)))
        )
        (   (command "_.shell" (strcat "attrib +r \"" dir "\\*.dwg\"")))
    )
    (princ)
)

 

Share this post


Link to post
Share on other sites
Sambuddy
Posted (edited)
7 minutes ago, Lee Mac said:

(

@Lee Mac

I will try your routine right away - just wanted to thank you AGAIN for everything you have done to address my concerns, some of which, are just dumb questions or unnecessary complexity onto a perfect way of life.

 

Thank you Lee

Could you explain your line here where you identify the username, does this also work over Server to detect and express on the alert message?

(setq opn (vl-some '(lambda ( dwg / own ) (if (setq own (dwgopen-p (strcat dir "\\" dwg))) (cons dwg own))) lst))

 

Edited by Sambuddy

Share this post


Link to post
Share on other sites
Lee Mac
40 minutes ago, Sambuddy said:

just wanted to thank you AGAIN for everything you have done to address my concerns, some of which, are just dumb questions or unnecessary complexity onto a perfect way of life.

 

You're most welcome @Sambuddy, I'm happy to help.

 

41 minutes ago, Sambuddy said:

Could you explain your line here where you identify the username, does this also work over Server to detect and express on the alert message?


(setq opn (vl-some '(lambda ( dwg / own ) (if (setq own (dwgopen-p (strcat dir "\\" dwg))) (cons dwg own))) lst))

 

 

I haven't tested it in such an environment, but I don't see any reason why it wouldn't work, providing that the calling program has read/write access to the server path.

Share this post


Link to post
Share on other sites
Sambuddy
Posted (edited)

@Lee Mac

You the man!

 

Thank you very much.

I do not know how some of your routines are working but they do wonder Lee! It does work over the server as well as locally. I still have to understand how your "opn" variable can detect username or how it detects the open file... speechless of your talent or how easy you solve problems!

Edited by Sambuddy

Share this post


Link to post
Share on other sites
BIGAL

Just a side note have you looked at old fashioned DOS, if I remember correct.

 

Attrib *.dwg -r

Share this post


Link to post
Share on other sites
Sambuddy
11 hours ago, BIGAL said:

Just a side note have you looked at old fashioned DOS, if I remember correct.

 

Attrib *.dwg -r

@BIGAL

hey Alan, i I did and even Lee has it on his routine above using shell command attrib. The problem I have is that people freak out when they see COD windows popping out when they are using AutoCAD. I had the same problem with Xcopy through shell. It was easy but then people started freaking out over strange activities on black windows hah.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...