Loading...
  所在位置:论坛首页 -> ┈┋电脑网络技术区┋┈ -> Asp/Asp.Net技术 -> 批量修改同一个目录中的所有文本文件的方法
标题:批量修改同一个目录中的所有文本文件的方法收藏 编辑 删除 楼主 | 上一篇 下一篇
鞋的恋爱
等级:职业侠客
权限:普通用户
积分:63
金钱:4498
声望:109
经验:96
发帖数:1325
注册:2006年8月22日
资料 短消息2008-5-18 10:17:25

在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下执行通过

2008-5-18 10:17:25 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 商业版本 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com