Rename Files from Attributes and text within the file

Rename Files from Attributes and text within the file

cslatteryAXGRE
Advocate Advocate
517 Views
8 Replies
Message 1 of 9

Rename Files from Attributes and text within the file

cslatteryAXGRE
Advocate
Advocate

Hello

I am looking for a way to rename a lot of files with multiple pieces of text or multiple attributes that are in the file. I would like the ability to do it both ways, but it does not have to be in the same code. Either LSP or VBA. I did find a VBA for Excel that did this but it only uses one piece of text, it works by selecting a window around the text. I also found a LSP that will do this with one attribute, but I would like to be able to use more that one attribute or more that one piece of text.

0 Likes
518 Views
8 Replies
Replies (8)
Message 2 of 9

paullimapa
Mentor
Mentor

Please clarify what you mean by rename a file. Where is this file? Is the current file name in the Text or Attribute value? What should the new file name be?


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

cslatteryAXGRE
Advocate
Advocate

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

0 Likes
Message 4 of 9

paullimapa
Mentor
Mentor

I see how the program you found works.  It uses odbx to open in the background the list of drawings you select from a folder say c:\temp. When the first drawing in the folder is opened say Drawing1.dwg and the block & attribute is found, the value only contains a string that refers to a file name like A101_Floor Plan.  Then it creates an associate list of the current opened drawing name c:\temp\Drawing1.dwg with this new name string A101_Floor Plan.  The routine then closes and opens the next drawing until all drawings are processed in this folder.  The lisp function then concludes by cycling through the association list to rename say c:\temp\Drawing1.dwg to A101_Floor Plan.dwg

I see why it only works with one attribute because you only have one drawing that it currently opens to rename to that new value found in the attribute.  What would you do when you found the next attribute value in the same drawing? How would you instruct it to rename which drawing file name with that attribute value?


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

cslatteryAXGRE
Advocate
Advocate

That is what I am trying to find out how to do. I want to use two or three attribute values in that file to generate the file name. If it can be done at all.

What I did do is I created an extra attribute that is populated with the values from the other attributes I wanted to use, then I use this code to read that new attribute. It does get the job done. I just wanted to see if there was a better way to do this.

0 Likes
Message 6 of 9

paullimapa
Mentor
Mentor

The problem is logistics. you cannot magically generate a drawing file name unless one already exists. 

 


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

cslatteryAXGRE
Advocate
Advocate

File name does exist. I want to change it to match the drawing number and name that is in the drawing.

0 Likes
Message 8 of 9

paullimapa
Mentor
Mentor

Well, the program is only as intelligent as the information you provide.

You can very easily generate a list of drawings like the lisp code does from a selected folder.

Then go through line by line and determine based on the drawing file name that exists now to determine what the new filename should be so you can rename it.

So if you have a matching drawing file name pattern in mind to search for in the list then you can code this in to do the rename without the need for searching for any attribute values.

 


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

Sea-Haven
Mentor
Mentor

To rename a open dwg you need to use saveas. But you can close a dwg and use a bat file to rename a dwg, the lisp looking in the open file would write the bat file and run it using "Shell" method. Another way would be to use VL-FILE-RENAME, but again the file to be renamed must be closed. Another is Accoreconsole could look in multiple files making the bat file or possibly VL-FILE-RENAME, to rename multiples rather than 1 dwg. VL-FILE-RENAME will not rename if it exists.

0 Likes