Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to rename or add Nth character in filenames

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello... I have files in the format:

xxxxx_1.tif
xxxxx_2.tif
yyyyy_10.tif
YYYYY_11.tif
zzzzz_100.tif
zzzzz_101.tif

I need all numbers after the _ to be three characters:
xxxxx_001.tif
xxxxx_002.tif
yyyyy_010.tif
YYYYY_011.tif
zzzzz_100.tif
zzzzz_101.tif

Can anyone think of a way I can do this with a vb script? All of my batch file options have failed. I'll have thousands of these files each day.

I'm hoping to find a way to insert a 0 based on these patterns since I only need to change the single and double characters after the _.

rename ?????_?.tif to ?????_00?.tif
rename ?????_??.tif to ?????_0??.tif

Thanks!

Brian
 
You would have to split the file name twice once at the . and then at the _, when you split at the _ the array will have two values aryFName(0) and aryFName(1)

aryFNameExt = Split(FName, ".")

aryFName = Split(aryFNameExt(0), "_")

Do Until Len(aryFName(1)) = 3

aryFName(1) = "0" & AryFName(1)

Loop

NewFIleName = aryFName(0) & "_" & aryFName(1) & "." aryFNameExt(1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top