Archived Boards > WinBatch Script Exchange

Tricky bulk file rename


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
FileDelete( ptn )         ; Deletes original files

Next  ;finished loop so continue down.

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

Thank you for taking the time to share your solution! Welcome back to WinBatch!

Nicely done!


[0] Message Index

Go to full version