Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在VB6中检查文件是否包含字符串_String_File_Vb6 - Fatal编程技术网

String 在VB6中检查文件是否包含字符串

String 在VB6中检查文件是否包含字符串,string,file,vb6,String,File,Vb6,如何检查某个文本文件是否包含某个字符串? 我是否必须实际打开文件并使用InStr(),还是有更方便的方法 顺便说一下,该语言是Visual Basic 6.0如果不打开文件,则无法使用,但还有一种替代方法(也包括打开文件)可以使用Shell,如下所示: <filename> is the name of the file <string> is the string you want to check 是文件名 是要检查的字符串 (假设要检查的文件位于当前目录中) S

如何检查某个文本文件是否包含某个字符串? 我是否必须实际打开文件并使用InStr(),还是有更方便的方法


顺便说一下,该语言是Visual Basic 6.0

如果不打开文件,则无法使用,但还有一种替代方法(也包括打开文件)可以使用Shell,如下所示:

<filename> is the name of the file
<string> is the string you want to check
是文件名
是要检查的字符串
(假设要检查的文件位于当前目录中)

Shell(“find/c”+chr(34)+“+chr(34)+>test.txt”)
打开“test.txt”输入为#1
输入#1,A
关闭#1
如果(右(A,3)=“:0”),则
Msgbox“不存在”
其他的
Msgbox“字符串存在”
如果结束

虽然这也涉及到打开文件,但它比使用InStr()函数搜索字符串要快得多(如果文件很大),因为它现在检查常量字符串。此外,您还可以稍微更改shell命令以同时搜索多个文件中的文本(使用通配符),这肯定比使用循环打开所有文件、使用另一个循环读取所有行并在每行上使用InStr()要快得多。

是的,您需要先加载文件。如果文件太大,无法装入内存,请将其分块加载。如果您是在下载代码,请检查此项。管理(文本)文件的最简单组件是FileSystemObject(在Microsoft脚本运行时中引用)。他询问如何使用VB6执行此操作,您建议使用
查找
,真的吗?如何检查错误(例如,命令shell无法创建
test.txt
)?我还怀疑关于您的解决方案的显著优越性能的说法是否有充分的根据。我很好奇,
Right(A,3)=“:0”
是否仍然是正确的签入,比如说,从右到左的区域设置。如果命令shell无法创建文件“test.txt”,我们可以始终使用PathFileExists()Win32 API进行检查;我相信在VB中打开一个文件,然后循环读取文件中的每一行,然后在每一行上使用InStr(),比使用“Find”要慢。至于你所指的地区,是的,我承认这可能是个问题。
    Shell("find /c "+chr(34)+"<string>"+chr(34)+" <filename>  > test.txt")

    Open "test.txt" for input as #1
        input #1, A
    Close #1

    If (Right(A,3)=": 0") Then 
            Msgbox "Does not exist"
    Else
            Msgbox "String exists"
    End if