Open Explorer 2 Folder Levels Up From Current Directory

Open Explorer 2 Folder Levels Up From Current Directory

thomasarpasi
Explorer Explorer
712 Views
10 Replies
Message 1 of 11

Open Explorer 2 Folder Levels Up From Current Directory

thomasarpasi
Explorer
Explorer

I'm looking to open Explorer 2 Folder Levels Up From Current Directory via lisp.  I've created a number of LISP using block insertion and scripting, but this has me at a standstill.

 

I have getting the current directory down using dwgprefix.  How do I tell it go back 2 levels in reference to the dwgprefix variable?  I have done alot of searching and nothing has helped.  My approach may be off?  I've tried applying vl-string-trim-right and have had no luck as well.

Thanks in advance. 

0 Likes
713 Views
10 Replies
Replies (10)
Message 2 of 11

Kent1Cooper
Consultant
Consultant

If you're looking to open a drawing in that folder 2 levels up, here's one approach that seems to work:

 

;|  DwgUp2Folders.lsp [command name: DU2F]
  To open a Drawing in the Folder Up 2 levels from the current one
  Kent Cooper, 20 April 2022
|;
(defun C:DU2F (/ prerev)
  (setq prerev (reverse (vl-string->list (getvar 'dwgprefix))))
  (vla-activate
    (vla-open
      (vla-get-documents
        (vlax-get-acad-object)
      )
      (getfiled
        "Select drawing:"
        (vl-list->string
          (reverse (member 92 (cdr (member 92 (cdr prerev)))))
        )
        "dwg" 0
      )
    )
  )
)
(prompt "\nType DU2F to open a Drawing Up 2 Folder levels from current.")

 

If instead you only want to open the folder in Windows Explorer or something, you should at least be able to incorporate the same method of getting that folder name into a version of your current-directory code.

Kent Cooper, AIA
Message 3 of 11

hak_vz
Advisor
Advisor

Something like this:

Opens Explorer two levels up from current file path.

It doesn't check if removing two sub-directories will jump over root i.e C:, so you can try to implement that check.

(defun twodirsup ( / string_to_list ret)
	(defun string_to_list ( str del / pos )
		(if (setq pos (vl-string-search del str))
			(cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
			(list str)
		)
	)
	(setq ret "")
	(foreach e 	(reverse(cddr(reverse (string_to_list (vl-filename-directory (getvar 'DWGPREFIX)) "\\"))))
		(setq ret (strcat ret e  "\\"))
	)
	(startapp "Explorer" ret)
	(princ)
)

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 4 of 11

ВeekeeCZ
Consultant
Consultant
(defun c:Explorer2Up () (startapp "explorer" (strcat (getvar 'dwgprefix) "..\\..")) (princ))
Message 5 of 11

thomasarpasi
Explorer
Explorer

It seems to jump to "Documents" folder.  Not sure why...could be something on my end.

0 Likes
Message 6 of 11

thomasarpasi
Explorer
Explorer

It seems to jump to "Documents" folder.  Not sure why...could be something on my end.

0 Likes
Message 7 of 11

hak_vz
Advisor
Advisor

When directory to jump from is not too deep it will show you "Documents" since jumping two levels up passes over root. Code would have to be updated to so that it can work only with paths deeper than 3 levels.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 8 of 11

ronjonp
Mentor
Mentor

@thomasarpasi 

@ВeekeeCZ  Code worked fine here.

0 Likes
Message 9 of 11

john.uhden
Mentor
Mentor

My turn...

   (defun @str2list (str pat / i j n lst)
     (cond
       ((/= (type str)(type pat) 'STR))
       ((= str pat)'(""))
       (T
         (setq i 0 n (strlen pat))
         (while (setq j (vl-string-search pat str i))
           (setq lst (cons (substr str (1+ i)(- j i)) lst)
                 i (+ j n)
           )
         )
         (reverse (cons (substr str (1+ i)) lst))
       )
     )
   )
;; This is the primary function:
(defun @up2 ( / a b c)
  (setq a (getvar "dwgprefix"))
   ;; "G:\\Projects-Cadd\\2019\\19190.Dewing.Mantoloking\\"
  (setq b (@str2list a "\\"))
   ;; ("G:" "Projects-Cadd" "2019" "19190.Dewing.Mantoloking" "")
  (setq c (reverse (cdr (cdr (reverse (vl-remove "" b))))))
   ;; ("G:" "Projects-Cadd")
  (apply 'strcat (mapcar '(lambda (x)(strcat x "\\")) c))
)

"G:\\Projects-Cadd\\"

John F. Uhden

0 Likes
Message 10 of 11

Sea-Haven
Mentor
Mentor

My take not deeply tested, pick any level.

 

 

 

; string to list by John Uhden
;  by John Uhden modified by Alan H April 2022

(defun @str2list (str pat / i j n lst)
     (cond
       ((/= (type str)(type pat) 'STR))
       ((= str pat)'(""))
       (T
         (setq i 0 n (strlen pat))
         (while (setq j (vl-string-search pat str i))
           (setq lst (cons (substr str (1+ i)(- j i)) lst)
                 i (+ j n)
           )
         )
         (reverse (cons (substr str (1+ i)) lst))
       )
     )
)

;; This is the primary function:

(defun @up2 ( / a b c)
  (setq a (getvar "dwgprefix"))
;; (setq a "G:\\Projects-Cadd\\2019\\projects\\1910\\19190.Dewing.Mantoloking\\")
  (setq b (@str2list a "\\"))

  (setq lst '() x 0 str (strcat (nth 0 b) "/") )
  (setq lst (cons str lst))

  (repeat (- (length b) 2)
    (setq x (1+ x))
    (setq str (strcat str (nth x b) "/"))
    (setq lst (cons str lst))
  )
  
  (setq lst (reverse lst))
  (setq lst (cons "Choose directory " lst))
  

  (if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  (setq ans (ah:butts but "V" lst))
  (setq ans  (vl-string-translate "/" "\\" ans))

  (startapp "explorer" ans)

(princ)
)

 

 

SeaHaven_1-1650591909585.png

 

Can set default button 2 up if required

Message 11 of 11

ronjonp
Mentor
Mentor

You could also do this easily in Windows Explorer. It's one more click though. 😃

ronjonp_0-1650638151270.png

 

0 Likes