導航:首頁 > 文件教程 > vb查找包含某些字元的文件

vb查找包含某些字元的文件

發布時間:2024-09-02 03:07:06

㈠ 請問vb打開文件名含有指定字元的文件的代碼是什麼啊

d=Dir("d:ab*.txt")
Ifd=""Then
MsgBox"文件未找到!"
Else
Open"d:"&dForInputAs#1
'.......文件操作過程略.......
Close#1
EndIf

㈡ vb怎樣讀取文件夾中含有某一字元的最新的文本文檔

這位在讀碩士,如果你對樓上網友們的答復不夠滿意,請看看下面解答吧,慎重起見代碼已通過實測,點擊按鈕實現你的要求:
Private Sub Command1_Click()
On Error GoTo err
Dim file() As String, str As String, n As Long
Dim dirxn As String, fle As String
Dim editTime As Date '用於存儲文件更新時間
Dim fname As String, findTxt As String
Dim InputData As String '用於存儲逐行讀取的文本內容
Dim FoundFile As String '用於存放找到的文本文件路徑名名
Dim fileContent As String '用於存放被讀取的含有某一字元的最新的文本文檔全部內容
dirxn = "F:\" '指定一個文件夾
fle = "*.txt" '指定文本文件類型
findTxt = "中國" '指定要查找的字元串
'逐個打開指定文件夾下的文本文件
str = Dir(dirxn & fle)
While Len(str) > 0
n = n + 1
ReDim Preserve file(1 To n)
file(n) = str
fname = dirxn & str
'逐行讀取文本內容
Open fname For Input As #1
Do While Not EOF(1)
Line Input #1, InputData
'用InStr函數判斷是否含有指定字元
If InStr(1, InputData, findTxt, vbTextCompare) <> 0 And Not IsNull(InStr(1, InputData, findTxt, vbTextCompare)) Then
'如果含指定字元,用FileDateTime函數獲取文檔更新時間
If IsNull(editTime) Then '如果文件更新時間editTime尚未賦值
FoundFile = fname
editTime = FileDateTime(fname)
Else '已賦值則與前一個文檔比較更改那個最新
If FileDateTime(fname) > editTime Then
'如果當前文件較新則更新時間設為當前文件之更新時間,當前文件路徑名則設為被找到的文件路徑名
editTime = FileDateTime(fname)
FoundFile = fname
End If
End If
Exit Do '使用跳出內循環節省系統開銷,因為被查字元只要出現一次就可以做出判斷
End If
Loop
Close #1
str = Dir()
Wend '外循環
If Not IsNull(FoundFile) Then '如果找到了含指定字元串的最近更新文本文件,將文本文件內容全部讀進變數fileContent中
Open FoundFile For Input As #1
Do While Not EOF(1)
Line Input #1, InputData
fileContent = fileContent & InputData & Chr(13) + Chr(10)
Loop
Close #1
End If
'此時你便可以使用該變數,例如顯示於文本框等TEXT1.Text=fileContent或用消息框顯示deng等
MsgBox fileContent
err: '錯誤處理
If err <> 0 Then MsgBox "該文件夾里沒有找到對應的文本文件"
End Sub

再推薦一個讀取文本的自定義函數
Private Function GetFile(FileName As String) As String
Dim i As Integer, s As String, BB() As Byte
If Dir(FileName) = "" Then Exit Function
i = FreeFile
ReDim BB(FileLen(FileName) - 1)
Open FileName For Binary As #i
Get #i, , BB
Close #i
s = StrConv(BB, vbUnicode)
GetFile = s
End Function
應用舉例
Dim a as String
a=GetFile("路徑名")

㈢ VB如何查找E盤下所有文件名包含某字元的jpg文件

提供一個 VB 遍歷指定文件夾下 文件的 代碼

'查找第一個文件的API
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
'查找下一個文件的API
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
'獲取文件屬性的API
Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
'關閉查找文件的API
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
'以下為調用瀏覽文件夾窗口的API
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

'常量
Public Const MAX_PATH = 260
Public Const MAXDword = &HFFFF
Public Const INVALID_HANDLE_VALUE = -1
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
Public Const BIF_RETURNONLYFSDIRS = 1

Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

'定義類(用於查找文件)
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

'定義類(用於瀏覽文件夾窗口)
Public Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type

'自定義函數
Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function

窗體代碼:
Function GetAllFilePath(path As String, SearchStr As String)
Dim FileName As String ' 文件名
Dim DirName As String ' 子目錄名
Dim dirNames() As String ' 目錄數組
Dim nDir As Integer ' 當前路徑的目錄數
Dim i As Integer ' 循環計數器變數
Dim hSearch As Long ' 搜索句柄變數
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer

If Right(path, 1) <> "\" Then path = path & "\"
'搜索子目錄
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(path & "*", WFD)

If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
If (DirName <> ".") And (DirName <> "..") Then
If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If

Cont = FindNextFile(hSearch, WFD) '獲取下一個子目錄
Loop

Cont = FindClose(hSearch)

End If

' 遍歷目錄並累計文件總數
hSearch = FindFirstFile(path & SearchStr, WFD)
Cont = True

If hSearch <> INVALID_HANDLE_VALUE Then
While Cont

FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
GetAllFilePath = GetAllFilePath + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
List1.AddItem path & FileName
If GetFileAttributes(path & FileName) = 32 Then
List2.AddItem FileName
End If
End If
Cont = FindNextFile(hSearch, WFD) ' 獲取下一個文件

Wend

Cont = FindClose(hSearch)
End If
'如果子目錄存在則遍歷之
If nDir > 0 Then
For i = 0 To nDir - 1
GetAllFilePath = GetAllFilePath + GetAllFilePath(path & dirNames(i) & "\", _
SearchStr)
Next i
End If
End Function

Private Sub Command1_Click()
Dim SearchPath As String
Dim FindStr As String
Dim FileSize As Long
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo

With udtBI
'設置瀏覽窗口
.hWndOwner = Me.hWnd
'返回選中的目錄
.ulFlags = BIF_RETURNONLYFSDIRS
End With

'調出瀏覽窗口
lpIDList = SHBrowseForFolder(udtBI)

If lpIDList Then
sPath = String$(MAX_PATH, 0)
'獲取路徑
SHGetPathFromIDList lpIDList, sPath
'釋放內存
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)

If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If

Screen.MousePointer = vbHourglass
List1.Clear
List2.Clear
SearchPath = sPath '選中的目錄為搜索的起始路徑
FindStr = "*.*" '搜索所有類型的文件(此處可另作定義)

FileSize = GetAllFilePath(SearchPath, FindStr)

Screen.MousePointer = vbDefault

End Sub

FindStr = "E:\*.jpg" 就可以 E盤遍歷.jpg文件

InStr(1, a, "123") 就可以識別 含有"123"的字元串: 返回 0 沒有,非 0 有

稍微改一下就可以了

這個程序主要是實現遍歷,可以找到指定文件夾下所有文件,包括子目錄下,
如果只需要搜索指定文件夾下文件,不處理子目錄,就可以不用這么復雜。

㈣ vb 按指定字元查找文件內容

你可以用Input$(1,文件號),每一次都會得到一個字元,只要看看是不是要的就可以了。

比方說:
Open "C:\f.txt" For Input As #1
Do While Not EOF(#1)
a$=Input$(1,#1)
If a$="f" Then
b=b+1
End If
Loop
Print "讀到";b;"個「f」"
Close #1
1、用filesystemobject方法可以
2、用OPEN命令打開文件
寫入內容(text1.text)
保存文件。
只要你在程序里不加msgbox提示,那它自然就沒有提示。偷偷的完成以上功能了。
這里建議你用filesystemobject來做,它處理文件很方便的!
工程--引用--microsoft scripting runtime
Dim Fso As FileSystemObject
Dim Load_File As TextStream
Set Fso = New FileSystemObject
Set Load_File = Fso.OpenTextFile("c:\3.txt")
Load_File.WriteLine text1.text
Load_File.Close
比如:
文本文件1.txt 的內容是:
a 12345
b 23456
c 47859
我在輸入框text1.text輸入:a
按確定
那麼輸入框text2.text輸出:12345
我在輸入框text1.text輸入:b
按確定
那麼輸入框text2.text輸出:23456
要怎麼實現?
提問者: x3602900 - 試用期 一級 最佳答案
Sub FindKeyword(ByVal kw As String)
Dim Stream$, filepath$ '建立變數
filepath = IIf(Right$(App.Path, 1) <> "\" And Right$(App.Path, 1) <> "/", App.Path & "\answer.txt", App.Path & "answer.txt")
'生成讀入文本路徑
Open filepath For Input As #1 '讀取
Do While Not EOF(1) '直到文件尾部
Line Input #1, Stream '行模式讀取
If Not EOF(1) Then '判斷是否到最後一行
Line Input #1, Stream '讀取下一行
If Mid$(Stream, 1, 1) = Text1.Text Then
Text2.Text = Mid$(Stream, 2) '輸出結果
End If
Else
Exit Do '直接退出do循環
End If
Stream = "" '初始化變數 : 清空
Loop
Close #1
End Sub

閱讀全文

與vb查找包含某些字元的文件相關的資料

熱點內容
ps入門必備文件 瀏覽:348
以前的相親網站怎麼沒有了 瀏覽:15
蘋果6耳機聽歌有滋滋聲 瀏覽:768
怎麼徹底刪除linux文件 瀏覽:379
編程中字體的顏色是什麼意思 瀏覽:534
網站關鍵詞多少個字元 瀏覽:917
匯川am系列用什麼編程 瀏覽:41
筆記本win10我的電腦在哪裡打開攝像頭 瀏覽:827
醫院單位基本工資去哪個app查詢 瀏覽:18
css源碼應該用什麼文件 瀏覽:915
編程ts是什麼意思呢 瀏覽:509
c盤cad佔用空間的文件 瀏覽:89
不銹鋼大小頭模具如何編程 瀏覽:972
什麼格式的配置文件比較主流 瀏覽:984
增加目錄word 瀏覽:5
提取不相鄰兩列數據如何做圖表 瀏覽:45
r9s支持的網路制式 瀏覽:633
什麼是提交事務的編程 瀏覽:237
win10打字卡住 瀏覽:774
linux普通用戶關機 瀏覽:114

友情鏈接