viewpoint-particle

Author Topic: Tricky bulk file rename  (Read 3008 times)

kennytmcp

  • Newbie
  • *
  • Posts: 1
Tricky bulk file rename
« on: June 26, 2013, 02:16:16 pm »
I needed to rename a folder of several hundred files, but I wanted to keep the first 6 characters and the last 8 characters
the file names were and needed to be 20 characters long. They are Pattern files for a lens cutter in the doctors office I work at.
An example of the file name is SILH-00000001234.PTN. Some one traced the patterns in the machine and named them SILH-0SPIRIT1234.PTN
This name didn't cause any issues, but This vendor does not have a "Spirit" line of products so I wanted to fix it.
It's been a while since I messed with WinBatch, A REALLY LONG TIME. Like before Y2K if I remember right.
I knew it could do what I wanted it to do and after an hour or so things kept falling into place in my memory.
I don't know if there was an easier way to do this, I'm sure there are some of you coders that have easier ideas, but here is how I got it done. 
Goal - Replace SPIRIT with 000000.

DirChange("C:\Users\kenny\Desktop\SILH")    ;set working dir.
List1 = FileItemize("C:\Users\kenny\Desktop\SILH\*.PTN")      ;list files in the dir I want to manupilate
ptncount=ItemCount(List1,@TAB)    ;File Count
For xx=1 TO ptncount            ;Loop = to the file count
ptn=ItemExtract(xx,List1,@TAB) ; get one file name at ta time
ptn1 = StrFixLeft(ptn, " ",8) ;fix string left to trim off all the way to the pattern number leaving the file name as 1234.PTN  8 characters     ::)
ptn2 = StrFixLeft(ptn1, "SILH-0000000",20) ; fix string left again, making the lenth 20 characaters using the desired padding string leaving new string ptn2
FileCopy(ptn, "C:\Users\kenny\Desktop\SILH\NEW\temp.ptn", @FALSE) ;copy files to a new folder and NEW name to save confusion. 
dirchange("C:\Users\kenny\Desktop\SILH\NEW\")  ; change to the new directory
filerename("temp.ptn",ptn2) ; rename temporary file name to the variable ptn2 the correct name
dirchange("C:\Users\kenny\Desktop\SILH\")
FileDelete( ptn )         ; Deletes original files

Next  ;finished loop so continue down.

Message("Thats It", "all files were copied and renamed")  ;end

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Tricky bulk file rename
« Reply #1 on: June 26, 2013, 02:31:14 pm »
Thank you for taking the time to share your solution! Welcome back to WinBatch!
Deana F.
Technical Support
Wilson WindowWare Inc.

kdmoyers

  • Full Member
  • ***
  • Posts: 227
Re: Tricky bulk file rename
« Reply #2 on: June 27, 2013, 03:56:15 am »
Nicely done!
-Kirby
The mind is everything; What you think, you become.