' Purpose: ' Author:吴文智 ' Date:2001-11-13 'Description:要试用本例请在窗体中填加一个按钮 ' 然后在代码窗体中粘贴如下代码 ' Good luck!Option ExplicitPrivate Sub Command1_Click() Dim strOldFileName As String Dim strNewFileName As String Dim strLine As String Dim strAll As String Dim lngCount As String
Dim x As New Scripting.FileSystemObject Dim y As Scripting.TextStream Set y = x.OpenTextFile("d:\xxxx.txt", ForReading) y.ReadAll VBA.MsgBox y.Line
Dim FSO As New Scripting.FileSystemObject Dim Fle As Scripting.TextStream Set Fle = FSO.OpenTextFile("c:\dd.txt", ForAppending) MsgBox Fle.Line, vbInformation, "Ok"
文件的每一行作为一个记录, 如: Line Input #1,"名字:","分数","班级" for classnum=1 to allclassmember Line Input #1,"我","92","3班" '这里用三个数组变量代替 next 读出时每一行既为一个记录,第一行为字段名.
文件为非文本文件,有些函数是不可用的如FSO.OpenTextFile
用 open "c:\joking.txt" for output as #1 do until eof(1) get#1 ,i,content ' i 为记录数 i=i+1 cols #1
DDD
EEE
FFFF
变成1 DDD
2 EEE
3 FFFF这样吗?
' Author:吴文智
' Date:2001-11-13
'Description:要试用本例请在窗体中填加一个按钮
' 然后在代码窗体中粘贴如下代码
' Good luck!Option ExplicitPrivate Sub Command1_Click()
Dim strOldFileName As String
Dim strNewFileName As String
Dim strLine As String
Dim strAll As String
Dim lngCount As String
strOldFileName = "c:\autoexec.bat"
strNewFileName = "c:\autoexec.bak"
Open strOldFileName For Input As #1
lngCount = 0
strAll = ""
Do While Not EOF(1)
Line Input #1, strLine
lngCount = lngCount + 1
strLine = CStr(lngCount) & " " & strLine & vbCrLf
strAll = strAll & strLine
Loop
Close #1
MsgBox "记录数合计:" & lngCount, vbInformation
Open strNewFileName For Output As #2
Print #2, strAll
Close #2
MsgBox "带有记录号的文件已存成" & strNewFileName, vbInformation
End Sub
我告诉你一个快些的办法:将这个文件导入到ACCESS数据库中,然后再按格式导出!绝吧,这个方法对万条以上记录的文件最有效了。具体代码我找不到了,不过这个思路是绝对可以的!
http://www.csdn.net/expert/topic/358/358535.shtm
Dim y As Scripting.TextStream
Set y = x.OpenTextFile("d:\xxxx.txt", ForReading)
y.ReadAll
VBA.MsgBox y.Line
Dim Fle As Scripting.TextStream
Set Fle = FSO.OpenTextFile("c:\dd.txt", ForAppending)
MsgBox Fle.Line, vbInformation, "Ok"
如:
Line Input #1,"名字:","分数","班级"
for classnum=1 to allclassmember
Line Input #1,"我","92","3班" '这里用三个数组变量代替
next
读出时每一行既为一个记录,第一行为字段名.
do until eof(1)
get#1 ,i,content ' i 为记录数
i=i+1
cols #1