本帖最后由 aloha20200628 于 2023-11-27 18:00 编辑
一。不写一堆纯P代码,就要用其他手段代劳。系统原装的 jscript 或外部资源如 python、sed.exe 等可以逐步简化一楼示例的解法》
以下6行代码存为批处理脚本运行》cmd+jscript混编- @set @v=1 /*
- @echo off
- for /f "delims=" %%F in ('dir/b/s/a-d *.txt') do (type "%%~F"|cscript /e:javascript "%~f0">"%%~F.new")
- exit/b
- */
- lines=WSH.stdin.readall().replace(/#[^#]*#/g, ''); WSH.echo(lines);
复制代码 以下5行代码存为批处理脚本运行》假设系统已经预装python- @echo off
- for /f "delims=" %%F in ('dir/b/s/a-d *.txt') do (
- type "%%~F"|python -c "import sys,re;s=sys.stdin.read();print(re.sub('#[^#]*#','',s,re.S))">"%%~F.new"
- )
- exit/b
复制代码 以下2行代码存为批处理脚本运行》假设sed.exe已被下载安装在系统路径- @echo off &for /f "delims=" %%F in ('dir/b/s/a-d *.txt') do (sed "s/#[^#]*#//g" "%%~F">"%%~F.new")
- exit/b
复制代码 二。再给一段纯P代码》未发现内置的字符串操作捷径,只能用纯P的基本方法,先获取字符串长度再逐字判断以求目标结果... 此法虽然基本但确有灵活性,可有多种字符串处理用途。
以下代码存为批处理脚本运行》- @echo off &setlocal enabledelayedexpansion
- for /f "delims=" %%F in ('dir/b/s/a-d *.txt') do (
- (for /f "usebackq delims=" %%s in ("%%~F") do (
- set "cut=-1" &set "_s=" &set "s=%%~s" & (call :_strLen s sl)
- for /L %%i in (0,1,!sl!) do (
- set "c=!s:~%%i,1!"
- if "!c!" neq "#" (if !cut! equ -1 set "_s=!_s!!c!") else (set/a "cut=0-!cut!")
- )
- echo,!_s!
- ))>"%%~F.new"
- )
- endlocal&exit/b
-
- :_strLen //获取字符串长度 %1=被测字符串变量名 %2=返回值变量名
- set "_str=!%~1!" &set "Len=0"
- for %%n in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
- if "!_str:~%%n,1!" neq "" (set/a "Len+=%%n"&set "_str=!_str:~%%n!")
- )
- set/a "%~2=%Len%" &exit/b
复制代码
|