My FAQ,最新最全的IT技术FAQ
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 未整理篇 | 技术讨论
  当前位置: > 程序开发 > 编程语言 > .NET > 临时文章
批量修改同一个目录中的所有文本文件的方法!
作者:未知 时间:2005-07-27 21:49 出处:CSDN 责编:My FAQ
              摘要:批量修改同一个目录中的所有文本文件的方法!

在Temp/目录下,所有的*.txt文件.
我想把所有的文件中的一列数据去掉如:
a.txt  -----> a.txt
a b 1 1       a 1 1
c d 2 2       c 2 2
e f 3 3       e 3 3
g h 4 4       g 4 4
i j 5 5       i 5 5

解决方法:
'批量处理文本文件的方法
'a:批量处理某一个目录下的文本文件
'b:批量修改文本文件中的第二列数据去掉

得到Temp/目录中的所有文本文件

 Dim di As IO.DirectoryInfo
 di = New IO.DirectoryInfo("F:\\Root\tbak")
 Dim fi As IO.FileInfo

For Each fi In di.GetFiles("*.txt")
            Dim s As String
            Dim reader As IO.StreamReader
            reader = New IO.StreamReader(fi.FullName)
            s = reader.ReadToEnd
            reader.Close()

'第一个文件处理操作
            ProcessString(s, fi.FullName)
        Next

--文件中处理过程

    Public Sub ProcessString(ByVal content As String, ByVal FileName As String)
        Dim reader As IO.StringReader
        reader = New IO.StringReader(content)
        Dim writer As IO.StreamWriter
        writer = New IO.StreamWriter(FileName)
        Dim line As String
        line = reader.ReadLine()
        While (line <> Nothing)
            Dim ss As String()
            ss = line.Split(" ")

--处理数租操纵
            Array.Copy(ss, 2, ss, 1, ss.Length - 2)
            writer.WriteLine(String.Join(" ", ss, 0, ss.Length - 1))
            line = reader.ReadLine
        End While
        writer.Close()
        reader.Close()
    End Sub

--window Xp ,SQL_SERVER2000下测试通过!

---文件Copy,然后放到指定目录,删除原来目录文件

'60秒刷新页面,判断是否有r0011.txt文件
'如果r0011.txt文件不存在,在指定目录中去Copy文本文件,设置当前时间为:文本文件时间+"23:59:00",Copy文件
' C:\,并更名为:r0011.txt文件
'等待60秒,循环

 '每30秒刷新一次
        Dim Lsql As String

        Response.AddHeader("Refresh", "30")
        If IO.File.Exists("F:\Root\r0011.txt") Then
            Response.Write("数据正等待处理.......")
        Else
            '从源目录中拷贝文件
            Dim di As IO.DirectoryInfo
            di = New IO.DirectoryInfo("F:\\Root\tbak")
            Dim fi As IO.FileInfo
            For Each fi In di.GetFiles("*.txt")

                If IO.File.Exists("F:\Root\r0011.txt") Then
                    '如果上一个文件还没入库,则等待
                    Response.Write("上一个文件正在进行入库处理.......")
                    Exit For
                Else

                    Dim s, Year, Month, day As String
                    Dim reader As IO.StreamReader

                    reader = New IO.StreamReader(fi.FullName)
                    s = reader.ReadToEnd
                    '更新系统当然时间.
                    'a:得到年份
                    Year = "20" + Left(Right(fi.Name(), Len(fi.Name) - 1), 2)
                    'b:得到月份
                    Month = Right(Left(Right(fi.Name(), Len(fi.Name) - 1), 4), 2)
                    'c:得到日期
                    day = Right(Left(Right(fi.Name(), Len(fi.Name) - 1), 6), 2)
                    '设置系统时间
                    Lsql = " exec master..xp_cmdshell 'date " & day & "-" & Month & "-" & Year & "' "
                    Lsql = Lsql + "exec master..xp_cmdshell 'time 23:50:00' "

                    ZeHua.Data.DataHelper.exeSQL("Provider=SQLOLEDB;Server=(local);database=IDBS;user id=sa;password=", Lsql)

                    ProcessString(s, "F:\Root\r0011.txt")
                    reader.Close()

                    '如果文件存在,删除文件
                    If IO.File.Exists(fi.FullName) Then
                        IO.File.Delete(fi.FullName)
                    End If

                End If

            Next

            If fi Is Nothing Then
                Response.Write("数据已经全部处理完毕!")
            End If

        End If

 Public Sub ProcessString(ByVal content As String, ByVal FileName As String)
        Dim sw As IO.StreamWriter
        sw = New IO.StreamWriter(FileName, False, System.Text.Encoding.Default)
        sw.Write(content)
        sw.Close()
    End Sub

--windowxp  SQL-Server2000下执行通过


 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有