Jump to content

strip out root from the directory path


Recommended Posts

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


Link to comment
Share on other sites

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



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 \

Link to comment
Share on other sites

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))


A few examples:

_$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 2)
_$ (stripfolder "C:\\Blocks\\2dlib\\People" 2)
_$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 1)
_$ (stripfolder "C:\\Blocks\\2dlib\\Trees\\Plan" 3)

Link to comment
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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...