Board logo

标题: [其他] [分享]纯批处理实现英文引号转化为中文引号 [打印本页]

作者: yiwuyun    时间: 2015-1-11 13:22     标题: [分享]纯批处理实现英文引号转化为中文引号

修改了我原来写的一个字符切分过程。不得已,必须破坏模块的局部特性,否则不能处理英文的!。
字符切分的代码可以切分除换行符之外的几乎所有可显字符。没试过其他标点符号,但分号,逗号,引号,括号,空格肯定可以
虽然常见到VBS,JS,和其他语言中有字符切分的函数,但批处理中没有,就写了一个。处理的文本放在1.txt中。效率肯定没
其他语言写的高。
  1. @echo off
  2. set __yiwuyun.txt=___yiwuyun.txt
  3. set quotation=^"
  4. echo 1 1>___m1.txt
  5. for /f "delims=" %%a in ('findstr /n .* 1.txt') do (
  6.   set "string=%%a"
  7.   setlocal EnableDelayedExpansion
  8.   set string=!string:*:=!
  9.   set "temp="
  10.   call :GetTokenTextNAndCount "^!string^!" "^!quotation^!"
  11.   set /p m1=<___m1.txt
  12.   for /l %%b in (1,1,!tokenTextCount!) do (
  13.     if %%b equ !tokenTextCount! (
  14.       set "temp=!temp!!tokenText%%b!"
  15.     ) else (
  16.       set /a m=!m1!%%2
  17.       if !m! equ 0 (
  18.         set "temp=!temp!!tokenText%%b!”"
  19.         set /a m1+=1
  20.         echo !m1!>___m1.txt
  21.       ) else (
  22.         set "temp=!temp!!tokenText%%b!“"
  23.         set /a m1+=1
  24.         echo !m1!>___m1.txt
  25.       )
  26.     )
  27.   )
  28.   echo.!temp!
  29.   endlocal
  30. )
  31. if exist ___m1.txt del ___m1.txt
  32. pause
  33. exit /b 0
  34. :GetStringLength
  35. setlocal
  36. set string=!string!
  37. set /a n=0
  38. :startGetStringLength
  39. if not "!string!"=="" (
  40.   set string=!string:~1!
  41.   set /a n=!n!+1
  42.   goto startGetStringLength
  43. )
  44. set /a stringLength=!n!
  45. endlocal&set stringLength=%stringLength%
  46. exit /b 0
  47. :GetTokenPlaceNAndCount
  48. setlocal
  49. set "string=%~1"
  50. set "secondParameter=%~2"
  51. call :GetStringLength
  52. set /a nGetTokenPlaceNAndCount=!stringLength!-1
  53. set /a nToken=1
  54. set /a tokenPlace0=-1
  55. for /l %%a in (0,1,!nGetTokenPlaceNAndCount!) do (
  56.   if "!string:~%%a,1!"=="!secondParameter!" (
  57.      set /a tokenPlace!nToken!=%%a
  58.      set /a nToken+=1
  59.   )
  60. )
  61. set /a tokenPlace!nToken!=!stringLength!
  62. set /a tokenCount=!nToken!-1
  63. type nul>!__yiwuyun.txt!
  64. for /l %%a in (0,1,!nToken!) do (
  65.    echo set /a tokenPlace%%a=!tokenPlace%%a!>>!__yiwuyun.txt!
  66. )
  67. echo set /a tokenCount=!tokenCount!>>!__yiwuyun.txt!
  68. endlocal
  69. for /f "tokens=1 delims=" %%a in ('type !__yiwuyun.txt!') do (
  70.   rem echo %%a
  71.   %%a
  72. )
  73. if exist !__yiwuyun.txt! del !__yiwuyun.txt!
  74. exit /b 0
  75. :GetTokenTextNAndCount
  76. rem 该函数必须去掉局部性,要不然不能处理感叹号。
  77. rem setlocal
  78. set "string=%~1"
  79. set "___secondParameter=%~2"
  80. rem if "!___secondParameter!"==" " (
  81. rem call :GetStringTrimRedundanceSpace
  82. rem set string=!stringTrimRedundanceSpace!
  83. rem )
  84. call :GetTokenPlaceNAndCount "^!string^!" "^!___secondParameter^!"
  85. set /a ___next=0
  86. set /a tokenTextCount=!tokenCount!+1
  87. set /a ___n=0
  88. :startGetTokenTextNAndCount
  89.   set /a ___beforePlace=!tokenPlace%___next%!
  90.   set /a ___first=!___beforePlace!+1
  91.   set /a ___next=!___next!+1
  92.   set /a ___afterPlace=!tokenPlace%___next%!
  93.   set /a ___second=!___afterPlace!-!___beforePlace!-1
  94.   set /a ___n=!___n!+1
  95.   if "!string!"=="" (
  96.     set "tokenText!___n!="
  97.   ) else (
  98.     set tokenText!___n!=!string:~%___first%,%___second%!
  99.   )
  100. if !___n! lss !tokenTextCount! goto startGetTokenTextNAndCount
  101. rem type nul>!__yiwuyun.txt!
  102. rem for /l %%a in (1,1,!tokenTextCount!) do (
  103. rem   echo set "tokenText%%a=!tokenText%%a!">>!__yiwuyun.txt!
  104. rem )
  105. rem echo set /a tokenTextCount=!tokenTextCount!>>!__yiwuyun.txt!
  106. rem endlocal
  107. rem for /f "tokens=1 delims=" %%a in ('type %__yiwuyun.txt%') do (
  108.   rem echo %%a
  109. rem  %%a
  110. rem )
  111. rem if exist %__yiwuyun.txt% del %__yiwuyun.txt%
  112. exit /b 0
复制代码

作者: mms0536    时间: 2015-1-11 18:14


看着好晕...怎么处理配对啊
要是不处理配对,下面的方式也可以处理
@echo off &echo test
for /f "delims=" %%a in (test.txt) do (
call :replace_str %%a
)
echo ok
pause
exit
:replace_str
set replace_ok=%1
set replace_ok=%replace_ok:“="%
set replace_ok=%replace_ok:”="%
echo %replace_ok%
goto :eof
作者: dzwind    时间: 2024-3-11 17:32

请问我运行后为什么没有反应?




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