【问题标题】:VB.NET - Replace column value in text file that is space delimitedVB.NET - 替换以空格分隔的文本文件中的列值
【发布时间】:2019-02-22 16:11:43
【问题描述】:

我正在尝试从 SSIS 运行 vb.net 脚本以在空格分隔的文本中执行以下操作;

  1. 循环遍历目录中的所有文件(我已经使用 .GetFiles 对此进行了编码)
  2. 在每个文本文件中循环遍历文件中的每一行
  3. 在行中替换/插入一个值
  4. 保存文件

我正在努力想出一种替换/插入值的方法。我不相信这对于 ReadLines 是可能的,而且我的搜索还没有为我的情况找到任何解决方案。我找到的所有解决方案都建议使用 .split,但由于此文件是文本分隔的且列大小不同,因此 .split 和 .replace 将不起作用。

有什么想法吗?这是文本文件中的一行示例,我要在其中插入值;

WMS0104        N00011              800171548-1    20190221                                                                                                                                  OVPRC                    <INSERT VALUE HERE>            PRINTER13           000000000000000000000000000000010000000000000000                                                                      00000000000000000000000000000000000000000000000000000000000000000000000000000     2019022108511300                                                                  00000000000000                                            00000000000000001

【问题讨论】:

  • 请发布您的代码和预期的输出。

标签: vb.net readlines


【解决方案1】:

知道了

        Dim BeforeWH_ID, ExistingWH_ID, AfterWH_ID, DesiredWH_ID, NewLineRecord As String

            DesiredWH_ID = "034                             "

            Dim lines() As String = IO.File.ReadAllLines(ExportFilePath)

            'Loop through each line in the array
            For i As Integer = 0 To lines.Length - 1

                'Fill line variables
                BeforeWH_ID = Mid(lines(i), 1, 215)
                ExistingWH_ID = Mid(lines(i), 215, 32)
                AfterWH_ID = Mid(lines(i), 247, 377)

                NewLineRecord = BeforeWH_ID & DesiredWH_ID & AfterWH_ID

                'Compare WH_ID
                If ExistingWH_ID <> DesiredWH_ID Then
                    'Replace the line in the array
                    lines(i) = NewLineRecord
                Else
                    'Keep the line in the array
                    lines(i) = lines(i)
                End If

                'Reset Variables
                BeforeWH_ID = Nothing
                ExistingWH_ID = Nothing
                AfterWH_ID = Nothing
                NewLineRecord = Nothing

            Next
            'Overrite existing file with line array
            IO.File.WriteAllLines(ExportFilePath, lines)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多