【问题标题】:VB.NET integer increment valueVB.NET 整数增量值
【发布时间】:2015-06-26 18:29:58
【问题描述】:

所以我使用 wget 从输入文件中下载几百个文件,我使用 Integer 函数让它在每次运行时递增文件编号,但文件编号如下:

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 等。

感谢法比奥。

谢谢

基山

【问题讨论】:

  • 对不起,我的拼写错误,这是我目前使用的。

标签: vb.net integer


【解决方案1】:

你需要格式化你的整数值

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

来自 MSDN Custom Numeric Format Strings

零占位符“0”

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

或者String.Format方法

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

根据评论更新:
您可以使用YourArray.Count 扩展方法或YourArray.Length 属性通过For Next 循环遍历数组

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

【讨论】:

  • 我目前正在运行 for each 循环,以让 wget 遍历文件中的所有链接并将其输出为 1.txt 2.txt,我不确定如何在顶部实现您的 for 函数其中,请参阅上面我更新的代码,这就是我正在使用的。
  • @KishanKaplan,在您的阵列上使用For Next 循环,或创建自己的“索引”变量。检查更新的答案
  • 感谢 Fabio,您的最后一段代码有效,但文件仍以 1.txt 2.txt 3.txt 等形式出现。我需要它们为 001 002 003 等形式
  • 用“零占位符”格式化整数值
  • 知道了,请参阅上面的主要帖子以获取工作代码,与您发布的内容略有不同,但它可以工作。感谢法比奥的帮助,非常感谢。
【解决方案2】:

使用 FORMAT 函数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多