批量修改同一个目录中的所有文本文件的方法!
2005-05-21 00:59
351 查看
在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下执行通过
我想把所有的文件中的一列数据去掉如:
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下执行通过
相关文章推荐
- 黑马程序员—这是一个能够键盘录入路径去批量修改指定文件夹下所有指定文件内容的方法
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- 批量修改文件后缀名的方法(当前目录及子目录)
- 批量修改文件后缀名的方法(当前目录及子目录)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
- shell脚本批量修改目录下所有文件中的部分语句
- linux下批量修改目录以及子目录里的所有文件的时间
- linux下批量修改目录下所有文件夹、文件名称及文件内容
- linux目录下的所有文件批量创建链接(但目录不链接)的实现方法
- C#.NET 扫描目录下所有的文本文件方法
- 批量修改同一目录下文件名--操作so easy
- linux下批量修改文件目录下所有cpp文件的最后更改时间
- shell脚本批量修改目录下所有文件中的部分语句
- java批量修改同一目录下具有相同特性的文件名
- 批量修改某一个目录的后缀名
- 使用Shell遍历目录及其子目录中的所有文件方法
- PHP使用递归方式列出当前目录下所有文件的方法
- VFP_获得给定目录下所有文件夹路径-非递归方法(转)
- GIT 版本控制软件的常用命令记录每次更新到仓库 现在我们手上已经有了一个真实项目的 Git 仓库,并从这个仓库中取出了所有文件的工作拷贝。接下来,对这些文件作些修改,在完成了一个阶段的目标和使用方法