Work with text strings

Work with text strings

carlos_m_gil_p
Advocate Advocate
835 Views
5 Replies
Message 1 of 6

Work with text strings

carlos_m_gil_p
Advocate
Advocate

Hello how are you.

 

Could you teach me how to do these three things.

 

I have some folder and file paths and I need to extract some specific things.

File and folder paths may vary.

 

Case 1

I have a folder path and I want it to return the penultimate folder, in this case "my folder"

 

(setq path-folder "D:\\folder1\\folder2\\my folder\\txt\\")

"my folder"

 

Case 2

I have a file path and I want it to return the file name without the extension.

 

(setq path-file "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")

"archivo"

 

Caso 3

I have a file path and I want it to return the file name with the extension.

 

(setq path-file-ext "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")

"archivo.xxx"

 

Beforehand thank you very much.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Accepted solutions (2)
836 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant

You need to separate your string by a certain character. That's a very common task and we call it "parsing".

You can try to make your own parser or use eg. THIS Lee's.

The result would be a list... 

 

("D:" "folder1" "folder2" "my folder" "txt" "archivo.xxx")

 

 

Also, learn about File-Handling functions, HERE is the list of them. Some functions might streamline your work.

 

Or... do you want guidance on how to write your own parser? ...it could be simple while loop with some basic string functions...

 
Message 3 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can find many routines to split a string with separator into a list.

Here's one example:

 

(defun str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
    (cons (substr str 1 pos) (str2lst (substr str (+ (strlen sep) pos 1)) sep))
    (list str)
  )
)

 

 

 

;; Caso 1
(setq path-folder "D:\\folder1\\folder2\\my folder\\txt\\")

(cadr (reverse (str2lst (vl-string-right-trim "\\" path-folder) "\\")))


;; Caso 2
(setq path-file "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")

(vl-filename-base path-file)

;; Caso 3
(setq path-file-ext "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")

(last (str2lst path-file-ext "\\"))

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6

sby0531
Enthusiast
Enthusiast
Accepted solution

Hi, below are my codes:

 

;; return the penultimate folder name
(progn
  (setq	path-folder "D:\\folder1\\folder2\\my folder\\txt\\"
	path-folder (vl-string-right-trim "\\" path-folder))
  (setq	n 0
	start 0
	acc nil)
  (while (setq pos (vl-string-search "\\" path-folder start))
    (setq acc (cons pos acc)
	  start (1+ pos)))
  (setq pos01 (cadr acc)
	pos02 (car acc))	
  (substr path-folder (+ 2 pos01) (- pos02 pos01 1)))
;;
;;return the file name without the extension
(progn
  (setq path-file "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")
  (vl-filename-base path-file))
;;
;;return the file name with the extension
(progn
  (setq path-file-ext "D:\\folder1\\folder2\\my folder\\txt\\archivo.xxx")
  (strcat (vl-filename-base path-file-ext) (vl-filename-extension path-file-ext)))
;; 
Message 5 of 6

carlos_m_gil_p
Advocate
Advocate

Hi @_gile 
As always, a thousand thanks for the help that you have given me to this day.
Thank you.
Excuse my English I only speak Spanish, it is from a translator.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes
Message 6 of 6

carlos_m_gil_p
Advocate
Advocate

Hi @sby0531 
It works well too.
Thanks for your help and everyone on the forum.


AutoCAD 2026
Visual Studio Code 1.99.3
AutoCAD AutoLISP Extension 1.6.3
Windows 10 (64 bits)

0 Likes