Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(xFile, True) '開啟文件檔,可覆蓋原文件檔模式
For i = 0 To UBound(My)
F.Writeline My(i)
Next
F.Close
End Sub
Function Latest_file(資料夾路徑 As String) As String '自訂函數(Function) ,傳回:最新存檔的檔案名稱
Dim F As Object, E, AR(), i As Integer, A As Variant, xFile As String
Set F = CreateObject("Scripting.FileSystemObject").GetFolder(資料夾路徑).Files
ReDim AR(1 To 2, 1 To F.Count)
For Each E In F
If E Like "*.txt" Then '檔案副檔名為txt
i = i + 1
AR(1, i) = E '陣列第一維 置入檔案名稱
AR(2, i) = CDbl(E.DateLastModified) '陣列第二維 置入存檔時間
End If
Next
A = Application.WorksheetFunction.Index(AR, 2)
On Error Resume Next
Latest_file = AR(Application.Match(Application.Max(A), A, 0), 1)
If Err Then MsgBox 資料夾路徑 & "資料夾,找不到 txt檔案 !!! ": End
End Function
複製代碼
作者: vm3503ru8 時間: 2017-9-7 14:05
請問版大@@
Sub OP()
Dim myDir As String, myRow As Integer
Dim my(6)
myDir = ThisWorkbook.Path & "\"
myRow = 15
Open myDir & "123.TXT" For Input As #1
Do Until EOF(1)
Input #1, my(0), my(1), my(2), my(3), my(4), my(5), my(6)
Sub Ex()
xPath = "d:\" 'ThisWorkbook.Path
Ex_修改最新文字檔
Workbooks.Open Latest_file(xPath) '匯入TXT到EXCEL
End Sub
Sub Ex修改文件檔案()
Dim F As Object, E, AR(), i As Integer, A As Variant, xFile As String
Dim My() As Integer
'Set F = CreateObject("Scripting.FileSystemObject").GetFolder(ThisWorkbook.Path).Files
Set F = CreateObject("Scripting.FileSystemObject").GetFolder("C:\Users\USER\Desktop\data").Files
'.Files ->這資料夾路徑中的所有的檔案
ReDim AR(1 To 2, 1 To F.Count)
For Each E In F
If E Like "*.mea" Then '檔案副檔名為txt
i = i + 1
AR(1, i) = E '陣列第一維 置入檔案名稱
AR(2, i) = CDbl(E.DateLastModified) '陣列第二維 置入存檔的日期時間
End If
Next
A = Application.WorksheetFunction.Index(AR, 2)
xFile = AR(Application.Match(Application.Max(A), A, 0), 1) '最新存檔的檔案名稱
Set F = CreateObject("Scripting.FileSystemObject").OpenTextFile(xFile, 1) '開啟文件檔,讀取模式
A = Split(F.ReadAll, vbLf) '讀取檔案內容
F.Close '關閉文件檔案
Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(xFile, True) '開啟文件檔,可覆蓋原文件檔模式
For i = 0 To UBound(A)
F.WriteLine (i)
Next
F.Close
End Sub
這是自己改的 可能不盡理想-..-
[attach]27725[/attach]作者: GBKEE 時間: 2017-9-10 08:33