plackowski Posted September 18, 2020 Share Posted September 18, 2020 Our company typically only has one layout per drawing file, so we don't bother to rename the Layout tab. However, this makes the Publish tool bothersome, since you can't publish without including the name of the Layout. I'd like a lisp routine that would allow me to point to a folder, say "C:\Transmittals", then remove anything after and including the last dash from all PDFs in that folder. I think I can use something like the code below, but I'm struggling with the syntax to find the last dash and remove everything after it. Our drawings are usually in the format of [discipline letter][3-digit number], but some projects have additional dashes in the file names. (defun renameFiles () (foreach X (vl-directory-files "C:/Transmittals" "*.pdf") (vl-file-rename X (strcat X ".pdf")) ) ) Quote Link to comment Share on other sites More sharing options...
rlx Posted September 18, 2020 Share Posted September 18, 2020 (edited) like this? (defun c:t1 ( ) (mapcar 'ditch-dash '("c:/Transmittals/drawing1-layout.pdf" "c:/Transmittals/drawing2-layout.pdf" "c:/Transmittals/drawing3-layout.pdf"))) (defun ditch-dash (s / i) (if (setq i (vl-string-position (ascii "-") s nil T))(strcat (substr s 1 i )(last (fnsplitl s))) s)) (setq test (c:t1)) Edited September 18, 2020 by rlx 1 Quote Link to comment Share on other sites More sharing options...
plackowski Posted September 18, 2020 Author Share Posted September 18, 2020 (edited) Thanks @rlx, that's exactly what I needed! Here's the final code for posterity: (defun C:renameFiles ( / ) (vl-load-com) (setq directory "C:/Transmittals") (foreach oldName (vl-directory-files directory "*.pdf") (if (null (vl-string-search "ayout" oldName)) (setq newName oldName) (setq newName (_removeAfterLastDash oldName)) ) (setq oldFilePath (strcat directory "/" oldName)) (setq newFilePath (strcat directory "/" newName)) (princ (strcat "\nOriginal File Name: " oldName)) (princ (strcat "\nUpdated File Name: " newName)) (princ (strcat "\noldFilePath: " oldFilePath)) (princ (strcat "\nnewFilePath: " newFilePath)) (vl-file-rename oldFilePath newFilePath) ) (princ) ) (defun _removeAfterLastDash (s / i) (if (setq i (vl-string-position (ascii "-") s nil T))(strcat (substr s 1 i )(last (fnsplitl s))) s)) Edited September 18, 2020 by plackowski Added a check to ensure the file name contains "layout" before trying to remove anything. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 18, 2020 Share Posted September 18, 2020 (edited) FWIW, you could refine the vl-directory-files filter and reduce the code to this: (defun c:renamefiles ( / dir new ) (setq dir "C:/Transmittals") (foreach pdf (vl-directory-files dir "*-layout*.pdf" 1) (setq pdf (vl-filename-base pdf) new (substr pdf 1 (vl-string-position 45 pdf nil t)) ) (princ (strcat "\nOriginal file: " dir "/" pdf ".pdf" "\n New file: " dir "/" new ".pdf" ) ) (vl-file-rename (strcat dir "/" pdf ".pdf") (strcat dir "/" new ".pdf")) ) (princ) ) You may also wish to test for the existence of an existing file with the new name before attempting the rename operation. Edited September 18, 2020 by Lee Mac Quote Link to comment Share on other sites More sharing options...
rlx Posted September 18, 2020 Share Posted September 18, 2020 nice alternative Master Lee Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted September 18, 2020 Share Posted September 18, 2020 24 minutes ago, rlx said: nice alternative Master Lee Thanks! Quote Link to comment Share on other sites More sharing options...
PEACE-PLEASE Posted September 19, 2020 Share Posted September 19, 2020 10 hours ago, Lee Mac said: FWIW, you could refine the vl-directory-files filter and reduce the code to this: (defun c:renamefiles ( / dir new ) (setq dir "C:/Transmittals") (foreach pdf (vl-directory-files dir "*-layout*.pdf" 1) (setq pdf (vl-filename-base pdf) new (substr pdf 1 (vl-string-position 45 pdf nil t)) ) (princ (strcat "\nOriginal file: " dir "/" pdf ".pdf" "\n New file: " dir "/" new ".pdf" ) ) (vl-file-rename (strcat dir "/" pdf ".pdf") (strcat dir "/" new ".pdf")) ) (princ) ) You may also wish to test for the existence of an existing file with the new name before attempting the rename operation. Genious Quote Link to comment Share on other sites More sharing options...
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.