VB.NET整数增量值

时间:2015-06-26 18:29:58

标签: vb.net integer

所以我正在使用wget从输入文件中下载几百个文件,并且我使用Integer函数让它在每次运行时增加文件编号,但文件的编号如下: / p>

1.ext
2.ext
3.ext

但我想让它们编号如下:

001.ext
002.ext
003.ext

最多文件数量将达到999,目前我正在使用下面的代码,但我无法弄清楚如何让VB使用3位数而不是1。

我用来做数字增量的代码是:

Dim i As Integer
i += 1

但它不会让我使用3号值,任何帮助吗?

当前代码:

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

以上执行:

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"

工作代码:

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

执行wget命令并将文件输出为001.txt,002.txt,003.txt等。

谢谢法比奥。

由于

基尚

2 个答案:

答案 0 :(得分:2)

您需要格式化整数值

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

来自MSDN Custom Numeric Format Strings

零占位符“0”

  

如果存在,则用相应的数字替换零;   否则,结果字符串中会出现零。

String.Format方法

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

根据评论更新:
您可以使用For Next扩展方法或YourArray.Count属性

来使用YourArray.Length循环遍历数组
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

如果您需要使用For Each循环,请添加您自己的“索引”变量

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

答案 1 :(得分:0)

使用FORMAT功能

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