be_bo Posted October 26, 2010 Posted October 26, 2010 I'm printing CAD to PDF using LSP. Here is the code for a full file name path: (setq pdf_name (getfiled "Specify file name to plot *.pdf" (getvar "SAVENAME") "pdf" 5)) i.e. - i:\cadd\project\Plan.pdf What would be the code to capture the folder path only, in other words, how can I subtract a file name from this expression, and get only a folder name? i.e. - i:\cadd\project\ Thank you! Quote
be_bo Posted October 26, 2010 Author Posted October 26, 2010 That would get the path to the folder with DWG file. I'm saving the PDF file into the different folder. My lisp would invoke a dialog box and a user can browse and save the PDF inito the folder of his choice. I need to get the path to this folder, so I can open this folder. So, I need to subtract a file name from this expression, and get only the folder name. Quote
VVA Posted October 26, 2010 Posted October 26, 2010 (vl-load-com) (setq path "i:\\cadd\\project\\Plan.pdf") (setq file (VL-FILENAME-BASE path)) (setq dir (VL-FILENAME-DIRECTORY path)) (setq ext (VL-FILENAME-EXTENSION path)) Quote
pBe Posted October 26, 2010 Posted October 26, 2010 (edited) there are more than one way to retrieve folder path... but since you already have the variable pdf_name (substr pdf_name 1 (- (strlen pdf_name)(strlen (getvar "dwgname")))) VVA you beat me to it Edited October 26, 2010 by pBe typo Quote
be_bo Posted October 26, 2010 Author Posted October 26, 2010 (edited) That was something I had to play with, but I made it to work! Thank you! спасибо, земляк!! Edited October 26, 2010 by be_bo typo Quote
be_bo Posted October 26, 2010 Author Posted October 26, 2010 (edited) That was a plug-and-play thing. Thank you!! Edited October 26, 2010 by be_bo typo Quote
be_bo Posted October 26, 2010 Author Posted October 26, 2010 Thank you both for your prompt and effective help! Quote
Lee Mac Posted October 26, 2010 Posted October 26, 2010 there are more than one way to retrieve folder path... but since you already have the variable pdf_name (substr pdf_name 1 (- (strlen pdf_name)(strlen (getvar "dwgname")))) Bear in mind that is not generic in that it assumes the filename is of the same length as the current drawing filename... *just making you aware* Anyway, some have already been posted, but here are a few more: (car (fnsplitl <filename>)) (vl-filename-directory <filename>) (substr <filename> 1 (vl-string-position 92 <filename> 0 t)) ;; assumes "\\" used (substr <filename> 1 (vl-string-position 92 (vl-string-translate "/" "\\" <filename>) 0 t)) Quote
pBe Posted October 26, 2010 Posted October 26, 2010 Bear in mind that is not generic in that it assumes the filename is of the same length as the current drawing filename... by george you're right (substr <filename> 1 (vl-string-position 92 <filename> 0 t)) ;;;;; Lee Mac 2010 This ones cool Quote
be_bo Posted October 27, 2010 Author Posted October 27, 2010 Thank you LeeMac for code and for making me aware, too! You explained me why when PDF file name length was set differently from DWG file name, it didn't give the proper folder name, while I was using (substr pdf_name 1 (- (strlen pdf_name)(strlen (getvar "dwgname")))) Quote
pBe Posted October 27, 2010 Posted October 27, 2010 Thank you LeeMac for code and for making me aware, too! You explained me why when PDF file name length was set differently from DWG file name, it didn't give the proper folder name, while I was using (substr pdf_name 1 (- (strlen pdf_name)(strlen (getvar "dwgname")))) I apologize for that be_bo.. i assume too much, ....... Lesson learned "pBe Go to your room!!!!! " Quote
irneb Posted October 27, 2010 Posted October 27, 2010 Just as another problem, filenames (and even folder names) may have more than just one dot. They may also not have any extension at all. (vl-load-com) (defun splitpath (path / sLst item ext name folder stage) (setq sLst (reverse (vl-string->list path)) stage 0) (foreach item sLst (cond ((= stage 2) (setq folder (cons item folder))) ((and (= item 46) (= stage 0)) ;Last dot (setq ext (vl-list->string folder) folder nil stage 1 ) ) ((and (or (= item 47) (= item 92)) ;Path delimeter / or \ (< stage 2) ;Only last one ) (setq name (vl-list->string folder) folder nil stage 2 ) ) (t (setq folder (cons item folder))) ) ) (if (= name "") (setq name nil)) (setq folder (vl-list->string folder)) (list folder name ext) ) Try it with these: (splitpath "c:\\testpath/subfolder\\file.name.ext") (splitpath "c:\\testpath/sub.folder\\") (splitpath "c:\\testpath/sub.folder\\filename") Quote
pBe Posted October 27, 2010 Posted October 27, 2010 (substr pdf_name 1 (- (strlen pdf_name) (+ (- (strlen pdf_name) (strlen (getvar "dwgprefix")))1 ))) what about this Quote
irneb Posted October 27, 2010 Posted October 27, 2010 what about thisAgain, that assumes the path you're trying to extract from is in the same folder as the drawing. It's a similar problem as per the DWGNAME variable. Quote
pBe Posted October 27, 2010 Posted October 27, 2010 yeah i know.... I should have stayed in my room Bummer, i should restrain myself from giving answers and stick with just asking questions. Quote
Lee Mac Posted October 27, 2010 Posted October 27, 2010 Just as another problem, filenames (and even folder names) may have more than just one dot. They may also not have any extension at all Irneb, The standard functions (as I listed previously) should deal with these cases correctly - do they not on your system? Quote
Lee Mac Posted October 27, 2010 Posted October 27, 2010 Your function sparked an interest Irneb, perhaps another way to write it would be: (defun LM:fnsplitl ( fn / sub ) (defun _sub ( s l / p ) (if (and l (setq p (vl-string-position (car l) s nil t))) (cons (substr s 1 p) (_sub (substr s (+ 2 p)) (cdr l))) (list s) ) ) (_sub (vl-string-translate "/" "\\" fn) '(92 46)) ) Or an iterative version: (defun LM:fnsplitl_iter ( fn ) ( (lambda ( f p ) (mapcar (function (lambda ( d ) (if (setq p (vl-string-position d (setq f (substr f (+ 2 p))) nil t)) (substr f 1 p) f ) ) ) '(92 46 46) ) ) (vl-string-translate "/" "\\" fn) -1 ) ) Quote
irneb Posted October 27, 2010 Posted October 27, 2010 yeah i know.... I should have stayed in my room Bummer, i should restrain myself from giving answers and stick with just asking questions. No, you learn much quicker when you try to do something. The "nice" thing about yours is you're trying to make it as simple and efficient as possible. Unfortunately it doesn't work for all scenarios. Irneb, The standard functions (as I listed previously) should deal with these cases correctly - do they not on your system? I know, just for those who don't want the vla stuff, but still want it to work on all possibilities. :wink:Edit: my code also uses the vl stuff, but that can be changed ... Or an iterative version:... I'd advise an iterative version over recursion in nearly all cases. While recursion could make for short-n-sweet coding, it's not the most efficient if not done exactly correct. See this post: http://devlicio.us/blogs/christopher_bennage/archive/2010/09/14/what-is-functional-programming-part-3-recursion.aspx Note the discussion about Tail Call optimization. If your function doesn't do this the Lisp interpreter's going to make huge amounts of CPU calls and RAM addressing just to step through the calls to the same function. And I'm not sure if AutoLisp optimizes these calls as more modern lisp compilers / interpreters do. 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.