阅读背景:

VB:读取一个txt中的字符串,在某个路径下搜索含有该字符串的文件,并拷贝到特定路径

来源:互联网 
1、读取E:
1、读取E:\1.txt中的字符串,txt中内容一共3行,如下:
PipeWeldingJoint
Expressions
tsOutputData
2、读取第一行PipeWeldingJoint,在D:\workdir及其子文件夹下搜索含有该字符串的文件
3、复制文件到指定目录E:\

8 个解决方案

#1


虽然你给了100分,我还是要说:苦力活自己做,什么地方有错了再来问。

#2


难度不大,就是要实现单过程的话难度就大了,文件穷举比较费事,做成几个函数过程+递归做就好些,别的没什么了,留给勤快人接这活吧

#3


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If My.Computer.FileSystem.FileExists(TextBox2.Text & "\devtest.txt") = False Then
            MsgBox("could not find devtest file")
            Return
        End If


        str3 = RichTextBox1.Text
        str3 = sb(str3)

        If str3 = "" Then
            MsgBox("the unit path is null", , "error")
            Return
            End
        End If

        If TextBox2.Text = "" Then
            TextBox2.Text = "E:\pr_data"
        End If

        If str7 = "" Then
            str7 = "E:\pr_data"
        End If

        If xml_file = True Then
            search_xml = New Threading.Thread(AddressOf xml)
            search_xml.Start()
        Else

        End If

        Dim n As Integer = 0
        Dim txt As IO.StreamReader = New IO.StreamReader(str7 & "\devtest.txt", System.Text.Encoding.Default)
        Do Until txt.EndOfStream
            n = n + 1
            ReDim Preserve str(n) '数组从1开始
            str(n) = txt.ReadLine()
        Loop
        txt.Close()
        Dim fgs As New System.IO.FileInfo(str7 & "\devtest.txt")
        If fgs.Length = 3 Then
            MsgBox("The devtest.txt is a null file.")
            Return
        End If


        '读取网页
        Dim MyClient As Net.WebClient = New Net.WebClient
        Dim MyReader As New System.IO.StreamReader(MyClient.OpenRead("file:///" & str3 & "/dt/runs/run1/logs/results/devtestMasterResults.xml"), System.Text.Encoding.UTF8)
        Dim MyWebCode As String = MyReader.ReadToEnd
        Dim strline2 As String
        strline2 = MyWebCode
        MyReader.Close()

        '读取数据
        Dim a, b, d, x, y, m, z, r As Integer
        Dim g, h, o, c As String
        g = Nothing
        Dim k, p As String
        h = Nothing
        o = Nothing
        r = 1
        a = 0

        For r = 1 To n Step 1
            strline = str(r)
            a = InStr(strline2, str(r))
            'MsgBox(str(r))
            If a <> 0 Then
                b = InStr(a, strline2, " id=")
                c = Mid(strline2, a, b - a - 1)
                'MsgBox(c)
                'MsgBox(strline)
                If c = strline Then
                    Do Until g <> ""
                        strline2 = MyWebCode
                        j = InStr(strline2, str(r))

                        If j Then
                            d = InStr(j, strline2, "relToThisFile=")
                            x = InStr(d, strline2, "name=")
                            y = InStr(d, strline2, "><")
                            g = Mid(strline2, d + 18, x - d - 20)
                            h = Mid(strline2, x + 6, y - x - 7)
                            strline2 = MyWebCode
                            m = InStr(y, strline2, "name=")
                            z = InStr(m, strline2, "><")
                            o = Mid(strline2, m + 6, z - m - 7)
                        End If
                    Loop
                    'MsgBox(str3)
                    k = str3 & "\dt\runs\run1\logs\" & g & "/" & h
                    p = str3 & "\dt\runs\run1\logs\" & g & "/" & o
                    'MsgBox(o)
                    My.Computer.FileSystem.CopyFile(p, TextBox2.Text & "\" & o) 'copy  .syslog 
                    My.Computer.FileSystem.CopyFile(k, TextBox2.Text & "\" & h) 'copy  .bad

                    g = ""
                Else
                   
                End If
            Else

                inexistence(r) = str(r)
                Dim aaa As Integer
                For aaa = 1 To r Step 1
                    My.Computer.FileSystem.WriteAllText("C:\Temp\temp.txt", inexistence(aaa) & vbCrLf, True)
                Next
                Process.Start("C:\Temp\temp.txt")

            End If
        Next


        Dim filenumber As Integer = GetFileNum(TextBox2.Text)
        Dim seqnumber As Integer = n * 2 + 1
        If filenumber = seqnumber Then
            MsgBox("Done")
        ElseIf filenumber < seqnumber Then
            MsgBox("Some file can't copy to " & TextBox2.Text, , "Error")
        End If

    End Sub

#4


我上面的代码是在一个内嵌的HTML中需找字符串,然后用这个字符串寻找文件,copy到指定目录
现在只是将问题简化了,上面代码,已经能够读取html中的字符串了。我想先用txt读字符串,学习下如何在某个文件(含子文件)下面搜索含有该字符串的文件。这个我之前没学过,自学了一段时间,对一些索引算法还是不懂,大神求帮助啊!都困扰我好长时间了
制作的小工具“FIND”就是寻找这些软件的,其他功能都是实现了就剩下这一个,受够了

#5


dir函数就可以穷举指定文件夹下的文件了
第一次调用时加路径 Dir(Path)
然后调用的时候用空参数 Dir("")
每次调用返回一个Path下的文件或者文件夹,直到返回值为“”时结束
如果返回的是文件夹,就需要递归下一级

#6


.Net 不是有现成的 My.Computer.FileSystem.FindInFiles() 可用吗。

#7


C:\> findstr /?
在文件中寻找字符串。

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         在一行的开始配对模式。
  /E         在一行的结尾配对模式。
  /L         按字使用搜索字符串。
  /R         将搜索字符串作为一般表达式使用。
  /S         在当前目录和所有子目录中搜索匹配文件。
  /I         指定搜索不分大小写。
  /X         打印完全匹配的行。
  /V         只打印不包含匹配的行。
  /N         在匹配的每行前打印行数。
  /M         如果文件含有匹配项,只打印其文件名。
  /O         在每个匹配行前打印字符偏移量。
  /P         忽略有不可打印字符的文件。
  /OFF[LINE] 不跳过带有脱机属性集的文件。
  /A:attr    指定有十六进位数字的颜色属性。请见 "color /?"
  /F:file    从指定文件读文件列表 (/ 代表控制台)。
  /C:string  使用指定字符串作为文字搜索字符串。
  /G:file    从指定的文件获得搜索字符串。 (/ 代表控制台)。
  /D:dir     查找以分号为分隔符的目录列表
  strings    要查找的文字。
  [drive:][path]filename
             指定要查找的文件。

除非参数有 /C 前缀,请使用空格隔开搜索字符串。
例如: 'FINDSTR "hello there" x.y' 在文件 x.y 中寻找 "hello" 或
"there"。'FINDSTR /C:"hello there" x.y' 文件 x.y  寻找
"hello there"。

一般表达式的快速参考:
  .        通配符: 任何字符
  *        重复: 以前字符或类别出现零或零以上次数
  ^        行位置: 行的开始
  $        行位置: 行的终点
  [class]  字符类别: 任何在字符集中的字符
  [^class] 补字符类别: 任何不在字符集中的字符
  [x-y]    范围: 在指定范围内的任何字符
  \x       Escape: 元字符 x 的文字用法
  \<xyz    字位置: 字的开始
  xyz\>    字位置: 字的结束

有关 FINDSTR 常见表达法的详细情况,请见联机命令参考。

#8


貌似是字符串的模式匹配的内容。

.txt中的字符串,txt中内容一共3行,如下:
PipeWeldingJoint PipeWelding



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: