vb怎样删除txt文本文件中有“非数字字符”的行?

2024-12-22 11:03:04
推荐回答(6个)
回答1:

'添加一个filelistbox,一个commandbutton
Private Sub Command1_Click()
Dim i As Integer
If Dir(App.Path & "\abc", vbDirectory) = "" Then MsgBox "没有“" & App.Path & "\abc" & "”文件夹": Exit Sub
File1.Path = App.Path & "\abc"
For i = 0 To File1.ListCount - 1
File1.ListIndex = i
If Right(File1.FileName, 3) = "txt" Then zhzsucyg (App.Path & "\abc\" & File1.FileName)
Next
End Sub
Private Function zhzsucyg(myFile As String)
Dim myLine() As String, myTemp As String, myOK As Boolean, myOutput As String, i As Integer, j As Integer
Open myFile For Binary As #1
myLine = Split(Input(LOF(1), 1), vbCrLf)
Close
For i = 0 To UBound(myLine)
myTemp = myLine(i)
For j = 0 To 9
myTemp = Replace(myTemp, CStr(j), "")
Next
If myTemp = "" Then myOutput = myOutput & myLine(i) & vbCrLf: myOK = True
Next
If myOK = True Then
Open myFile For Output As #1
Print #1, myOutput
Close
myOK = False
End If
End Function

回答2:

Private Sub Command1_Click()
dim txt as integer
test=text1.text
If Dir("C:\abc\text.text", vbDirectory) <> "" Then
MsgBox "文件夹:C:\WINDOWS\system32\roect 存在!"
Else
MkDir "C:\abc\text.text"
If (Asc(test) >= 48 And Asc(TemporarySave) <= 57) Then
For n = 0 To UBound(test)
If IsNumeric(test(n)) = False Then testa(n) = ""
Next
s = Join(test, vbCrLf)
Set ts = fil1.OpenAsTextStream(ForWriting)
ts.Write s
ts.Close

End If
End Sub

回答3:

-- 1行 Perl 解决问题

perl -i -pe "s/^[^\n]*?[^\d\s[:punct:]\r\n][^\r\n]*\r?$[\n]//m" *.txt

别和我提什么Commandbar~~

回答4:

fso对象的subfiles遍历某文件夹下的所有文件,过滤出txt文件,然后将3楼的代码做成一个函数,参数为文件名,即可完成

回答5:

可以用ASC函数来判断它的码值来判断是否是数字(数字的码值是48----57)

回答6:

直接以FSO对象,打开文件,进行处理,很方便