Jump to content

AutoLisp to rename PDFs and remove "-Layout1" from published files


Recommended Posts

Posted

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

 

Posted (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 by rlx
  • Thanks 1
Posted (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 by plackowski
Added a check to ensure the file name contains "layout" before trying to remove anything.
Posted (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 by Lee Mac
Posted
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

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.

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