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.

Renaming Files on an Incremental Basis

Keywords: rename StrFixLeft

Question:

How would you go about renaming files on an incremental basis? I.E., FOO.DOC becomes FOO.001, etc. Actually, I want to use dates as extensions, as in N29 for November 29, 115 for January 15, etc. Didn't I read something about this in the user's guide tutorial?

Actually, I want to use dates as extensions, as in N29 for November 29, 115 for January 15, etc. Didn't I read something about this in the user's guide tutorial? Sample WIL file to take a file name "OldName", and rename the file so that the file extension becomes a month/day code for the file.

Answer:

Here's an example:
OldName="C:\TEMP\ABC.TXT"      ;Get the filename to rename
fp=FilePath(OldName)           ;Pick apart file name
fr=FileRoot(OldName)
Now=TimeYmdHms()               ;Get current time in computer format
month=ItemExtract(2,Now,":")   ;Extract month string
day=ItemExtract(3,Now,":")     ;Extract day string

; letters ABCDEFGHIJKL used for months so that extensions will
; sort in date order.  change as desired

mletter=StrSub("ABCDEFGHIJKL",month,1)   ; Convert month code to
                                       ; single letter
NewName=StrCat(fp,fr,".",mletter,day)    ; Concat string together to build new filename
FileMove(OldName,NewName,0)              ; Move file to new name
                                       ; (in case user also changes drives)

Question:

I know there are keystrokes to do this, but my problem is I need to have 8-number (0 filled) prefix that is incremented by 1 each time from whatever the starting filename is.

For example, if I have files 000038990.tif through 000147680.tif (I never have to rename that many files...at least yet), I will copy them to a new subdirectory in which in want them to start at 000200000.tif and continue until all files are renamed 00020000.tif, 00020001.tif, etc. I can manually give the routine count of files to rename, & I can give the starting image number, but what command do I use to have it zero-fill to the left?

Answer:

  1. Don't (please please don't) do this with keystrokes. Use FileRename to rename files.

  2. Zero fill to left. If you have the latest and greatest version, see the StrFixLeft function in the WIL help file.

  3. No latest and greatest??
    a="12345.tif"
    a=StrCat(StrSub("0000000000000",1,12-StrLen(a)),a)
    Message("New a",a)
    
  4. Here is a renamer I use for jpeg files. It renames its file with a new name based on the file timestamp. Notice that it goes thru an intermediate *.jpeg file to avoid name conflicts that *might* occur when renaming a file that was already somehow renamed...
    jpglist=FileItemize("*.jpg")
    jpgcount=ItemCount(jpglist,@TAB)
    For xx=1 To jpgcount
       jpg=ItemExtract(xx,jpglist,@TAB)
       ft=FileYmdHms(jpg)
       ft=StrReplace(ft,":","")
       jpg2=StrCat("A",ft,".jpeg")
       FileRename(jpg,jpg2)
    Next
    FileRename("*.jpeg","*.jpg")
    Message("All","Doned")
    
Or you can do it like this:
   n=StrCat(StrFill("0",8-StrLen(n)),n)


Question:

Problem... I have a directory that has 100 files in it. They start at 00.tif and end with 99.tif. I need to rename every file in the following sequence.

Example:

Old Filename New Filename
00.tif = NH006653.tif

**************************
00.tif = NH006653.tif
01.tif = NH006654.tif
02.tif = NH006655.tif
03.tif = NH006656.tif
etc.. Then the new filename changes to 

48.tif = NH006700.tif
49.tif = NH006701.tif
50.tif = NH006702.tif
51.tif = NH006703.tif

The list ends at
99.tif = NH006751.tif
I have several other directories that contain 99 .tif files that have to be renamed as well. If I could get the logic to rename these I would be able to modify the script to work for the other folders.

Answer:

Something like:
:Tp
flags=1|2
a = AskDirectory("Select Directory", "\\atl\sys\apps\sumcases\novartis\images\atlnov01", "","Are you sure?",flags)

list = FileItemize("%a%*.tif") ;all tif files

q = AskYesNo('STOP!', 'ARE YOU SURE YOU WANT TO RENAME%@CRLF%THE CURRENT BEG DOC NAMES?')
If q == @YES Then GoSub DoCopy
If q == @NO Then GoSub Tp


:DoCopy
ask = AskLine("STOP!", "Please enter the FIRST 6 Characters of%@CRLF%the Beg Doc Name. (i.e. NH0069)",
"NH00")
docname = StrUpper(ask)
;init = AskLine("STOP!", "Please enter the FIRST Initial Number%@CRLF%of the Beg Doc Name. (i.e. 21)", "33")

ask2 = AskLine("STOP!", "Please enter the FIRST 6 Characters of%@CRLF%the 2nd Beg Doc Name. (i.e. NH0070)",
"NH00")
docname2 = StrUpper(ask2)
;init2 = AskLine("STOP!", "Please enter the FIRST Initial Number%@CRLF%of the 2nd Beg Doc Name. (i.e. 21)", "00")

BoxOpen("Processing File Names", "Please Wait...")

list2 = ItemSort (list, @TAB)
count=ItemCount(list2,@TAB)

init=33
For i=1 To count
   file=ItemExtract(i,list2,@TAB)
   rootname=FileRoot(file)
   If rootname>=67
      flag=1
      Break
   EndIf

   newname=StrCat("%docname%",StrFixLeft(init,0,2),".tif")
   ;newname=strcat("NH00xx",init,".tif")

   BoxText ("%@CRLF%From - %file%%@CRLF%%@CRLF%To - %newname%")
   FileRename("%a%%file%","%a%%newname%")

   init=init+1
Next

If flag==1 Then Goto newinit

While @FALSE
   :newinit

   init2=00
   For i=68 To count
      list2 = ItemSort (list, @TAB)
      file=ItemExtract(i,list2,@TAB)
      rootname=FileRoot(file)

      newname=StrCat("%docname2%",StrFixLeft(init2,0,2),".tif")
      ; newname=strcat("NH00xx",init,".tif")

      BoxText ("%@CRLF%From - %file%%@CRLF%%@CRLF%To - %newname%")
      FileRename("%a%%file%","%a%%newname%")
      init2=init2+1
   Next
   BoxShut()
   GoSub Tp
EndWhile

Article ID:   W13244
Filename:   Renaming Files on an Incremental Basis.txt
File Created: 2007:03:29:09:11:32
Last Updated: 2007:03:29:09:11:32