Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
kpennell
Posts: 515
Registered: ‎04-14-2004
Message 1 of 9 (167 Views)
Accepted Solution

Return name of different folders

167 Views, 8 Replies
05-15-2013 01:44 PM

Hello all,

 

The following gives me the first folder string, with no back-slashes, that the drawing exists in.

 

(setq FirstFolder
 ((lambda (/ tmpDir tmpPos)
 (substr
 (setq tmpDir (getvar "dwgprefix"))
 (+ 2 (setq tmpPos (vl-string-search "\\" tmpDir)))
 (- (vl-string-search "\\" tmpDir (1+ tmpPos)) (1+ tmpPos))))))

 

Is there a way to find the name of the second folder?

 

Is there a way to find the name of the third folder?

 

The string length of the characters for each of the first, second, third (and so on) folders are different, which means I can't count character using the "substr" function.

 

Thanks

KP

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 2 of 9 (161 Views)

Re: Return name of different folders

05-15-2013 02:04 PM in reply to: kpennell

kpennell wrote:

Hello all,

 

The following gives me the first folder string, with no back-slashes, that the drawing exists in.

.... 

Is there a way to find the name of the second folder?

 

Is there a way to find the name of the third folder?

....


One way is to use one of the many examples of string-splitting-up routines on this forum.  Just one example is the sdtol function [it stands for String Delimited to List] here.  If you apply it as in the first usage example there, you'll get a list from which you can easily extract any of the folder names using (cadr)/(caddr)/etc., or using (nth).

Kent Cooper
Distinguished Contributor
kpennell
Posts: 515
Registered: ‎04-14-2004
Message 3 of 9 (154 Views)

Re: Return name of different folders

05-15-2013 02:07 PM in reply to: Kent1Cooper

Hmm, I don't see that function in the help literature through AutoCAD.

 

I'll certainly be looking what I can see on this discussion group.

 

Thanks again KC

Valued Mentor
alanjt_
Posts: 469
Registered: ‎08-25-2008
Message 4 of 9 (148 Views)

Re: Return name of different folders

05-15-2013 02:15 PM in reply to: kpennell
(defun AT:Str2Lst (s d / i l d)
  ;; Convert string to list, based on separator
  ;; s - String to convert
  ;; d - Delimeter to break string into items
  ;; Ex. - (AT:Str2Lst "1,2,3" ",") -> '("1" "2" "3")
  ;; Alan J. Thompson, 11.11.09 / 04.01.10
  (while (setq i (vl-string-search (strcase d) (strcase s)))
    (setq l (cons (substr s 1 i) l)
          s (substr s (+ i 1 (strlen d)))
    )
  )
  (vl-remove "" (reverse (cons s l)))
)

 

 

eg.

(AT:Str2Lst (getvar 'DWGPREFIX) "\\")

 

Valued Mentor
alanjt_
Posts: 469
Registered: ‎08-25-2008
Message 5 of 9 (145 Views)

Re: Return name of different folders

05-15-2013 02:16 PM in reply to: alanjt_

Oops, if you want the third item in the list:

 

(caddr (AT:Str2Lst (getvar 'DWGPREFIX) "\\"))

 

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 6 of 9 (136 Views)

Re: Return name of different folders

05-15-2013 02:47 PM in reply to: kpennell

kpennell wrote:

Hmm, I don't see that function in the help literature through AutoCAD.

....


No, you won't find it there -- I wrote it.  It's similar in many ways to others on the forum, but if I recall, many of the others work with single characters such as commas or spaces as delimiters [which is also viable in your situation, since as someone else pointed out, "\\" is really a single character in AutoLISP], but in (sdtol) I was going for the possibility of multiple-character delimiters, as you can see from the description there that uses "Mickey Mouse" as a delimiter.

Kent Cooper
Distinguished Mentor
BlackBox_
Posts: 733
Registered: ‎02-25-2013
Message 7 of 9 (130 Views)

Re: Return name of different folders

05-15-2013 03:09 PM in reply to: alanjt_

alanjt_ wrote:
(defun AT:Str2Lst (s d / i l d)
  ;; Convert string to list, based on separator
  ;; s - String to convert
  ;; d - Delimeter to break string into items
  ;; Ex. - (AT:Str2Lst "1,2,3" ",") -> '("1" "2" "3")
  ;; Alan J. Thompson, 11.11.09 / 04.01.10
  (while (setq i (vl-string-search (strcase d) (strcase s)))
    (setq l (cons (substr s 1 i) l)
          s (substr s (+ i 1 (strlen d)))
    )
  )
  (vl-remove "" (reverse (cons s l)))
)

 

 

eg.

(AT:Str2Lst (getvar 'DWGPREFIX) "\\")

 


Interesting approach... Here's one I just posted a bit ago... And another for FilePaths.



"Potential has a shelf life." - Margaret Atwood


Autodesk Exchange Apps ~ Autoloader ~ AutoCAD Security

Distinguished Contributor
kpennell
Posts: 515
Registered: ‎04-14-2004
Message 8 of 9 (103 Views)

Re: Return name of different folders

05-16-2013 03:30 AM in reply to: Kent1Cooper

KC,

 

Once again, I've accepted your response as the solution to my post.  Huge Thanks.  I can see this being very powerful for other applications.

 

KP

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 9 of 9 (88 Views)

Re: Return name of different folders

05-16-2013 05:52 AM in reply to: kpennell

kpennell wrote:

KC,

 

Once again, I've accepted your response as the solution to my post.  Huge Thanks.  I can see this being very powerful for other applications.

 

KP


Thank you -- it's good to know that routines written for a specific request are finding application to different [however slightly in this case] purposes.

Kent Cooper

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community