Kishan Kaplan
Kishan Kaplan

Reputation: 13

VB.NET integer increment value

So i'm using wget to download a few hundred files from an input file and I'm using a Integer function to get it to increment the file number each time it runs, but the files are being number like below:

1.ext
2.ext
3.ext

However I want them to be numbered like below:

001.ext
002.ext
003.ext

The max number of files ofcourse will go up to 999, currently im using the code below but I can't figure out how to get VB to use a 3 digit value instead of 1.

The code I'm using to do the number increment is:

Dim i As Integer
i += 1

But it won't let me use a 3 number value, any help ?

Current code:

Dim readfilelinks() As String = IO.File.ReadAllLines("tmp\file_links.mda")


        For Each line As String In readfilelinks


            Command1 = CommandForm.File_Command12.Text
            Command2 = CommandForm.File_Command12_1.Text
            Dim Com11 As New ProcessStartInfo
            Com11.FileName = "cmd.exe"
            Com11.Arguments = Command1 & Resources & "\tmp\file_links\" & (i) & ".txt" & Command2 & " " & Command2 & line & Command2
            Com11.UseShellExecute = True
            Com11.WindowStyle = ProcessWindowStyle.Normal
            Dim proc11 As Process = Process.Start(Com11)
            proc11.WaitForExit()
        Next

The above executes:

wget.exe --no-check-certificate -O "C:\Somedir\tmp\1.txt" "hxxp://somewebsite.com"
wget.exe --no-check-certificate -O "C:\Somedir\tmp\2.txt" "hxxp://somewebsite.com"
wget.exe --no-check-certificate -O "C:\Somedir\tmp\3.txt" "hxxp://somewebsite.com"

WORKING CODE:

Dim readfilelinks() As String = IO.File.ReadAllLines("tmp\file_links.mda")

        Dim i As Int32 = 0

        For Each link As String In readfilelinks

            Command1 = CommandForm.SoundCloud_Command12.Text
            Command2 = CommandForm.SoundCloud_Command12_1.Text
            Dim Com11 As New ProcessStartInfo
            Com11.FileName = "cmd.exe"
            Com11.Arguments = Command1 & Resources & "\tmp\file_links\" & (String.Format("{0:000}", i)) & ".txt" & Command2 & " " & Command2 & link & Command2
            Com11.UseShellExecute = True
            Com11.WindowStyle = ProcessWindowStyle.Normal
            Dim proc11 As Process = Process.Start(Com11)
            proc11.WaitForExit()

            i += 1

        Next

That executes the wget command and outputs the files as 001.txt, 002.txt, 003.txt etc.

Thanks Fabio.

Thanks

Kishan

Upvotes: 0

Views: 3697

Answers (2)

Fabio
Fabio

Reputation: 32445

You need format your Integer value

For i As Int32 = 0 To MaxNumber
    Dim fileName As String = i.ToString("000") & ".ext"
Next

From MSDN Custom Numeric Format Strings

Zero placeholder "0"

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

Or String.Format method

For i As Int32 = 0 To MaxNumber
    Dim fileName As String = String.Format("{0:000}.ext", i)
Next

Update based on comment:
You can loop through array with For Next loop using YourArray.Count extension method or YourArray.Length property

Dim readfilelinks() As String = IO.File.ReadAllLines("tmp\file_links.mda")
For i As Int32 = 0 To readfilelinks.Count - 1
    Dim line As String = readfilelinks(i)
    'Then use your code
Next

If you need to use For Each loop then add your own "index" variable

Dim i As Int32 = 0
For Each line As String In readfilelinks
    'Your code
    Com11.Arguments = String.Format("{0}{1}\tmp\file_links\{2:000}.txt{3} {3}{4}{3}",
                                    Command1,
                                    Resources,
                                    i,
                                    Command2,
                                    line)
    'Your code
    i += 1 'Increase by 1   
Next

Upvotes: 2

Zeddy
Zeddy

Reputation: 2089

Use the FORMAT function

Dim MyNextFileName as String
Dim Counter as Integer
'
For Counter = 1 to 10
  MyNextFileName = Format(Counter,"000") & ".ext"
  'Save File
Next

Upvotes: 0

Related Questions