Hello, lisp , I want to get the path without the last folder

Hello, lisp , I want to get the path without the last folder

eng_minamaged
Advocate Advocate
1,542 Views
23 Replies
Message 1 of 24

Hello, lisp , I want to get the path without the last folder

eng_minamaged
Advocate
Advocate

Hello, lisp , I want to get the path without the last folder  (setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\") 

0 Likes
1,543 Views
23 Replies
Replies (23)
Message 2 of 24

paullimapa
Mentor
Mentor

here's one way by splitting the path into separate elements and then combine all back dropping the last element in the list:

 

; function to drop last folder in given path
; Argument:
; tst = folder path as a string
; Example:
; (droplastdir "C:\\Autodesk\\Test")
; Returns:
; "C:\\Autodesk"
(defun droplastdir (tst / aec_str2lst dir i new)
(vl-load-com)
;;;--- aec_str2lst function
; splits given string with separator 
; returns list with each item
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-do-i-split-a-file-path-into-bits-based-on-back-slash/td-p/2396237
; Usage:
; (aec_str2lst (findfile "acad.exe") "\\")
; Returns:
; ("C:" "Program Files" "Autodesk" "AutoCAD 2020" "acad.exe")
; Usage:
; (aec_str2lst "0 1 2 3" " ")
; Returns:
; ("0" "1" "2" "3")
(defun aec_str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
   (cons (substr str 1 pos)
    (aec_str2lst (substr str (+ (strlen sep) pos 1)) sep)
   )
   (if(not(zerop(strlen str)))(list str))
  )
) ; defun

(if (equal (setq dir (aec_str2lst tst "\\")) (list tst)) ; if no change
 (setq new tst) ; no change
 (progn
  (setq new "" i 0)
  (repeat (1- (length dir))
   (setq new (strcat new "\\" (nth i dir)))(setq i(1+ i))
  )
  (if (= "" (setq new (substr new 2 (1- (strlen new)))))(setq new (car dir)))  ; if empty string 
 ) ; progn
) ; if
new
) ; defun

 

 

 

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 24

komondormrex
Mentor
Mentor
Accepted solution

hi,

check this

(setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\")
(if (= "\\" (substr my-path (strlen my-path) 1))
	(setq before-last-my-path (strcat (vl-filename-directory (vl-filename-directory my-path)) "\\"))
       	(setq before-last-my-path (strcat (vl-filename-directory my-path) "\\"))
)
0 Likes
Message 4 of 24

Kent1Cooper
Consultant
Consultant
Accepted solution

(setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\")

then

(substr my-path 1 (1+ (vl-string-position 92 (substr my-path 1 (1- (strlen my-path))) 1 T)))

returns
"C:\\Users\\Username\\Documents\\Folder1\\"

Kent Cooper, AIA
0 Likes
Message 5 of 24

eng_minamaged
Advocate
Advocate

I am sure this is true code but I can't test it because I don't have the ability to understand lisp code

0 Likes
Message 6 of 24

paullimapa
Mentor
Mentor

Actually very straightforward to test

just save the attached droplastdir.lsp file to a folder in your c: drive and then drag&drop it into your AutoCAD graphics window

Then at the command prompt enter the following:

(setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\") 

(droplastdir my-path)

AutoCAD should return:

"C:\\Users\\Username\\Documents\\Folder1"


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 7 of 24

eng_minamaged
Advocate
Advocate

I appreciate your help... If I want the save current drawing to the new path 

0 Likes
Message 8 of 24

paullimapa
Mentor
Mentor

Try 

 

(setq dwgname (strcat (droplastdir my-path) "\\" (vl-filename-base (getvar"dwgname")))) (vla-saveas (vla-get-ActiveDocument (vlax-get-Acad-Object)) dwgname)

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 9 of 24

eng_minamaged
Advocate
Advocate

Ok I am sorry but it give me error if i want to  create new folder beside last folder in path

Not in last folder but beside it

"new drawings" and then save as in it

(setq result (substr result 1 (1+ (vl-string-position 92 (substr result 1 (1- (strlen result))) 1 T))))
(setq result (vl-string-left-trim "\\" result)) ; Remove leading backslashes
(setq result (vl-string-right-trim "\\" result)) ; Remove trailing backslashes

(vl-mkdir strcat result "/new  Drawings/")

(if (< (getvar "expert") 2) ; add to chk setting
(command "_.SAVEAS" "" (strcat result "/new  Drawings" "\\" (nth n Files_Folder)) "_Yes")
)

0 Likes
Message 10 of 24

paullimapa
Mentor
Mentor
Accepted solution

no worries:

(setq newdir "new drawings")
(setq newprefix (strcat (droplastdir (getvar"dwgprefix")) "\\" newdir))
(setq dwgname (vl-filename-base (getvar"dwgname")))
(vl-mkdir newprefix)
(vla-saveas (vla-get-ActiveDocument (vlax-get-Acad-Object)) (strcat newprefix "\\" dwgname))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 11 of 24

eng_minamaged
Advocate
Advocate

@paullimapa could you tell me what is the error in my code?

0 Likes
Message 12 of 24

paullimapa
Mentor
Mentor

what error is it showing?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 24

eng_minamaged
Advocate
Advocate

I use lisp to run code on folder of dwgs and I want to save as it in new folder beside the last folder but it can't save as I think there as error in line of save as in path 

 

(setq result (substr result 1 (1+ (vl-string-position 92 (substr result 1 (1- (strlen result))) 1 T))))
(setq result (vl-string-left-trim "\\" result)) ; Remove leading backslashes
(setq result (vl-string-right-trim "\\" result)) ; Remove trailing backslashes

(vl-mkdir strcat result "/new  Drawings/")

(if (< (getvar "expert") 2) ; add to chk setting
(command "_.SAVEAS" "" (strcat result "/new  Drawings" "\\" (nth n Files_Folder)) "_Yes")
)

0 Likes
Message 14 of 24

paullimapa
Mentor
Mentor

perhaps you meant for @Kent1Cooper to look at this because it's not the same code I provided you to run.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 15 of 24

eng_minamaged
Advocate
Advocate

If any advice I will be gratfull

0 Likes
Message 16 of 24

paullimapa
Mentor
Mentor

I'm just a bit confused by what you're attempting to do with these particular way of composing the code.

Like what is this attempting to do?

(setq result (substr result 1 (1+ (vl-string-position 92 (substr result 1 (1- (strlen result))) 1 T))))
What does the original result stand for?
My assumption would be:
(setq result (getvar"dwgprefix")) 
This would then be the current drawing's path which would be similar to your original line of code in your OP:
(setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\") 
And drop the last folder returning this as result:
"C:\\Users\\Username\\Documents\\Folder1\\"
Then why would you need this line of code immediately afterwards:
(setq result (vl-string-left-trim "\\" result)) ; Remove leading backslashes
Do you expect to have "\\" in front of C:\\ which you do not have?
Next, why do you need this line of code?
(setq result (vl-string-right-trim "\\" result)) ; Remove trailing backslashes
You'll now get this as result after running the above line:
"C:\\Users\\Username\\Documents\\Folder1"
Then you decide to add the slash back but as a forward slash to create the new folder after result:
(vl-mkdir strcat result "/new  Drawings/")
but this line of code fails because you're missing parenthesis and you don't need an additional slash at the end so it should look like this instead:
(vl-mkdir (strcat result "/new  Drawings"))
Lastly, with this line of code, what is Files_Folder set equal to which is not anywhere in the previous lines of code?
(command "_.SAVEAS" "" (strcat result "/new  Drawings" "\\" (nth n Files_Folder)) "_Yes")
 
 

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 17 of 24

komondormrex
Mentor
Mentor
Accepted solution

if files_folder is a list of filenames you want to save in newly created "New Drawings" adjacent folder to "folder2", check the code below

 

 

(setq my-path "C:\\Users\\Username\\Documents\\Folder1\\Folder2\\"
	  new_folder "New Drawings"
)
(if (= "\\" (substr my-path (strlen my-path) 1))
	(setq adjacent_folder (strcat (vl-filename-directory (vl-filename-directory my-path)) "\\" new_folder))
    (setq adjacent_folder (strcat (vl-filename-directory my-path) "\\" new_folder))
)
(if (vl-mkdir adjacent_folder)
	(foreach file Files_Folder 
		(command "_.SAVEAS" "" (strcat adjacent_folder "\\" file) "_Yes")
	)
)

 

 

 

0 Likes
Message 18 of 24

eng_minamaged
Advocate
Advocate

@komondormrex it is like magic . Thank you very much for your help and for your time Thank @komondormrex @paullimapa @Kent1Cooper . And @paullimapa  I am very sorry for you. I am really afraid of the big codes, but of course, you are making a great effort because you have helped many people, and of course I am one of them.

0 Likes
Message 19 of 24

eng_minamaged
Advocate
Advocate

If the folder "New Drawings" exists it won't save the drawings in it.. I want to create it if not exist if not...... save the drawings in it

0 Likes
Message 20 of 24

paullimapa
Mentor
Mentor
(if(not(vl-directory-p (strcat result "/new  Drawings")))(vl-mkdir (strcat result "/new  Drawings")))

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes