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