读取文本文件,复制$分隔的内容并插入带有新页眉和页脚的新文本文件。 [英] Read a Text File, copy the contents separated by $ and insert into a new text file with new Headers and footers.

查看:62
本文介绍了读取文本文件,复制$分隔的内容并插入带有新页眉和页脚的新文本文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow,

我是一名中级VB.Net程序员,我需要助手来完成项目。我有如下请求。



1.确认存在源文件(文本文件)

获取字段值,位置File_Locations表,其中code =''TEST''。如果源文件不存在于此位置,则结束处理,否则继续执行下面的2。

2.Build文件名

从Control_Table获取这些字段的值; File_Base,FileSeqNo,FileSuffix。并将文件名创建为File_Base_FileSeqNo_FileSuffix。

3将页眉和页脚详细信息写入上面2中创建的文件。这些是新的详细信息,如处理日期等。

4.将实际消息写入上面2中创建的文件。

对于Code =''TEST''的位置中的每个文件。

如果源文件中存在行,则将$分隔的内容复制到上面2中创建的文件中,并且包括新的页眉和页脚详细信息。



请注意,源位置会有多个文件,但每个文件必须复制到上面创建的新文件中。



我刚刚做了一些编码,但是我坚持了下来所以请协助。

实际上代码belwo正在工作但是当它是'试图写入2中创建的文件,它说该文件正由另一个进程使用,无法访问。



  Sub 处理()
' 通过数据库获取文件位置
Dim imf As 字符串 = das.CheckIMFLocation( TEST
Dim imfp As String = das.CheckIMFLocation( TESTP
Dim intInFiles 作为 整数 = 0
' 检查代码为IMF的位置的所有文件
Dim filename As String = Nothing
' 将thewholefile变为字符串
Dim di As New DirectoryInfo(imf)
Dim fiArr As FileInfo()= di.GetFiles ()
' Dim fri As FileInfo

' 1。检查内向源文件
diInward = DirectoryInfo(imf)
fiInward = diInward.GetFiles( * .txt
intInFiles = fiInward.Length

' '检查文件夹中至少有一个文件,并获取可用文件的名称
'并将其分配给变量
' For fi frr in fiArr
' filename = fri.Name
' 下一步
thewholefile = imf& \& filename

' 从控制表中获取字段的值并构建文件名
Dim FESFileNameInwardBase 作为 字符串
Dim FESFilenameInwardCurSeqNo 作为 字符串
Dim FESFileNameInwardSuffix 作为 字符串
Dim ProcessedFileName 作为 字符串
Dim createfile 作为 字符串
Dim fs As FileStream = Nothing
Dim objStreamWriter As StreamWriter

FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base
FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No
FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix

' 检查IMF位置是否存在文件
如果 intInFiles> 0 然后
' < span class =code-comment>对于该位置中的每个文件,在处理位置为它们创建一个已处理的文件
' < span class =code-comment>并读取内容并复制到名为的新文件。

' 1.为文件读取创建文件
对于 每个 disfri fiArr
' MessageBox.Show(disfri.Name)
' ======== ===============每次获取文件的新名称======================
FESFileNameInwardBase = da s.ReturnWFControls.FES_Filename_Inward_Base ' SWI
FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No 100001
FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix ' TEST
ProcessedFileName = FESFileNameInwardBase& _& FESFilenameInwardCurSeqNo& _& FESFileNameInwardSuffix
' ==================== ===结束获取文件名=====================================

' 在写入文件之前先创建文件
createfile = imfp& \& ProcessedFileName& .txt
fs = File.Create(createfile)
fs。 Flush()
fs.Close()

' 读取每个文件并获取详细信息以$符号分隔
使用 MyReader 作为 TextFieldParser(imf& \& disfri.Name)
' 使用MyReader作为新TextFieldParser(disfri.Name)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters( $

Dim currentRow As String ()
while MyReader.EndOfData
尝试
currentRow = MyReader.ReadFields()
Dim currentField As 字符串
' 此for循环读取文件中的每个数据
对于 每个 currentField currentRow
MessageBox.Show(currentField)

' 在此处插入代码以写入新文件,每行读取
' 然后退出循环并读取下一个文件并执行相同操作。
objStreamWriter = StreamWriter(createfile)
objStreamWriter .WriteLine(currentField)
下一步
Catch ex As Exception
MessageBox.Show( Line& ; ex.Message& _
无效,将被跳过。
结束 尝试
结束 while
结束 使用
' 读取以$ sign分隔的每个文件的结尾
下一步 disfri
否则
' 没有文件存在,因此退出子
MessageBox.Show( 否文件存在 无文件,MessageBoxButtons.OK,MessageBoxIcon。停止
退出 Sub
结束 如果
结束 Sub

解决方案

到上面2中创建的文件中,并包含新的页眉和页脚详细信息。



请注意,源位置会有多个文件,但每个文件必须复制到上面创建的新文件中。



我刚刚做了一些编码,但是我坚持了下来所以请协助。

实际上代码belwo正在工作但是当它是'试图写入2中创建的文件,它说该文件正由另一个进程使用,无法访问。



  Sub 处理()
' 通过数据库获取文件位置
Dim imf As 字符串 = das.CheckIMFLocation( TEST
Dim imfp As String = das.CheckIMFLocation( TESTP
Dim intInFiles 作为 整数 = 0
' 检查代码为IMF的位置的所有文件
Dim filename As String = Nothing
' 将thewholefile变为字符串
Dim di As New DirectoryInfo(imf)
Dim fiArr As FileInfo()= di.GetFiles ()
' Dim fri As FileInfo

' 1。检查内向源文件
diInward = DirectoryInfo(imf)
fiInward = diInward.GetFiles( * .txt
intInFiles = fiInward.Length

' '检查文件夹中至少有一个文件,并获取可用文件的名称
'并将其分配给变量
' For fi frr in fiArr
' filename = fri.Name
' 下一步
thewholefile = imf& \& filename

' 从控制表中获取字段的值并构建文件名
Dim FESFileNameInwardBase 作为 字符串
Dim FESFilenameInwardCurSeqNo 作为 字符串
Dim FESFileNameInwardSuffix 作为 字符串
Dim ProcessedFileName 作为 字符串
Dim createfile 作为 字符串
Dim fs As FileStream = Nothing
Dim objStreamWriter As StreamWriter

FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base
FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No
FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix

' 检查IMF位置是否存在文件
如果 intInFiles> 0 然后
' < span class =code-comment>对于该位置中的每个文件,在处理位置为它们创建一个已处理的文件
' < span class =code-comment>并读取内容并复制到名为的新文件。

' 1.为文件读取创建文件
对于 每个 disfri fiArr
' MessageBox.Show(disfri.Name)
' ======== ===============每次获取文件的新名称======================
FESFileNameInwardBase = da s.ReturnWFControls.FES_Filename_Inward_Base ' SWI
FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No 100001
FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix ' TEST
ProcessedFileName = FESFileNameInwardBase& _& FESFilenameInwardCurSeqNo& _& FESFileNameInwardSuffix
' ==================== ===结束获取文件名=====================================

' 在写入文件之前先创建文件
createfile = imfp& \& ProcessedFileName& .txt
fs = File.Create(createfile)
fs。 Flush()
fs.Close()

' 读取每个文件并获取由


签署的详细信息
使用 MyReader 正如 TextFieldParser(imf& \& disfri.Name)
' 使用MyReader作为新TextFieldParser(disfri .Name)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(



Dim currentRow As 字符串()
MyReader.EndOfData
尝试
currentRow = MyReader.ReadFields()
Dim currentField 作为 字符串
' 此for循环读取文件中的每个数据
对于 每个 currentField currentRow
MessageBox.Show(currentField)

' 在此处插入要写入新文件的代码,每行读取
' 然后退出循环并读取下一个文件并执行相同操作。
objStreamWriter = StreamWriter(createfile)
objStreamWriter.WriteLine(currentField)
下一步
Catch ex As 异常
MessageBox.Show( Line& ex.Message& _
无效,将被跳过。
结束 尝试
结束 while
结束 使用
' 读取每个文件的结尾


Hellow,
I''m an intermediate VB.Net programmer and I need assistant to get a project done. I have a request as follows.

1. Confirm that source files exist (the text files)
Obtain value of field, Location in File_Locations table where code = ''TEST''. If the source files don''t exists in this location then end the processing, else proceed to 2 below.
2.Build File Name
Obtain value of the these fields from Control_Table; File_Base, FileSeqNo, FileSuffix. And create the filename as File_Base_FileSeqNo_FileSuffix.
3 Write the Header and Footer details to file created in 2 above. These are new details like the date processed, etc.
4. Write the Actual Message to the file created in 2 above.
For each file in location where Code = ''TEST''
If row exists in Source file copy the contents separated by $ onto the file created in 2 above, and include the new header and footer details.

Note that there will be several files in the source location but each must be copied to a new file created as in 2 above.

I just did a bit of the coding as follows but I''m stuck with this so please assist.
Actually the code belwo is working but when it''s trying to write to the file created in 2 it''s saying the file is in use by another process and can''t be accessed.

Sub Process()
'Obtain the file location via the DB
        Dim imf As String = das.CheckIMFLocation("TEST")
        Dim imfp As String = das.CheckIMFLocation("TESTP")
        Dim intInFiles As Integer = 0
        'Checks for all the file in the location where code is IMF 
        Dim filename As String = Nothing
        'Dim thewholefile As String
        Dim di As New DirectoryInfo(imf)
        Dim fiArr As FileInfo() = di.GetFiles()
        'Dim fri As FileInfo

        '1. Check for inward source files
        diInward = New DirectoryInfo(imf)
        fiInward = diInward.GetFiles("*.txt")
        intInFiles = fiInward.Length

        ''checks at least a file is in the folder and gets the name of the availabe file
        ''and assign that to the variable
        'For Each fri In fiArr
        '    filename = fri.Name
        'Next
        'thewholefile = imf & "\" & filename

        'Obtain the value of the fields from control table and builds the file name
        Dim FESFileNameInwardBase As String
        Dim FESFilenameInwardCurSeqNo As String
        Dim FESFileNameInwardSuffix As String
        Dim ProcessedFileName As String
        Dim createfile As String
        Dim fs As FileStream = Nothing
        Dim objStreamWriter As StreamWriter

        FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base
        FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No
        FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix

        'Check if file exists on the location for IMF
        If intInFiles > 0 Then
            'For each file in the location, create a processed file for them at the processed location
            'and read the contents and copy to the new file named.

            ' 1. Create a file for the file read
            For Each disfri In fiArr
                'MessageBox.Show(disfri.Name)
                '=======================gets new name for the file each time ======================
                FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base 'SWI
                FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No '100001
                FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix 'TEST
                ProcessedFileName = FESFileNameInwardBase & "_" & FESFilenameInwardCurSeqNo & "_" & FESFileNameInwardSuffix
                '=======================End getting file name =====================================

                'create the file here first before you write to it
                createfile = imfp & "\" & ProcessedFileName & ".txt"
                fs = File.Create(createfile)
                fs.Flush()
                fs.Close()

                'Read each file and get the details separated by the $ sign
                Using MyReader As New TextFieldParser(imf & "\" & disfri.Name)
                    'Using MyReader As New TextFieldParser(disfri.Name)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters("$")

                    Dim currentRow As String()
                    While Not MyReader.EndOfData
                        Try
                            currentRow = MyReader.ReadFields()
                            Dim currentField As String
                            'This for loop reads each data in the file
                            For Each currentField In currentRow
                                MessageBox.Show(currentField)

                                'Insert here the code to write to the new file, each line read
                                'and then exit loop and read the next file and do the same.
                                objStreamWriter = New StreamWriter(createfile)
                                objStreamWriter.WriteLine(currentField)
                            Next
                        Catch ex As Exception
                            MessageBox.Show("Line " & ex.Message & _
                                            "is not valid and will be skipped.")
                        End Try
                    End While
                End Using
                'End of reading each file separated by $ sign
            Next disfri
        Else
            'No files exists so exit sub
            MessageBox.Show("No Files exist", "No File", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        End If
End Sub

解决方案

onto the file created in 2 above, and include the new header and footer details.

Note that there will be several files in the source location but each must be copied to a new file created as in 2 above.

I just did a bit of the coding as follows but I''m stuck with this so please assist.
Actually the code belwo is working but when it''s trying to write to the file created in 2 it''s saying the file is in use by another process and can''t be accessed.

Sub Process()
'Obtain the file location via the DB
        Dim imf As String = das.CheckIMFLocation("TEST")
        Dim imfp As String = das.CheckIMFLocation("TESTP")
        Dim intInFiles As Integer = 0
        'Checks for all the file in the location where code is IMF 
        Dim filename As String = Nothing
        'Dim thewholefile As String
        Dim di As New DirectoryInfo(imf)
        Dim fiArr As FileInfo() = di.GetFiles()
        'Dim fri As FileInfo

        '1. Check for inward source files
        diInward = New DirectoryInfo(imf)
        fiInward = diInward.GetFiles("*.txt")
        intInFiles = fiInward.Length

        ''checks at least a file is in the folder and gets the name of the availabe file
        ''and assign that to the variable
        'For Each fri In fiArr
        '    filename = fri.Name
        'Next
        'thewholefile = imf & "\" & filename

        'Obtain the value of the fields from control table and builds the file name
        Dim FESFileNameInwardBase As String
        Dim FESFilenameInwardCurSeqNo As String
        Dim FESFileNameInwardSuffix As String
        Dim ProcessedFileName As String
        Dim createfile As String
        Dim fs As FileStream = Nothing
        Dim objStreamWriter As StreamWriter

        FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base
        FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No
        FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix

        'Check if file exists on the location for IMF
        If intInFiles > 0 Then
            'For each file in the location, create a processed file for them at the processed location
            'and read the contents and copy to the new file named.

            ' 1. Create a file for the file read
            For Each disfri In fiArr
                'MessageBox.Show(disfri.Name)
                '=======================gets new name for the file each time ======================
                FESFileNameInwardBase = das.ReturnWFControls.FES_Filename_Inward_Base 'SWI
                FESFilenameInwardCurSeqNo = das.ReturnWFControls.FES_Inward_Current_Seq_No '100001
                FESFileNameInwardSuffix = das.ReturnWFControls.FES_Filename_Inward_Suffix 'TEST
                ProcessedFileName = FESFileNameInwardBase & "_" & FESFilenameInwardCurSeqNo & "_" & FESFileNameInwardSuffix
                '=======================End getting file name =====================================

                'create the file here first before you write to it
                createfile = imfp & "\" & ProcessedFileName & ".txt"
                fs = File.Create(createfile)
                fs.Flush()
                fs.Close()

                'Read each file and get the details separated by the


sign Using MyReader As New TextFieldParser(imf & "\" & disfri.Name) 'Using MyReader As New TextFieldParser(disfri.Name) MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.SetDelimiters("


") Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() Dim currentField As String 'This for loop reads each data in the file For Each currentField In currentRow MessageBox.Show(currentField) 'Insert here the code to write to the new file, each line read 'and then exit loop and read the next file and do the same. objStreamWriter = New StreamWriter(createfile) objStreamWriter.WriteLine(currentField) Next Catch ex As Exception MessageBox.Show("Line " & ex.Message & _ "is not valid and will be skipped.") End Try End While End Using 'End of reading each file separated by


这篇关于读取文本文件,复制$分隔的内容并插入带有新页眉和页脚的新文本文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆