找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
楼主: netbenton

[原创] 探讨批处理代码效率

[复制链接]
发表于 2022-8-23 12:02:05 | 显示全部楼层
看这个十年前的老帖 http://www.bathome.net/thread-11799-1-1.html 可见当年批处理计算字符串长度的"技法峰值"
在此分享源网站 https://www.dostips.com 的这段经典代码(见以下代码段),其内还有两枚技术硬核》
一。句式 set "str=a!%~1!" 提高形参 %1 的容错率,一网打尽键盘所有可见字符
二。句式 (endlocal ... set /a %~2=%len%) 令局部变量亡前可续命给全局变量

附加几行代码》针对经典代码的测试/用法
  1. @echo off
  2. :[Loop] //测试代码 备注》调用子过程的形参须是变量名
  3. set "str=" &set/p str="输入一个字符串获取其长度:"
  4. if not defined str exit/b
  5. (call :strLen str sL)
  6. echo,长度=%sL%
  7. goto[Loop]

  8. :: 分享计算字符串长度的经典代码如下》
  9. ::      string [in]  - variable name containing the string being measured for length
  10. ::      len [out] - variable to be used to return the string length
  11. :: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
  12. :: Created 20081122,changed 20101116,source https://www.dostips.com
  13. :strLen string len -- returns the length of a string
  14. (   setlocal enabledelayedexpansion
  15.     set "str=a!%~1!" &rem keep the a up front to ensure we get the length and not the upper bound,it also avoids trouble in case of empty string
  16.     set "len=0"
  17.     for /l %%a in (12,-1,0) do (
  18.         set /a "len|=1<<%%a"
  19.         for %%b in (!len!) do if "!str:~%%b,1!"=="" set /a "len&=~1<<%%a"
  20.     )
  21. )
  22. ( endlocal & rem return values
  23.     if "%~2" neq "" set /a %~2=%len%
  24. )
  25. exit /b
复制代码
发表于 2022-8-23 14:25:46 | 显示全部楼层
回复 31# aloha20200628
谢谢提供链接,内容确实精彩!!!
发表于 2022-9-6 19:53:25 | 显示全部楼层
回复 31# aloha20200628
谢谢,正想找这种代码呢!
发表于 2023-4-3 13:29:51 | 显示全部楼层
回复 31# aloha20200628
大佬,最近我又看到了Batcher大佬的多行回退,但是并未在win10中实现,不知为什么,
发表于 2024-4-29 16:43:00 | 显示全部楼层
多年后的膜拜……
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 02:28 , Processed in 0.010763 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表