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:
Or you can do it like this:
- Don't (please please don't) do this with keystrokes. Use FileRename to rename files.
- Zero fill to left. If you have the latest and greatest version, see the StrFixLeft function in the WIL help file.
- No latest and greatest??
a="12345.tif" a=strcat(strsub("0000000000000",1,12-strlen(a)),a) Message("New a",a)- 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")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.tifI 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: W13244Filename: Renaming Files on an Incremental Basis.txt