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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航