Author Topic: Batch File Renaming An Entire Dir  (Read 4574 times)

0 Members and 1 Guest are viewing this topic.

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Batch File Renaming An Entire Dir
« on: March 21, 2018, 12:03:37 PM »
I have a bit of LISP code for some batch processing a directory, but I'm looking to add zero's to every sub directory name as well as the associated files within each sub directory contained in a parent directory, and I'm thinking LISP would be more of a cumbersome work around than anything. Can anyone point me in some sort of direction to accomplish this?

Sample:

C:\Proposals\P123 - Some Name\P123 - Some Name.dwg

Change to:

C:\Proposals\P000123 - Some Name\P000123 - Some Name.dwg

I'm not very familiar with much outside of LISP, so any help at all would be appreciated.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Batch File Renaming An Entire Dir
« Reply #1 on: March 21, 2018, 02:14:14 PM »
Do a dos batch file.
Many examples on line.
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Re: Batch File Renaming An Entire Dir
« Reply #2 on: March 21, 2018, 03:41:53 PM »
Do a dos batch file.
Many examples on line.

I've been looking at some things, trying to comprehend. It's not something I've done before though.

Would you happen to have a link to a decent example?

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Batch File Renaming An Entire Dir
« Reply #3 on: March 21, 2018, 04:09:12 PM »
Alright, let's start at the file name; how--in Autolisp (since that is what you know)--would you parse the file name?

"P123 - Some Name" --> "P<000>123 - Some Name"

Next I'd like you to address the xref paths -i.e. will this break xref paths if you start renaming folders and files? And what happens if someone's accessing a file or folder (the network <WINDOWS> server counts as "someone" too).

BTW, there must be thousands of links for renaming files. And, I've even found a few that list "the best options for...". Like:
https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Rod

  • Newt
  • Posts: 185
Re: Batch File Renaming An Entire Dir
« Reply #4 on: March 21, 2018, 05:22:48 PM »
I've found "advanced renamer" to fit all of my needs, its pretty good for a lot of renaming tasks.
Cheers, Rod.
"All models are wrong, some models are useful" - George Box

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Re: Batch File Renaming An Entire Dir
« Reply #5 on: March 22, 2018, 09:56:29 AM »
Alright, let's start at the file name; how--in Autolisp (since that is what you know)--would you parse the file name?

"P123 - Some Name" --> "P<000>123 - Some Name"

Next I'd like you to address the xref paths -i.e. will this break xref paths if you start renaming folders and files? And what happens if someone's accessing a file or folder (the network <WINDOWS> server counts as "someone" too).

BTW, there must be thousands of links for renaming files. And, I've even found a few that list "the best options for...". Like:
https://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/

No, these drawings are sans Xrefs, so no problem there.

As far as parsing the filename, grab the directory name then search that directory for a matching filename. If I were to go the LISP route, all I have to go on is a bit of code that I mashed together some years ago, but i'm just now getting back into things of that nature, so tweaking it to what I need is a little cumbersome since I've forgotten a lot of what I knew. I've been involved in other roles and just now coming up for air where I have time to automate and correct a few things I've wanted to do but didn't have the time. Here's the original code, it compiles a txt file with the name of each subdirectory in a given directory:

Code: [Select]
(DEFUN C:dir_list2 ()

;;;;;;;;;;;;;;;;;;;;;;;;;Select directory to be processed

(setq dfil (getfiled "Select a **FILE** in the directory you want to list, then click on OPEN" "y:/" "*" 0))
(setq wutdir (vl-filename-directory dfil))
(setq wutfiles (vl-directory-files wutdir nil -1))
(setq dwglistfile (strcat wutdir "\\dir_list2.txt"))
(SETQ dwgfiler (OPEN dwglistfile "w"))
(WRITE-LINE wutdir dwgfiler)
(foreach n wutfiles
(WRITE-LINE n dwgfiler)
(princ)
)
(alert "\n***File DIR_LIST2.TXT has been created in the directory you chose.***")
(CLOSE dwgfiler)
(startapp "notepad" dwglistfile)


(princ)


);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;DEFUN

I appreciate the link, it's one that I've looked at. Still trying to comprehend it.

I've found "advanced renamer" to fit all of my needs, its pretty good for a lot of renaming tasks.
Cheers, Rod.

Thank you, this looks like something I could use.

If I had the time to relearn LISP like I used to know it, I'd more than likely tackle this without asking for help, but I'm trying to do this while things are slow... and that doesn't happen, so this is a small window of opportunity and it's as if LISP is all brand new to me again. I understand how crass it can be to come here asking for people to just solve your problems for you, so I'm doing my best to not be that guy, and still find a solution, although it may be a quick one at that.

Again, thank you for the help! Time to dig in.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Batch File Renaming An Entire Dir
« Reply #6 on: March 22, 2018, 11:11:47 AM »
Not code, but I've used THIS for years and it works really well.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Re: Batch File Renaming An Entire Dir
« Reply #7 on: March 22, 2018, 11:59:27 AM »
Not code, but I've used THIS for years and it works really well.

Thanks ron!

GDF

  • Water Moccasin
  • Posts: 2081
Re: Batch File Renaming An Entire Dir
« Reply #8 on: March 22, 2018, 12:03:44 PM »
Here is an example:

The files are currently named like this:

Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg

And I'd like to change them to:

December 001.jpg
December 002.jpg
December 003.jpg






@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=Vacation2010
SET new=December
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Re: Batch File Renaming An Entire Dir
« Reply #9 on: March 22, 2018, 02:21:18 PM »
Here is an example:

The files are currently named like this:

Vacation2010 001.jpg
Vacation2010 002.jpg
Vacation2010 003.jpg

And I'd like to change them to:

December 001.jpg
December 002.jpg
December 003.jpg






@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=Vacation2010
SET new=December
for /f "tokens=*" %%f in ('dir /b *.jpg') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)

If they were all the same name that would work, but there are different variations as in:

P01 - Some Drawing
P567 - Some Other Drawing

Thank you though.

I've found "advanced renamer" to fit all of my needs, its pretty good for a lot of renaming tasks.
Cheers, Rod.

This did exactly what I needed. I just added "000" after the "P" (after adjusting 1-99 first). I had to go through the preview to adjust some of the files/directories that were created by other users who didn't follow directions, but it worked nicely.

After almost 13 years of proposals, I was coming up on number 1000 and I didn't think I'd be here that long when I first started doing this. It was just a job to get me by, but well, here I am.

Thanks again for the help. Hopefully I can return the favor at some point.

GDF

  • Water Moccasin
  • Posts: 2081
Re: Batch File Renaming An Entire Dir
« Reply #10 on: March 28, 2018, 12:41:51 PM »
Here is an example LeeMac helped me with...

Code: [Select]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; File Name Replace Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:RNF ( / dir tmp )
    (initget "Service CP CD")
    (if (setq tmp (getkword "\n* Rename Service-number or File-type [Service/CP/CD] <Service>: ")
              dir (LM:browseforfolder "Select folder of drawings to be renamed" "F:\\Services\\" 0)
        )
        (cond
            (   (= tmp "CP") (dwgfr dir "Blockout"        "Computer Prelim "  ))
            (   (= tmp "CD") (dwgfr dir "Computer Prelim" "Construction Docs "))
            (   (renamedwgs dir))
        )
    )
    (princ)
)
(defun renamedwgs ( dir / str )
    (cond
        (   (wcmatch (setq str (substr dir (+ 2 (cond ((vl-string-position 92 dir nil t)) (-1))) 6)) "~######")
            (princ "\nThe 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))))
                     )
              )
            )
        )
    )
)
(defun dwgfr ( dir fnd rep / new old )
    (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")))
        )
    )
)
;; 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 c:RNF-N  (/ dir fnd new old rep)
  (cond ((= "" (setq fnd (getstring t "\n* Specify string to find <Existing>: "))))
        ((null (setq rep (getstring t (strcat "\n* Specify string to replace <New>\"" fnd "\": "))
                     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 C:RNFBO  ()   
  (command
    "saveas"
    ""
    (vl-string-subst
      "Computer Prelim"
      "Blockout"
      (strcat (getvar "dwgprefix") (getvar "dwgname")))
    "y") 
  (princ))
;;;
(defun C:RNFCP  ()   
  (command
    "saveas"
    ""
    (vl-string-subst
      "Construction Docs"
      "Computer Prelim"
      (strcat (getvar "dwgprefix") (getvar "dwgname")))
    "y") 
  (princ))

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

DeeGeeCees_V.2.0

  • Newt
  • Posts: 154
Re: Batch File Renaming An Entire Dir
« Reply #11 on: March 29, 2018, 09:20:28 AM »
Thank you, GDF. Lots of useful code in there.