Author Topic: Renaming Files in a Directory using ObjectDBX  (Read 3796 times)

0 Members and 1 Guest are viewing this topic.

GDF

  • Water Moccasin
  • Posts: 2081
Renaming Files in a Directory using ObjectDBX
« on: March 06, 2017, 02:35:07 PM »
I'm trying to renaming files by selecting a directory folder and renaming the files using ObjectDBX.

1). Is this even possible?
2). Could it be done by using part of the folder name, like the first part of the drawing name: 170021 and substituting it with the six numbers of the folder
name. Ex: "170021 Smith BasePlan.dwg" found in folder name "170034 Smith", the drawing would be renamed to "170034 Smith BasePlan.dwg"?
I would like to use ObjectDBX to accomplish this.

Here are some example of using only AutoLisp and not ObjectDBX.

Code: [Select]
(defun C:RN-1  (/ filen1 filen2 num)
  (setq filen1 (strcat (getvar "dwgprefix") (getvar "dwgname")))
  (setq num (strlen
              (substr (getvar "dwgprefix") 12 (- (strlen (getvar "dwgprefix")) 12))))
  (prompt "\n* Saveas Open drawing with New Folder Name...")
  (setq filen2
         (strcat (substr (getvar "dwgprefix") 12 (- (strlen (getvar "dwgprefix")) num))
                 (substr (getvar "dwgname") 13 (- (strlen (getvar "dwgprefix")) 11))))
  (command "_.saveas" "" (strcat (getvar "dwgprefix") filen2))
  (dos_rename filen1 filen2)
  (dos_delete filen1)
  (princ))

(defun c:RN-2  (/ old new f)
  (if (null dos_about)
    (arxload "doslib2k.arx"))
  (if (null *dir*)
    (setq *dir* (getvar "dwgprefix")))
  (setq old (getstring T "Enter Old Name: "))
  (setq new (getstring T "Enter New Name: "))
  (setq f (dos_getfilem "Select File(s)" *dir* "All Files (*.*)|*.*"))
  (cond (f
         (setq *dir* (car f))
         (setvar "CMDECHO" 0)
         (foreach
                dwgfile  (acad_strlsort (cdr f))
           (vl-file-rename
             (strcat *dir* dwgfile)
             (strcat *dir* (vl-string-subst new old dwgfile))))))
  (if dos_about
    (arxunload "doslib2k.arx"))
  (princ))

Any help would be appreciated

Gary
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #1 on: March 06, 2017, 02:49:52 PM »
I did find this online...
Maybe it can be modified to do the work I want...I just don't have the expertize to do it. So any help...
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Renaming Files in a Directory using ObjectDBX
« Reply #2 on: March 06, 2017, 03:54:54 PM »
vl-directory-files and vl-file-rename (trapped) should suffice unless I'm missing something that necessitates opening the files via ObjectDBX. Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Renaming Files in a Directory using ObjectDBX
« Reply #3 on: March 06, 2017, 05:46:46 PM »
Quick example, using my Browse for Folder function:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:renamedwgs ( / dir str )
  2.     (cond
  3.         (   (not (setq dir (LM:browseforfolder "Select folder of drawings to be renamed" nil 0)))
  4.             (princ "\n*Cancel*")
  5.         )
  6.         (   (wcmatch (setq str (substr dir (+ 2 (cond ((vl-string-position 92 dir nil t)) (-1))) 6)) "~######")
  7.             (princ "\nThe selected folder does not start with 6-digits.")
  8.         )
  9.         (   (foreach dwg (vl-directory-files dir "*.dwg" 1)
  10.                 (and (wcmatch dwg "###### *.dwg")
  11.                      (or (wcmatch dwg (strcat str "*"))
  12.                          (vl-file-rename (strcat dir "\\" dwg) (LM:uniquefilename (strcat dir "\\" str (substr dwg 7))))
  13.                      )
  14.                 )
  15.             )
  16.         )
  17.     )
  18.     (princ)
  19. )
  20.  
  21. ;; Unique Filename  -  Lee Mac
  22. ;; Returns a filename suffixed with the smallest integer required for uniqueness
  23.  
  24. (defun LM:uniquefilename ( fnm )
  25.     (if (findfile fnm)
  26.         (apply
  27.            '(lambda ( pth bse ext / tmp )
  28.                 (setq tmp 1)
  29.                 (while (findfile (setq fnm (strcat pth bse "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  30.             )
  31.             (fnsplitl fnm)
  32.         )
  33.     )
  34.     fnm
  35. )

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #4 on: March 06, 2017, 08:15:32 PM »
WOW

That you so much Lee.

Awesome Awesome Awesome
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Renaming Files in a Directory using ObjectDBX
« Reply #5 on: March 07, 2017, 08:04:20 AM »
No worries!  :-)

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #6 on: May 04, 2017, 10:06:32 PM »
On a different note: How would I modify Lee's code above to replace a partial string within a file name?

Ex:
170091 Leary Computer Prelim 01.dwg
170091 Leary Construction Docs 01.dwg

Replacing "Computer Prelim" with "Construction Docs" in the example above...

Lost in digital space.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #7 on: May 08, 2017, 09:40:39 PM »
On a different note: How would I modify Lee's code above to replace a partial string within a file name?

Ex:
170091 Leary Computer Prelim 01.dwg
170091 Leary Construction Docs 01.dwg

Replacing "Computer Prelim" with "Construction Docs" in the example above...

Lost in digital space.

Is the above string replace possible?
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Renaming Files in a Directory using ObjectDBX
« Reply #8 on: May 09, 2017, 04:04:03 AM »
You could also try the code from my reply #7 in this thread https://www.theswamp.org/index.php?topic=53012.0
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #9 on: May 09, 2017, 09:59:11 AM »
So I could do this at the command line:

(command "saveas" "" (vl-string-subst "Computer Prelim" "Blockout" (strcat (getvar "dwgprefix")(getvar "dwgname"))) "y")
(command "saveas" "" (vl-string-subst "Construction Docs" "Computer Prelim" (strcat (getvar "dwgprefix")(getvar "dwgname"))) "y")

Now, how would I do this using ObjectDBX and vl-file-rename?
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Renaming Files in a Directory using ObjectDBX
« Reply #10 on: May 09, 2017, 10:14:00 AM »
ObjectDBX isn't necessary for using (vl-file-rename ...).

Gotta be careful calling SAVEAS, as it makes the new drawing file current in the editor.  In the past I've called QSAVE and used (vl-file-copy ...) on the current document with good results.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Renaming Files in a Directory using ObjectDBX
« Reply #11 on: May 09, 2017, 02:45:46 PM »
Hi Gary,

Try the following:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:dwgfr ( / dir fnd new old rep )
  2.     (cond
  3.         (   (= "" (setq fnd (getstring t "\nSpecify string to find: "))))
  4.         (   (null
  5.                 (setq rep (getstring t (strcat "\nSpecify string to replace \"" fnd "\": "))
  6.                       dir (LM:browseforfolder "Select folder of drawings" nil 0)
  7.                 )
  8.             )
  9.             (princ "\n*Cancel*")
  10.         )
  11.         (   (foreach dwg (vl-directory-files dir "*.dwg" 1)
  12.                 (if (/= (setq old (vl-filename-base dwg))
  13.                         (setq new (LM:stringsubst rep fnd old)) ""
  14.                     )
  15.                     (vl-file-rename (strcat dir "\\" old ".dwg") (LM:uniquefilename (strcat dir "\\" new ".dwg")))
  16.                 )
  17.             )
  18.         )
  19.     )
  20.     (princ)
  21. )
  22.  
  23. ;; String Subst  -  Lee Mac
  24. ;; Substitutes a string for all occurrences of another string within a string.
  25.  
  26. (defun LM:stringsubst ( new old str / pos )
  27.     (if (setq pos (vl-string-search (strcase old) (strcase str)))
  28.         (strcat (substr str 1 pos) new (LM:stringsubst new old (substr str (+ 1 pos (strlen old)))))
  29.         str
  30.     )
  31. )
  32.  
  33. ;; Unique Filename  -  Lee Mac
  34. ;; Returns a filename suffixed with the smallest integer required for uniqueness
  35.  
  36. (defun LM:uniquefilename ( fnm )
  37.     (if (findfile fnm)
  38.         (apply
  39.            '(lambda ( pth bse ext / tmp )
  40.                 (setq tmp 1)
  41.                 (while (findfile (setq fnm (strcat pth bse "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  42.             )
  43.             (fnsplitl fnm)
  44.         )
  45.     )
  46.     fnm
  47. )
  48.  

Again, the above uses my Browse for Folder function.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #12 on: May 09, 2017, 06:04:32 PM »
Lee

Thanks again. You make the impossible look so easy.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

GDF

  • Water Moccasin
  • Posts: 2081
Re: Renaming Files in a Directory using ObjectDBX
« Reply #13 on: May 09, 2017, 09:36:15 PM »
Thanks again Lee

AWESOME

This saves me time renaming files based upon my clients preferences:


Heres is LeeMac"s code modified to my usage:

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; File Name Repace Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun RNF:TypeCP  (/ dir fnd new old rep)
  (cond ;;((= "" (setq fnd (getstring t "\nSpecify string to find: "))))
        ((= "" (setq fnd "Blockout")))
        ;;((null (setq rep (getstring t (strcat "\nSpecify string to replace \"" fnd "\": "))
        ((null (setq rep "Computer Prelim "
                     dir (LM:browseforfolder
                           "Select Folder for Drawings to be Renamed"
                           "F:\\Service"
                           0)))
         (princ "\n*Cancel*"))
        ((foreach
                dwg  (vl-directory-files dir "*.dwg" 1)
           (if (/= (setq old (vl-filename-base dwg))
                   (setq new (LM:stringsubst rep fnd old))
                   "")
             (vl-file-rename
               (strcat dir "\\" old ".dwg")
               (LM:uniquefilename (strcat dir "\\" new ".dwg")))))))
  (princ))

(defun RNF:TypeCD  (/ dir fnd new old rep)
  (cond ;;((= "" (setq fnd (getstring t "\nSpecify string to find: "))))
        ((= "" (setq fnd "Computer Prelim")))
        ;;((null (setq rep (getstring t (strcat "\nSpecify string to replace \"" fnd "\": "))
        ((null (setq rep "Construction Docs "
                     dir (LM:browseforfolder
                           "Select Folder for Drawings to be Renamed"
                           "F:\\Service"
                           0)))
         (princ "\n*Cancel*"))
        ((foreach
                dwg  (vl-directory-files dir "*.dwg" 1)
           (if (/= (setq old (vl-filename-base dwg))
                   (setq new (LM:stringsubst rep fnd old))
                   "")
             (vl-file-rename
               (strcat dir "\\" old ".dwg")
               (LM:uniquefilename (strcat dir "\\" new ".dwg")))))))
  (princ))
;; String Subst  -  Lee Mac
;; Substitutes a string for all occurrences of another string within a string.
(defun LM:stringsubst  (new old str / pos)
  (if (setq pos (vl-string-search (strcase old) (strcase str)))
    (strcat (substr str 1 pos)
            new
            (LM:stringsubst new old (substr str (+ 1 pos (strlen old)))))
    str))
;; Unique Filename  -  Lee Mac
;; Returns a filename suffixed with the smallest integer required for uniqueness
(defun LM:uniquefilename  (fnm)
  (if (findfile fnm)
    (apply '(lambda (pth bse ext / tmp)
              (setq tmp 1)
              (while (findfile
                       (setq fnm (strcat pth bse "(" (itoa (setq tmp (1+ tmp))) ")" ext)))))
           (fnsplitl fnm)))
  fnm)
(defun RNF:Service  (/ dir str)
  (cond ((not (setq dir (LM:browseforfolder
                          "Select Folder for Drawings to be Renamed"
                          "F:\\Service"
                          0)))
         (princ "\n*Cancel*"))
        ((wcmatch
           (setq str (substr dir
                             (+ 2
                                (cond ((vl-string-position 92 dir nil t))
                                      (-1)))
                             6))
           "~######")
         (princ "\n* The selected folder does not start with 6-digits. *"))
        ((foreach
                dwg  (vl-directory-files dir "*.dwg" 1)
           (and (wcmatch dwg "###### *.dwg")
                (or (wcmatch dwg (strcat str "*"))
                    (vl-file-rename
                      (strcat dir "\\" dwg)
                      (LM:uniquefilename (strcat dir "\\" str (substr dwg 7)))))))))
  (princ))
;; Unique Filename  -  Lee Mac
;; Returns a filename suffixed with the smallest integer required for uniqueness
(defun LM:uniquefilename  (fnm)
  (if (findfile fnm)
    (apply '(lambda (pth bse ext / tmp)
              (setq tmp 1)
              (while (findfile
                       (setq fnm (strcat pth bse "(" (itoa (setq tmp (1+ tmp))) ")" ext)))))
           (fnsplitl fnm)))
  fnm)
;;;
(defun C:RNF  (/ tmp)
  (initget "S CP CD")
  (setq tmp (getkword
              "\n* Rename Service-number or File-type: [Service/CP/CD] <Service>: "))
  (if (not tmp)
    (setq tmp "S"))
  (cond ((= tmp "S") (RNF:Service))
        ((= tmp "CP") (RNF:TypeCP))
        ((= tmp "CD") (RNF:TypeCD)))
  (princ))
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: Renaming Files in a Directory using ObjectDBX
« Reply #14 on: May 10, 2017, 08:01:12 AM »
You're most welcome Gary.

This may be clearer for you:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:rnf ( / dir tmp )
  2.     (initget "Service CP CD")
  3.     (if (setq tmp (getkword "\nRename Service-number or File-type [Service/CP/CD] <Service>: ")
  4.               dir (LM:browseforfolder "Select folder of drawings to be renamed" "F:\\Service" 0)
  5.         )
  6.         (cond
  7.             (   (= tmp "CP") (dwgfr dir "Blockout"        "Computer Prelim "  ))
  8.             (   (= tmp "CD") (dwgfr dir "Computer Prelim" "Construction Docs "))
  9.             (   (renamedwgs dir))
  10.         )
  11.     )
  12.     (princ)
  13. )
  14.  
  15. (defun renamedwgs ( dir / str )
  16.     (cond
  17.         (   (wcmatch (setq str (substr dir (+ 2 (cond ((vl-string-position 92 dir nil t)) (-1))) 6)) "~######")
  18.             (princ "\nThe selected folder does not start with 6-digits.")
  19.         )
  20.         (   (foreach dwg (vl-directory-files dir "*.dwg" 1)
  21.                 (and (wcmatch dwg "###### *.dwg")
  22.                      (or (wcmatch dwg (strcat str "*"))
  23.                          (vl-file-rename (strcat dir "\\" dwg) (LM:uniquefilename (strcat dir "\\" str (substr dwg 7))))
  24.                      )
  25.                 )
  26.             )
  27.         )
  28.     )
  29. )
  30.  
  31. (defun dwgfr ( dir fnd rep / new old )
  32.     (foreach dwg (vl-directory-files dir "*.dwg" 1)
  33.         (if (/= (setq old (vl-filename-base dwg))
  34.                 (setq new (LM:stringsubst rep fnd old)) ""
  35.             )
  36.             (vl-file-rename (strcat dir "\\" old ".dwg") (LM:uniquefilename (strcat dir "\\" new ".dwg")))
  37.         )
  38.     )
  39. )
  40.  
  41. ;; String Subst  -  Lee Mac
  42. ;; Substitutes a string for all occurrences of another string within a string.
  43.  
  44. (defun LM:stringsubst ( new old str / pos )
  45.     (if (setq pos (vl-string-search (strcase old) (strcase str)))
  46.         (strcat (substr str 1 pos) new (LM:stringsubst new old (substr str (+ 1 pos (strlen old)))))
  47.         str
  48.     )
  49. )
  50.  
  51. ;; Unique Filename  -  Lee Mac
  52. ;; Returns a filename suffixed with the smallest integer required for uniqueness
  53.  
  54. (defun LM:uniquefilename ( fnm )
  55.     (if (findfile fnm)
  56.         (apply
  57.            '(lambda ( pth bse ext / tmp )
  58.                 (setq tmp 1)
  59.                 (while (findfile (setq fnm (strcat pth bse "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  60.             )
  61.             (fnsplitl fnm)
  62.         )
  63.     )
  64.     fnm
  65. )
  66.