批量修改同一个目录中的所有文本文件的方法!

博客介绍了批量处理Temp/目录下*.txt文件的方法,去掉文件中第二列数据。还给出了代码实现,包括获取文件、处理文件内容等。此外,涉及每30秒刷新页面,判断文件是否存在,若不存在则拷贝文件、更新系统时间并删除原文件,在window Xp和SQL_SERVER2000下测试通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值