2013-03-26 33 views
0

我在VB 2010中編寫了一個帶有一個按鈕的窗體,完全是白癡。我在一段時間內還沒有完成VB,並且注意到2010年有很多變化,比較幾年前我做過。用於讀寫的VBA文本文件輸入

我需要做的是從文件中讀取數據,並在寫入原始數據的同時寫入兩個新的單獨文件。它將從文本文件中讀取以獲取一些內容並比較當前日期。

文本內容將是一個序列,接下來的兩列將是日期。

Dim iFileName As New StreamReader("G:\SAP In and Out\test.txt", False) 
    Dim sFileExport As New StreamWriter(DateValue(Now)) + " SAP Export", False) 
    Dim sFileImport As New StreamWriter(DateAdd(DateInterval.Day, 10, DateValue(Now)) + " SAP Import", False) 

    Dim VTS As String 'Will be used to grab the VT serial 
    Dim CurrentDate As Date 'Will be used to compare to grabbed dates 
    Dim NextDateOut As Date 'Will be used to grab next date out value 
    Dim NextDateIn As Date 'Will be used to grab next date in value 

    'Setting a consistant value with daily' 
    CurrentDate = DateValue(Now) 

    Do While Not EOF(1) 

     Input(iFileName),VTS,NextDateOut,NextDateIn 

     If CurrentDate = NextDateOut Then 

      'Write to the export File 
      sFileExport.write(VTS) 

      'Write under the just read line in the open file 
      iFileName.write(/newline + VTS + /TAB + (DateAdd(DateInterval.Day, 20, DateValue(Now)) + /tab + (DateAdd(DateInterval.Day, 30, DateValue(Now))) 

     ElseIf CurrentDate = NextDateIn Then 

      'Write to import file 
      sFileImport.Write(VTS) 

     End If 

    Loop 

End Sub 

但是我的語法是關閉的,而且顯然沒有運行,因爲我在尋求幫助。 請提前幫助和感謝。我一直在這個工作了幾個小時,還沒有任何積極的結果呢。

+0

你可以發佈您的輸入文本文件的一個示例行? – Steve 2013-03-26 08:20:22

+1

VB <> VBA ... :) – 2013-03-26 08:58:44

+0

對不起,我犯了一個錯誤我以爲你只是使用導入文件進行閱讀(不適合寫太多) – Zeddy 2013-03-26 12:32:04

回答

0

嗯,我認爲它現在,但我不得不添加一個額外的功能,所以我可以測試它。 由於我期望您使用美國日期格式(MM/DD/YYYY),並使用英國日期格式(DD/MM/YYYY),所以我收到日期格式的錯誤。

無論如何,你可以去掉這個函數,因爲我不認爲你會需要它,但我已經把它放在了任何地方,所以你可以看到它的相當自我解釋,並簡單地在日期格式之間轉換,儘管它是因爲你的日子和月份不是兩位數字(不是前導零)。

我希望它能幫助您指向正確的方向,並且可以將其切換並根據您的偏好進行更改。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 


    Dim iFileName As New System.IO.StreamReader("c:\temp\vttest.txt", False) 
    Dim iReadData As List(Of String) 
    Dim Buffer As String 

    'Read complete file into array 
    iReadData = New List(Of String) 
    Do While Not iFileName.EndOfStream() 
     Try 
      Buffer = "" 
      Buffer = iFileName.ReadLine() 
      iReadData.Add(Buffer) 
     Catch ex As Exception 
      'error or end of file 
     End Try 
    Loop 
    iFileName.Close() 

    'Not needed 
    'Dim VTS As String    'This is in iLineData(0) after we split the line 
    'Dim NextDateOut As Date 'This is in iLineData(1) after we split the line 
    'Dim NextDateIn As Date  'This is in iLineData(2) after we split the line 

    'Process 
    Dim iFileUpdated As New System.IO.StreamWriter("c:\temp\vttest_copy.txt", True) 
    Dim sFileExport As New System.IO.StreamWriter("c:\temp\vttest" & Replace(DateValue(Now), "/", "_") & " SAP Export.txt", True) 
    Dim sFileImport As New System.IO.StreamWriter("c:\temp\vttest" & Replace(DateAdd(DateInterval.Day, 10, DateValue(Now)), "/", "_") + " SAP Import.txt", True) 
    Dim iLineData() As String 

    'Setting a consistant value with daily' 
    Dim CurrentDate As Date = DateValue(Now) 
    Dim RowId = 0 
    Do While RowId < iReadData.Count() 
     iLineData = Split(iReadData(RowId), " ") 
     iFileUpdated.WriteLine(iLineData(0) & " " & iLineData(1) & " " & iLineData(2)) 
     If CurrentDate = FormatDate(iLineData(1), "US", "UK") Then 
      'Write to the export File 
      sFileExport.WriteLine(iLineData(0)) 
      'Write under the just read line in the open file 
      iFileUpdated.WriteLine(iLineData(0) & " " & (DateAdd(DateInterval.Day, 20, DateValue(Now))) & " " & (DateAdd(DateInterval.Day, 30, DateValue(Now)))) 
     ElseIf CurrentDate = FormatDate(iLineData(2), "US", "UK") Then 
      'Write to import file 
      sFileImport.WriteLine(iLineData(0)) 
     End If 
     RowId = RowId + 1 
    Loop 
    sFileExport.Close() 
    sFileImport.Close() 
    iFileUpdated.Close() 
    MsgBox("Finshed") 
End Sub 

'This section added because my PC is set to DD/MM/YYYY (UK) 
'Expect yours is set to MM/DD/YYYY (US) 
Function FormatDate(ThisDate As String, ThisFormat As String, ThisTargetFormat As String) As Date 
    Dim X As Integer = 0 
    Dim Day1 As Integer = 0 
    Dim Month1 As Integer = 0 
    Dim Year1 As Integer = 0 
    Dim Buf As String = "" 
    If ThisFormat = "US" Then 
     For X = 1 To Len(ThisDate) 
      If Mid(ThisDate, X, 1) = "/" Then 
       If Month1 > 0 Then 
        Day1 = Buf 
        Buf = "" 
       Else 
        Month1 = Buf 
        Buf = "" 
       End If 
      Else 
       Buf = Buf & Mid(ThisDate, X, 1) 
      End If 
     Next 
     Year1 = Buf 
    Else 
     For X = 1 To Len(ThisDate) 
      If Mid(ThisDate, X, 1) = "/" Then 
       If Day1 > 0 Then 
        Month1 = Buf 
        Buf = "" 
       Else 
        Day1 = Buf 
        Buf = "" 
       End If 
      Else 
       Buf = Buf & Mid(ThisDate, X, 1) 
      End If 
     Next 
     Year1 = Buf 
    End If 
    'reformat for output 
    If ThisFormat = "US" Then 
     If ThisTargetFormat = "US" Then 
      'USA 
      FormatDate = (Format(Month1, "00") & "/" & Format(Day1, "00") & "/" & Format(Year1, "0000")) 
     Else 
      'UK 
      FormatDate = (Format(Day1, "00") & "/" & Format(Month1, "00") & "/" & Format(Year1, "0000")) 
     End If 
    Else 
     If ThisTargetFormat = "US" Then 
      'USA 
      FormatDate = (Format(Month1, "00") & "/" & Format(Day1, "00") & "/" & Format(Year1, "0000")) 
     Else 
      'UK 
      FormatDate = (Format(Day1, "00") & "/" & Format(Month1, "00") & "/" & Format(Year1, "0000")) 
     End If 
    End If 

End Function 

此外,我改變了文件名,以免覆蓋現有文件(這樣我就可以比較兩個文件)。

大部分的問題都是由FILENAMES日期的反斜槓引起的 - 讓電腦看起來像/ 3/12這樣的路徑,我想它是將斜線翻譯成文件夾分隔符,所以我用UNDERSCORES替換了它們飛。

一旦你編輯的代碼根據自己的喜好,你可以覆蓋現有文件,而不是,這是我如何與樣品中_copy.txt測試生成_copy.txt。

更新

感謝您的反饋意見。那麼它奇怪你的(生成的)文件是空的,因爲在我有一個有數據,另一個是空的。這導致我假設你的調整可能與它有關係?

爲了您的參考,我在下面發佈了它們。

原始文件[VTSTEST.TXT]

VT1000 3/26/2013 4/5/2013 
VT1100 3/26/2013 4/5/2013 
VT2000 3/27/2013 4/6/2013 
VT2200 3/27/2013 4/6/2013 

VTSTEST.TXT副本(修改後)

VT1000 3/26/2013 4/5/2013 
VT1100 3/26/2013 4/5/2013 
VT2000 3/27/2013 4/6/2013 
VT2000 16/04/2013 26/04/2013 
VT2200 3/27/2013 4/6/2013 
VT2200 16/04/2013 26/04/2013 

注:我期望兩個較長的線的那些插入介於現有的四行文字之間

VTTEST 06_04_2013 SAP導入。TXT

這個文件是空的

VTTEST27_03_2013 SAP export.txt到

VT2000 
VT2000 
+0

首先非常感謝你!這似乎是在正確的方向。我正在瀏覽它,並在其中進行一些調整。它運行,但導出和導入文件是空白的我仍然試圖找出一個。我想我可能不得不做一些數據類型轉換或者如果我是對的? – 2013-03-27 04:04:15