Zakwaan Posted June 4, 2015 Posted June 4, 2015 Newbie question I need to strip out the main root from the directory path. I use this to get the path: (setq dfil (getfiled "Directory listing" "" "dwg" 2)) (setq Filepath (vl-filename-directory dfil)) This returns for example: "C:\\Blocks\\2dlib\\Cars\\Plan" How do I turn the above result into: "2dlib\Cars\Plan" stripping out "C:\\Blocks\\" and only 1 backslash between directories? "C:\\Blocks\\2dlib\\Trees\\Plan" > "2dlib\Trees\Plan" "C:\\Blocks\\2dlib\\People" > "2dlib\\People" Many thanks Zakwaan Quote
Lee Mac Posted June 4, 2015 Posted June 4, 2015 Firstly, note that the backslash character is an escape character in AutoLISP. This means that a single backslash is used to give an alternative meaning to the characters which follow it, for example: \n = new-line character \t = tab character \" = double-quote character etc. Hence, in order to obtain a single backslash, one must prefix the backslash with another backslash in order to mark it as a literal backslash and not an escape character (you are effectively using the first backslash as an escape character to give an alternative meaning to the second backslash). Therefore, in AutoLISP, two backslashes represent a single backslash; observe: _$ (princ "\nThis is a single backslash \\") This is a single backslash \ Quote
Lee Mac Posted June 4, 2015 Posted June 4, 2015 Regarding stripping the folder, consider the following recursive function: (defun stripfolder ( dir lvl / pos ) (if (and (< 0 lvl) (setq pos (vl-string-position 92 dir))) (stripfolder (substr dir (+ 2 pos)) (1- lvl)) dir ) ) A few examples: _$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 2) "2dlib\\Trees\\Plan" _$ (stripfolder "C:\\Blocks\\2dlib\\People" 2) "2dlib\\People" _$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 1) "Blocks\\2dlib\\Trees\\Plan" _$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 3) "Trees\\Plan" Quote
Lee Mac Posted June 5, 2015 Posted June 5, 2015 You're welcome - I hope the explanation was clear! Quote
Recommended Posts
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.