标题: [问题求助] VBS如何批量处理csv文件中文本文字的替换? [打印本页]
作者: whxj2004 时间: 2012-12-12 21:59 标题: VBS如何批量处理csv文件中文本文字的替换?
我有一大批csv,里面很多文本需要替换;例如:
1.csv,2.csv......中,我要将所有的JAN替换成01;FEB替换成02......依次类推到DEC替换为12!
还请高手指点下如何写这段VBS,我的CSV文件很大,每个都有几万条数据;每天要转换100多个这种CSV,还请高手解惑!万分感谢!
作者: czjt1234 时间: 2012-12-13 09:37
本帖最后由 czjt1234 于 2012-12-13 09:41 编辑
直接替换当前目录下所有.csv文件里的数据,不做备份
请先在新建文件夹里试验
其中 strPath 可自定义路径- Dim Month(11,1), strRead
-
- strPath = CreateObject("Wscript.Shell").CurrentDirectory
-
- Month(0,0) = "Jan" : Month(0,1) = "01"
- Month(1,0) = "Feb" : Month(1,1) = "02"
- Month(2,0) = "Mar" : Month(2,1) = "03"
- Month(3,0) = "Apr" : Month(3,1) = "04"
- Month(4,0) = "May" : Month(4,1) = "05"
- Month(5,0) = "Jun" : Month(5,1) = "06"
- Month(6,0) = "Jul" : Month(6,1) = "07"
- Month(7,0) = "Aug" : Month(7,1) = "08"
- Month(8,0) = "Sep" : Month(8,1) = "09"
- Month(9,0) = "Qct" : Month(9,1) = "10"
- Month(10,0) = "Nov" : Month(10,1) = "11"
- Month(11,0) = "Dec" : Month(11,1) = "12"
-
- Set objFSO = CreateObject("Scripting.FileSystemObject")
-
- Set objFolder = objFSO.GetFolder(strPath)
- set objFiles = objFolder.Files
-
- for Each objFile In objFiles
- If LCase(Right(objFile.Name, 3)) = "csv" Then
- With objFSO.OpenTextFile(objFile.Name, 1)
- strRead = .ReadAll
- .Close
- End With
-
- For i = 0 To 11
- With New RegExp
- .IgnoreCase = True
- .Global = True
- .Pattern = Month(i,0)
- strRead = .Replace(strRead, Month(i,1))
- End With
- Next
-
- With objFSO.OpenTextFile(objFile.Name, 2)
- .Write strRead
- .Close
- End With
-
- Msgbox objFile.Name & " 替换完毕。"
- End If
- Next
复制代码
作者: apang 时间: 2012-12-13 10:45
也来一个- Arr = Array("Jan","Feb","Mar","Apr","May","Jun",_
- "Jul","Aug","Sep","Oct","Nov","Dec")
- Set FSO = CreateObject("Scripting.FileSystemObject")
- For Each File in FSO.GetFolder(".").Files
- Ext = FSO.GetExtensionName(File)
- If Lcase(Ext) = "csv" Then MySub
- Next
- MsgBox "OK"
-
- Sub MySub()
- Set F = FSO.OpenTextFile(File,1)
- Text = F.ReadAll :F.Close
- For i = 0 To Ubound(Arr)
- Set Re = New RegExp
- Re.Pattern = Arr(i)
- Re.Global = True
- Re.IgnoreCase = True
- Text = Re.Replace(Text,Right("0"&i+1,2))
- Next
- Set F = FSO.OpenTextFile(File,2)
- F.Write Text :F.Close
- Set F = Nothing
- End Sub
复制代码
作者: whxj2004 时间: 2012-12-14 15:13
本帖最后由 whxj2004 于 2012-12-14 15:27 编辑
非常好用,感谢2位!简直帮了我大忙!运行很快!
但是出现新问题了:
原来csv中的日期,例如:01-May-12,是2012-05-01。
转换后,显示为01-05-12,使用excel打开识别为,2001-05-12,头大!
高人们,有没有更加好些的办法?
作者: czjt1234 时间: 2012-12-14 16:11
要不把-12也替换为-2012
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |