The files are just in explorer. The files are named something like Draw1.dwg. In the file is the drawing number and name, like A101 and Floor Plan. These could be in either an attributed block or just plain text. I want to be able to utilize lsp or VBA to rename all the files in a folder to A101_Floor Plan.dwg by pulling this from inside the drawing. I found a lsp that someone else made and it does work very well, but I need it to select from more than one attribute. I cannot seem to get it to do that.
Here is the lsp code
(defun c:BatchRename (/ *error* Directory-Dia BlkName TagName Dir dbxApp FileName RenameList )
(vl-load-com)
(defun *error* (msg)
(if dbxApp (vlax-release-object dbxApp))
(setq dbxApp nil)
(vl-bt)
)
;---------------------------------------------------------
(defun Directory-Dia ( Message / sh folder folderobject result)
;; 16 Will let you type in the path
;; 64 Will let you create a new folder
(vl-load-com)
(setq sh
(vla-getInterfaceObject
(vlax-get-acad-object)
"Shell.Application"
)
)
(setq folder
(vlax-invoke-method
sh
'BrowseForFolder
(vla-get-HWND (vlax-get-Acad-Object))
Message
0 ; This is the bit number to change.
)
)
(vlax-release-object sh)
(if folder
(progn
(setq folderobject
(vlax-get-property folder 'Self)
)
(setq result
(vlax-get-property FolderObject 'Path)
)
(vlax-release-object folder)
(vlax-release-object FolderObject)
(if (/= (substr result (strlen result)) "\\")
(setq result (strcat result "\\"))
result
)
)
)
)
;--------------------------------------------------------------------------------------
(setq BlkName "CIR-NOTE") ; update to your block name
(setq TagName "NOTE") ; update to your attribute's tag value
(if (setq Dir (Directory-Dia "Select directory to rename."))
(progn
(setq dbxApp
(if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
(vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
(vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
)
)
(foreach file (mapcar (function (lambda (x) (strcat Dir x))) (vl-directory-files Dir "*.dwg" 1))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-Open (list dbxApp file)))
(prompt (strcat "\n *Error opening file: " file))
(vlax-for lo (vla-get-Layouts dbxApp)
(vlax-for obj (vla-get-Block lo)
(if
(and
(= (vla-get-ObjectName obj) "AcDbBlockReference")
(= (vla-get-Name obj) BlkName)
)
(foreach att (vlax-invoke obj 'GetAttributes)
(if (= (vla-get-TagString att) TagName)
(setq FileName (vla-get-TextString att))
)
)
)
)
)
)
(setq RenameList (cons (cons file FileName) RenameList))
(setq FileName nil)
)
)
)
(vlax-release-object dbxApp)
(setq dbxApp nil)
(foreach i RenameList
(if
(or
(null (cdr i))
(not (vl-file-rename (car i) (strcat Dir (cdr i) ".dwg")))
)
(prompt "\n *Was not able to rename: " (car i))
)
)
(princ)
)