[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
什么时候出了这么一个好帖?竟然没注意到。

计算字符串长度,利用临时文件和xcopy,也有一个不错的办法
  1. @echo off
  2. set "str=Hello, bat! %%<^_^>%%""
  3. setlocal enabledelayedexpansion
  4. set str2=!str:y=-!
  5. set str2=!str2:n=-!
  6. set str2=!str2:a=-!
  7. echo;!str2!n>str.tmp
  8. goto:next 用for解析命令输出会降低效率,干脆用临时文件
  9. for /f %%a in ('
  10.     xcopy /-y %SystemRoot%\notepad.exe %SystemRoot%\explorer.exe ^<str.tmp ^| find /i /c "%SystemRoot%\explorer.exe"
  11. ') do set strlen=%%a-1
  12. :next
  13. xcopy /-y %SystemRoot%\notepad.exe %SystemRoot%\explorer.exe <str.tmp | find /i /c "%SystemRoot%\explorer.exe" > strlen.tmp
  14. set /p strlen=<strlen.tmp
  15. set /a strlen-=1
  16. echo;!str!&echo/&echo 上面的字符串长度为:!strlen!
  17. del str.tmp strlen.tmp
  18. pause
复制代码
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

回复 15楼 的帖子

其实不用临时文件的话,效率也不是很低,而且更简洁一些。
  1. @echo off
  2. set "str=例如:Hello, bat! %%<^_^>%%""
  3. setlocal enabledelayedexpansion
  4. set str2=!str:y=-!
  5. set str2=!str2:n=-!
  6. set str2=!str2:a=-!
  7. for /f %%a in ('
  8.     set str2^|xcopy /-y %SystemRoot%\notepad.exe %SystemRoot%\explorer.exe ^| find /i /c "%SystemRoot%\explorer.exe"
  9. ') do set /a strlen=%%a-9
  10. echo;!str!&echo/&echo 上面的字符串长度为:!strlen!
  11. pause
复制代码
1

评分人数

    • CrLf: 思路很有趣,但是优势似乎不大技术 + 1
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

回复 20楼 的帖子

计算字节数方法较多。我的是计算字符个数。
命令行参考:hh.exe ntcmds.chm::/ntcmds.htm
求助者请拿出诚心,别人才愿意奉献热心!
把查看手册形成条件反射!

TOP

返回列表