WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

File Operations

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Tricky Bulk FileRename

 Keywords: File Rename FileRename StrFixLeft Pad 

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. Goal - Replace SPIRIT with 000000.
; Code posted to WinBatch Forum 2013.06.26 by kennytmcp 
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

Article ID:   W17877
Filename:   Tricky Bulk FileRename .txt
File Created: 2013:06:27:08:31:02
Last Updated: 2013:06:27:08:31:02