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!

Better way to increment with leading zeros? 2

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
US
K, I got a page that a user enters a number (ie 13) to a textbox. We want to start renaming a series of files starting with that number and incrementing up, but with leading zeros. (ie 0013) Here is what I have now:
Code:
Dim incNum As String = numStart.Text
For Each File In di.GetFiles("*.jpg")
 While incNum.Length < 4
  incNum = &quot;0&quot; & incNum
 End While
 File.CopyTo(&quot;F:\Images\Jobs2\&quot; & job1.Text & &quot;\Full\&quot; & incNum & &quot;.jpg&quot;)
 incNum = incNum + 1
Next

Is there a better way?
 
While incNum.Length < 4
incNum = &quot;0&quot; & incNum
End While

---->
incNum=Right(&quot;0000&quot; & incNum,4)
 
' Try the PadLeft method
Dim incNum As String = numStart.Text
For Each File In di.GetFiles(&quot;*.jpg&quot;)
incNum = incNum.PadLeft(4,&quot;0&quot;)
File.CopyTo(&quot;F:\Images\Jobs2\&quot; & job1.Text & &quot;\Full\&quot; & incNum & &quot;.jpg&quot;)
incNum = incNum + 1
Next
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Both posts were good, but I used John's in the code. You both get a star.

Question tho, anybody know any differences in performance/memory?
 

padleft should be faster.

but i have no proof. I just remember that there were other string formatting functions like this that MS said was faster than using & or +.

Kris

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top