标题: [文本处理] 求BAT脚本将目录里txt文件里英文引号改为中文引号 [打印本页]
作者: dzwind 时间: 2024-3-11 15:31 标题: 求BAT脚本将目录里txt文件里英文引号改为中文引号
将目录中所有TXT里的英文引号变为中文引号。
作者: Five66 时间: 2024-3-11 17:08
换vbs或jscript或powershell或其他
作者: dzwind 时间: 2024-3-11 17:28
对于小白来说,真的是太难了。
作者: Five66 时间: 2024-3-11 18:45
保存为ansi编码的bat
txt文件编码需要为utf8
不成对引号的不替换
英文引号不分左右,结果可能不准确- #?&cls&pause&powershell -c "type -literalpath '%~f0'|out-string|iex"&pause&exit
-
- $cd='utf8'
-
- $z={param($s) '“'+$s.value.substring(1,$s.value.length-2)+'”'}
- $zz={param($s) "‘"+$s.value.substring(1,$s.value.length-2)+"’"}
-
- gci -n|%{
- $str=[io.file]::ReadAllText($pwd.path+"\"+$_)
- $str=[regex]::Replace($str,'".*?"',$z)
- $str=[regex]::Replace($str,"'.*?'",$zz)
- sc -LiteralPath ("__new"+$_) -Encoding $cd -value $str
- [console]::writeline("has been save to file: "+"__new"+$_)
- $str=""
- }
复制代码
作者: aloha20200628 时间: 2024-3-11 19:00
本帖最后由 aloha20200628 于 2024-3-11 19:49 编辑
回复 1# dzwind
给一个批处理版本如下,代码存为test.bat
运行方法如》test.bat "源文件.txt"
读取命令行参数为源文件,输出文件扩展名为*.new
中文双引号可选两种类型,见11-12行- @echo off &setlocal enabledelayedexpansion
- if "%~1"=="" exit/b
- set/a "pm=1"
- (for /f "usebackq delims=" %%a in ("%~1") do (
- set "s=%%a"&set "_s=%%a"
- for %%p in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
- if "!_s:~%%p,1!" neq "" (set/a "ls+=%%p"&set "_s=!_s:~%%p!")
- )
- for /L %%n in (0,1,!ls!) do (
- set "c=!s:~%%n,1!" &if ^!c! == ^" (
- set/a "pm=0-!pm!" &if !pm! equ -1 (set "c=「") else (set "c=」")
- REM set/a "pm=0-!pm!" &if !pm! equ -1 (set "c=“") else (set "c=”")
- )
- set "_line=!_line!!c!"
- )
- echo,!_line!&set "_line="&set "ls=0"
- ))>"%~1.new"
- endlocal&exit/b
复制代码
作者: aloha20200628 时间: 2024-3-11 19:30
本帖最后由 aloha20200628 于 2024-3-11 19:56 编辑
回复 1# dzwind
5楼代码可用于随机性测试单个文件,若均予通过,可用以下代码完成脚本所在目录下的全部*.txt替换...生成与每一个源文件对应的*.new新文件- @echo off &setlocal enabledelayedexpansion
- for /f "delims=" %%F in ('dir/s/b/a-d *.txt') do (
- set/a "pm=1"
- (for /f "usebackq delims=" %%a in ("%%~F") do (
- set "s=%%a"&set "_s=%%a"
- for %%p in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
- if "!_s:~%%p,1!" neq "" (set/a "ls+=%%p"&set "_s=!_s:~%%p!")
- )
- for /L %%n in (0,1,!ls!) do (
- set "c=!s:~%%n,1!" &if ^!c! == ^" (
- REM set/a "pm=0-!pm!" &if !pm! equ -1 (set "c=「") else (set "c=」")
- set/a "pm=0-!pm!" &if !pm! equ -1 (set "c=“") else (set "c=”")
- )
- set "_line=!_line!!c!"
- )
- echo,!_line!&set "_line="&set "ls=0"
- ))>"%%~F.new"
- )
- endlocal&exit/b
复制代码
作者: dzwind 时间: 2024-3-11 21:01
本帖最后由 dzwind 于 2024-3-12 15:39 编辑
谢谢楼上的各位大神
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |