Board logo

标题: [文本处理] 如何用批处理将半角引号替换为全角引号 [打印本页]

作者: pan528    时间: 2015-1-3 14:35     标题: 如何用批处理将半角引号替换为全角引号

如何用DOS批处理的方式,将半角引号替换为全角引号。
思路上:先将回车替换为一个特殊的数字,再在半角引号前插入回车,然后根据单行替换为左引号,双行替换为右引号,去回车,最后再用回车符号替换特殊的数字……
但,怎样替换回车、用回车替换特殊符号等都搞不定,试了好多次,不得要领,请各位高手相助!先谢了!

材料:
更改后的效果:
作者: tmplinshi    时间: 2015-1-3 23:03

  1. fr -rnnl:"***:\"(.*?)\"" -t:"“\1”" 半角引号.txt
复制代码
如果要替换目录下所有 txt,则文件名用 *.txt
fr 下载地址: http://baiy.cn/utils/fr/index.htm
作者: pan528    时间: 2015-1-4 17:43

回复 2# tmplinshi


谢谢回贴。已通过测试!

只是 fr 是第三方工具。如果能用 DOS 写就完美了!
作者: yu2n    时间: 2015-1-4 18:33

VBS 算 DOS 么 ...
  1. file1 = "C:\Users\Yu2n\Downloads\半角引号.txt"
  2. file2 = "C:\Users\Yu2n\Downloads\半角引号1.txt"
  3. Set fso = CreateObject("Scripting.filesystemobject")
  4. set oTxt = fso.OpenTextFile(file1, 1, False, -2)
  5. str1 = oTxt.ReadAll
  6. For Each objItem In Split(str1, vbCrLf)
  7.   str2= str2 & regEx_Replace("("")(.*)("")", objItem, "“$2”") & vbCrLf
  8. Next
  9. set oTxt = fso.OpenTextFile(file2, 2, True, -2)
  10. oTxt.Write str2
  11. oTxt.Close
  12. Msgbox "Done!"
  13. Function regEx_Replace(ByVal sPattern, ByVal str, ByVal strReplace)
  14.   With CreateObject("VBScript.RegExp")
  15.     .Pattern=sPattern :  .IgnoreCase=True
  16.     .Global=True      :  .MultiLine=True
  17.     regEx_Replace = .Replace(str,strReplace)
  18.   End With
  19. End Function
复制代码

作者: pan528    时间: 2015-1-7 09:59

回复 4# yu2n


    感谢回贴,已通过测试!

VBS只是DOS的表弟,虽然更高级一些,但不是纯DOS。

希望有DOS高手能用纯dos代码解决问题。
作者: pan528    时间: 2015-1-7 10:40

回复 4# yu2n


   vbs我还没有入门,如果要批量处理文本和拖入文本,代码应怎样改,请指点。谢谢!
作者: yu2n    时间: 2015-1-7 12:09

本帖最后由 yu2n 于 2015-1-8 19:20 编辑

回复 6# pan528
批量处理文本和拖入文本?
建议直接做成浏览文件夹的形式,直接对文件夹里面所有的TXT类型文件处理。
选取多个拖放?好像Windows对拖放个数有限制,如果文件路径够长,估计一次处理不了多少文件。
VBS文件拖拽的个数限制(无法执行 – 参数列表过长)
http://demon.tw/programming/vbs-parameter-too-long.html


' VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n
  1. ' VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n
  2. Call CommandMode("VBS 批量替换TXT文件中的英文引号为成对中文引号 By Yu2n")
  3. Main
  4. Sub Main()
  5.   Dim strFolder, arrPath, strPath, nFileCount, i
  6.   strFolder = BrowseForFolder("请选择要替换的 TXT 文件所在目录:")
  7.   If strFolder = "" Then
  8.     WScript.Echo vbCrLf & " --- 错误:没有选择文件夹。程序即将退出 ..." & vbCrLf
  9.     Exit Sub
  10.   End If
  11.   arrPath = ScanFolder(strFolder)
  12.   ' 统计个数,用于显示进度
  13.   For Each strPath In arrPath
  14.     If LCase(Right(strPath,4))=".txt" Then nFileCount = nFileCount + 1
  15.   Next
  16.   ' 执行替换
  17.   Dim dtStart, objWord :  dtStart = Now()
  18.   For Each strPath In arrPath
  19.     If LCase(Right(strPath,4))=".txt" Then
  20.       i = i + 1 :  WScript.Echo "[" & i & "/" & nFileCount & "]" & strPath
  21.       Call DoReplace(strPath, strPath)    ' 执行替换
  22.     End If
  23.   Next
  24.   WScript.Echo vbCrLf & " --- " & nFileCount & " 个文档完成替换,耗时 " _
  25.     & DateDiff("s",dtStart,Now()) & " 秒。" & vbCrLf
  26. End Sub
  27. ' 替换英文引号为中文引号(成对)
  28. Sub DoReplace(ByVal file1, ByVal file2)
  29.   Set fso = CreateObject("Scripting.filesystemobject")
  30.   set oTxt = fso.OpenTextFile(file1, 1, False, -2)
  31.   str1 = oTxt.ReadAll
  32.   For Each objItem In Split(str1, vbCrLf)
  33.     str2= str2 & regEx_Replace("("")(.*?)("")", objItem, "“$2”") & vbCrLf
  34.   Next
  35.   set oTxt = fso.OpenTextFile(file2, 2, True, -2)
  36.   oTxt.Write str2
  37.   oTxt.Close
  38. End Sub
  39. ' 正则替换文本
  40. Function regEx_Replace(ByVal sPattern, ByVal str, ByVal strReplace)
  41.   With CreateObject("VBScript.RegExp")
  42.     .Pattern=sPattern :  .IgnoreCase=True
  43.     .Global=True      :  .MultiLine=True
  44.     regEx_Replace = .Replace(str,strReplace)
  45.   End With
  46. End Function
  47. ' 浏览文件夹
  48. Function BrowseForFolder(ByVal strTips)
  49.   Dim objFolder
  50.   Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001)
  51.   If (Not objFolder Is Nothing) Then BrowseForFolder = objFolder.Self.Path  'objFolder.Items().Item().Path
  52. End Function
  53. ' 获取文件夹所有文件夹、文件列表(数组)
  54. Function ScanFolder(ByVal strPath)
  55.     Dim arr() : ReDim Preserve arr(-1)
  56.     Call SCAN_FOLDER(arr, strPath) : ScanFolder = arr
  57. End Function
  58. Function SCAN_FOLDER(ByRef arr, ByVal folderSpec)
  59.   On Error Resume Next
  60.   Dim fso, objItems, objFile, objFolder
  61.   Set fso = CreateObject("Scripting.FileSystemObject")
  62.   Set objItems = fso.GetFolder(folderSpec)
  63.   If Right(folderSpec, 1) <> "\" Then folderSpec = folderSpec & "\"
  64.   If (Not fso.FolderExists(folderSpec)) Then Exit Function
  65.   For Each objFile In objItems.Files
  66.     ReDim Preserve arr(UBound(arr) + 1)
  67.     arr(UBound(arr)) = objFile.Path
  68.   Next
  69.   For Each objFolder In objItems.subfolders
  70.     Call SCAN_FOLDER(arr, objFolder.Path)
  71.   Next
  72.   ReDim Preserve arr(UBound(arr) + 1)
  73.   arr(UBound(arr)) = folderSpec
  74. End Function
  75. ' 以命令提示符环境运行(保留参数)
  76. Sub CommandMode(ByVal sTitle)
  77. If (LCase(Right(WScript.FullName,11)) = "wscript.exe") Then
  78.   Dim oArg, sArgs
  79.   For Each oArg In WScript.Arguments
  80.     sArgs = sArgs & " """ & oArg & """"
  81.   Next
  82.   CreateObject("WScript.Shell").Run( _
  83.     "cmd /c Title " & sTitle & " & Cscript.exe //NoLogo """ & _
  84.     WScript.ScriptFullName & """ " & sArgs & " & pause"), 1, False
  85.     Wscript.Quit
  86. End If
  87. End Sub
复制代码

作者: pan528    时间: 2015-1-8 19:01

回复 7# yu2n

谢谢回贴!

但测试出了一点问题:
1、一列中有多组引号时,只处理两头的引号,中间没有处理。
2、拖拽目录时弹出选择目录的提示,能否,智能判断,自动找到目录?
作者: yu2n    时间: 2015-1-8 19:21

本帖最后由 yu2n 于 2015-1-8 19:38 编辑

回复 8# pan528
1.  已更新代码。可自行修改 35 行代码:
  1. str2= str2 & regEx_Replace("("")(.*)("")", objItem, "“$2”") & vbCrLf
复制代码
替换为
  1. str2= str2 & regEx_Replace("("")(.*?)("")", objItem, "“$2”") & vbCrLf
复制代码
2. 本身不支持拖拽功能。是要记录最后一次打开位置的意思么?这个效果不太好。
  1.   Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001)
复制代码
修改为
  1.   Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001, "D:\")
复制代码
试试效果,那就只能选择这个位置了…
作者: pan528    时间: 2015-1-9 21:36

回复 9# yu2n


感谢你的耐心回贴!
全角问题解决了,批量处理也没有问题,虽然目录不能自动追踪,但对效率影响不是太大。
再次感谢你的帮助!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2