VB如何批量写入文本中的内容??
用VB中的文件系统控件,(dir和drive及filel控件),就是一群TXT在一个目录下,我想让批量往TXT中写入一个字母,请高手指点...
用VB中的文件系统控件,(dir和drive及filel控件),就是一群TXT在一个目录下,我想让批量往TXT中写入一个字母,请高手指点
展开
2个回答
展开全部
'将下面保存为 Form1.frm
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5085
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 5085
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.FileListBox File1
Height = 4050
Left = 2520
Pattern = "*.txt"
TabIndex = 4
Top = 720
Width = 2175
End
Begin VB.DirListBox Dir1
Height = 3450
Left = 240
TabIndex = 3
Top = 1200
Width = 2055
End
Begin VB.DriveListBox Drive1
Height = 300
Left = 240
TabIndex = 2
Top = 720
Width = 2055
End
Begin VB.CommandButton Command1
Caption = "批量写入"
Height = 495
Left = 3360
TabIndex = 1
Top = 120
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 0
Text = "P"
Top = 120
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
For i = 0 To File1.ListCount - 1
f = FreeFile
Open File1.Path & "\" & File1.List(i) For Append As #f
Print #f, Text1
Close f
Next
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5085
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 5085
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.FileListBox File1
Height = 4050
Left = 2520
Pattern = "*.txt"
TabIndex = 4
Top = 720
Width = 2175
End
Begin VB.DirListBox Dir1
Height = 3450
Left = 240
TabIndex = 3
Top = 1200
Width = 2055
End
Begin VB.DriveListBox Drive1
Height = 300
Left = 240
TabIndex = 2
Top = 720
Width = 2055
End
Begin VB.CommandButton Command1
Caption = "批量写入"
Height = 495
Left = 3360
TabIndex = 1
Top = 120
Width = 1215
End
Begin VB.TextBox Text1
Height = 375
Left = 240
TabIndex = 0
Text = "P"
Top = 120
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
For i = 0 To File1.ListCount - 1
f = FreeFile
Open File1.Path & "\" & File1.List(i) For Append As #f
Print #f, Text1
Close f
Next
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
展开全部
首先引入Microsoft Scripting Runtime
Dim fso As New FileSystemObject
Dim objFile, objFolder
Dim ex As Object
Dim i As Integer
Folder = "文件夹路径"
Set objFolder = fso.getFolder(Folder)
Debug.Print objFolder.name
For Each objFile In objFolder.SubFolders (如果换成Files,就是获取文件名)
Debug.Print objFile.name
'在此处可以用objFile.name做判断,如果获取到的文件名是你需要的TXT文件,
'就调用TXT文件写入方法
'由于写TXT文件的方法很简单,网上随处可见这里我就不写了
Next
Dim fso As New FileSystemObject
Dim objFile, objFolder
Dim ex As Object
Dim i As Integer
Folder = "文件夹路径"
Set objFolder = fso.getFolder(Folder)
Debug.Print objFolder.name
For Each objFile In objFolder.SubFolders (如果换成Files,就是获取文件名)
Debug.Print objFile.name
'在此处可以用objFile.name做判断,如果获取到的文件名是你需要的TXT文件,
'就调用TXT文件写入方法
'由于写TXT文件的方法很简单,网上随处可见这里我就不写了
Next
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询