vb怎樣修改文件
① VB修改文本文件
Private Sub Command1_Click()
On Error GoTo err_cancel
Dim isFileName As String
'瀏覽要核虧添加的的文件
Form1.CommonDialog1.CancelError = True '取消報錯 並且跳改伍神出執行!橘枯
Form1.CommonDialog1.DialogTitle = "選擇要打開的Word文件"
Form1.CommonDialog1.Filter = "文本文件(*.txt)|*.txt"
Form1.CommonDialog1.FileName = ""
Form1.CommonDialog1.ShowOpen
isFileName = Form1.CommonDialog1.FileName '獲取完整路徑
Text1.Text = isFileName
Exit Sub
err_cancel:
MsgBox "請正確選擇文件。"
End Sub
Private Sub Command2_Click()
'多行數據
Dim strLine As String
Dim zzc As String
Dim fileNamePath As String
fileNamePath = Text1.Text
Open fileNamePath For Input As #1
Do Until EOF(1)
Line Input #1, strLine
If Mid(Trim(strLine), 1, 3) = "abc" Then strLine = "bcd"
zzc = zzc + strLine + Chr(13) + Chr(10)
Loop
Close #1
Open fileNamePath For Output As #1
Print #1, , zzc
Close #1
MsgBox "修改任務完成!"
End Sub
通過測試!
........張志晨:奉獻知識==收獲愉快..........
② 用vb 如何修改文件中的數據
建穗蘆腔立一個text文本和兩個command按鈕,在屬性框把text1.multiline文本的屬性改為true
private
sub
command1_click()
'打開文本按鈕
dim
a
as
string,
b
as
string
open
"c:\documents
and
settings\user\猜衫桌面\新建
文本文檔
(3).txt"
for
input
as
#1
while
not
eof(1)
input
#1,
a
b
=
b
&
a
&
vbcrlf
wend
close
#1
text1
=
b
end
sub
private
sub
command2_click()
'保存文本按鈕
open
"c:\documents
and
settings\user\桌面\新建嘩歲
文本文檔
(3).txt"
for
output
as
#1
print
#1,
text1
close
#1
end
sub
③ vb中怎麼修改TXT文件內容啊
Open
App.Path
&
"\123.txt"
For
append
As
#1Print
#1,
"不要相信含讓哥!"close
#用APPEND模式打開文件晌老拍
會檢測是否有123.TXT文宴羨件沒有就自動新建
④ VB怎麼修改文本內容呢
打開原txt文件讀取修改,放指晌森入新建txt文件內:
Sub ReplaceTxtFile()
Dim f1, f2
f1 = "D:\1.txt" '原文件路徑
f2 = "D:\2.txt" '修改後唯畝文件路徑
Open f2 For Output As #2
Open f1 For Input As #1
Do While Not EOF(1)
Line Input #1, vartemp '讀取f1,修改謹岩f1
If InStr(1, vartemp, "身高", vbTextCompare) > 0 And InStr(1, vartemp, "男", vbTextCompare) > 0 Then
vartemp = Replace(vartemp, "男", "女")
End If
Print #2, vartemp '寫入f2
Loop
Close #1
'Kill f1'可以刪除f1
Close #2
End Sub
⑤ VB 如何修改TXT內容
樓上的別瞧不起VB6,其實這樣的簡單問題VB6處理起來也很簡單,而且不需要FSO
1、最後一行寫入數據:
Open "c:\缺叢123.txt" For Append As #1
Print #1, "要寫叢型入的數據"
Close #1
2、修改某一行數據:
Dim s() As String
Open "c:\123.txt" For Binary As #1
s = Split(Input(LOF(1), #1), vbCrLf)
s(4) = "修改後的數據"滲扮猜 '修改第5行的數據(s(0)是第1行)
Put #1, , Join(s, vbCrLf)
Close #1
⑥ 用vb怎樣這樣修改文件名
Private Sub command1_click()
Dim d As String
d = Dir("c:\abc\*.txt")
Do Until d = ""
Name "c:\abc\" & d As "c:\abc\" & Text1.Text & d
d = Dir
Loop
End Sub
以上代碼是把"c:\abc"目錄的所有txt的文沖旦件名前面插入text1的內容。如果只想給最新創建的文件添桐判空加,那麼可以在局瞎循環中用FileDateTime("c:\abc\" & d)檢測文件的時間,找出最新的那個即可。
⑦ VB中怎麼修改txt文件
Private Sub Command1_Click()
Dim S As String
Dim S1$, S2$, S3$, S4$
Open "d:\vbtxt2.txt" For Output As #1
Open "d:\vbtxt.txt" For Input As #2
Do While Not EOF(2)
Line Input #2, S
If InStr(S, "王權") > 0 Then
x = InStr(S, "王權"如鎮)
S1 = Left(S, x + 1)
S4 = S1
S2 = Right(S, Len(S) - x - 1)
For i = 1 To Len(S2)
S3 = Mid(S2, i, 1)
If S3 <> " " Then '找到王權後面的第一個答孝數字,即改渣舉粗為7
S4 = S4 & "7"
Exit For
Else
S4 = S4 & S3
End If
Next i
S4 = S4 & Mid(S2, i + 1)
Print #1, S4
Else
Print #1, S
End If
Loop
Close (1)
Close (2)
Kill ("d:\vbtxt.txt") '刪除原來的文件
Name "d:\vbtxt2.txt" As "d:\vbtxt.txt" '修改後來的文件名為原來一樣
MsgBox "操作完畢!", , "OK"
End Sub