本帖最后由 CrLf 于 2011-12-21 07:49 编辑
发一个将普通“格式化”为变量型函数的工具,格式化后的函数,只有用变量延迟直接或二次扩展才能动态地读取变量,而且原有空行、以 : 开头的行均被忽略。
但是思路比较粗糙,对 % 未进行成对判断,对开启变量延迟后含有 ! 的命令未做充分的二次转义处理(难度太大,等于要解析语法),并且不支持扩展参数,参数外的双引号必须被去除(其实也不是必须,但是条件太苛刻)。
合并行.bat :- @echo off
- echo %*
- set "g=&"
- set "code=setlocal enabledelayedexpansion&set n=&set :=&for %%%%a in (1 1) do if defined : ((for %%%%b in (!:!) do set/a n+=1&set #!n!=%%%%b"
- for %%z in (%*) do (
- for /f "usebackeol=:tokens=*" %%b in ("%%~z") do (
- set str=%%b
- setlocal enabledelayedexpansion
- if !str:~^,1! neq ^) (set "str=(!str!") else set g=
- if !code:~-1! neq ^( (set "code=!code!)") else set g=
- set code=!code!!g!!str!
- for /f "delims=" %%c in ("!code!") do endlocal&set code=%%c
- )
- call :Test %%z
- )
- exit
- :Test
- set out=&setlocal enabledelayedexpansion
- set "code=!code!)) else set :="
- set b=!code!
- for /l %%a in (1 1 9) do set code=!code:%%%%a=$#%%a$!
- for /l %%a in (0 1 8000) do (
- set /a "n-=^!^!n"
- if !code:~%%a^,4!==$ (set "out=!out!^!"&set code=!code:~3!) else set out=!out!!code:~%%a,1!
- if !code:~%%a!.==. echo 合并后:!out!&echo !out!>1.cmd&endlocal&call 转义.bat 1.cmd "_%~n1"&pause&exit /b
- )
复制代码 转义.bat :- @echo off&setlocal enabledelayedexpansion&set hh=^
-
-
- for %%a in ("!hh!") do (
- endlocal
- set out=
- set tmp=
- for /f "usebackdelims=" %%b in (%1) do set hs=%%b
- setlocal enabledelayedexpansion
- for /f "eol=;delims=" %%b in ("!hs:"^=%%~a"!") do (
- endlocal&set str=%%b&set /a n=!n
- setlocal enabledelayedexpansion
- if !n!==0 set "str=!str:^=^^!"
- if !n%%b neq n%%b (
- for /l %%c in (8101 -100 1) do if "!str:~%%c!"=="" set len=%%c
- for /l %%c in (0 1 !len!) do if !str:~%%c^,1!==^^! (
- if !n!==0 (set "tmp=!tmp!^^^^^^^!") else set "tmp=!tmp!^^^!"
- ) else set "tmp=!tmp!!str:~%%c,1!"
- set str=!tmp!
- )
- if !n!==0 for %%c in (^( ^) ^& ^| ^< ^>) do set "str=!str:%%c=^%%c!"
- for /f "eol=:delims=" %%c in ("!out!!str!") do endlocal&set out=%%c
- )
- )
- setlocal enabledelayedexpansion
- echo 转义后:!out!&echo set "%~2=!out!">>2.cmd
- pause
复制代码 样本:- :strlen <stringVarName> [retvar]
- :: 思路: 二分回溯联合查表法
- :: 说明: 所求字符串大小范围 0K ~ 8K;
- :: stringVarName ---- 存放字符串的变量名
- :: retvar ---- 接收字符长度的变量名
- :: 原帖:http://www.dostips.com/forum/viewtopic.php?f=3&t=1429
-
- setlocal enabledelayedexpansion
- set $=%1#
- set N=&for %%z in (4096 2048 1024 512 256 128 64 32 16)do if !$:~%%z!. NEQ . set/aN+=%%z&set $=!$:~%%z!
- set $=!$!fedcba9876543210&set/aN+=0x!$:~16,1!
- for /f "delims=" %%a in ("%2=!n!")do endlocal&set %%a
复制代码 拖动样本到 合并.bat 上的处理结果:- set "_%%:=setlocal enabledelayedexpansion&set n=&set :=&for %%a in (1 1)do if defined : ((for %%b in (^!:^!)do set/a n+=1&set #^!n^!=%%b)&(setlocal enabledelayedexpansion)&(set $=^!#1^!#)&(set N=&for %%z in (4096 2048 1024 512 256 128 64 32 16)do if ^!$:~%%z^!. NEQ . set/aN+=%%z&set $=^!$:~%%z^!)&(set $=^!$^!fedcba9876543210&set/aN+=0x^!$:~16,1^!)&&(for /f "delims=" %%a in ("^^^!#2^^^!=^^^!n^^^!")do endlocal&set %%a))else set :="
复制代码
|