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

[文本处理] 批处理如何去掉字符串两边的空格

[复制链接]
发表于 2008-1-19 23:00:12 | 显示全部楼层
真他齐全 特别是 %%~nxi 的用法
发表于 2023-8-4 17:00:33 | 显示全部楼层
本帖最后由 tiandyoin 于 2023-8-4 21:38 编辑
  1. @goto :main_20230424_102850

  2. @Usage:
  3.                 cd /d "%~dp0" & call "字符串 Trim.bat" :Trim[Ext] arg1_name L[eft] R[ight]

  4. :main_20230424_102850
  5.         @echo off||code by tiandyoin&title "字符串 Trim.bat"  
  6.         :: 约定 %~1 为 :Trim... 其中的一个标签(Label)。  
  7.         if "%~1"=="" (
  8.                 call :test_20230424_102850  
  9.                 cd /d "%~dp0" & call "判断 双击运行 or 命令提示符窗口.bat" :where-am-i
  10.         ) else (
  11.                 :: 即 call :Trim[Ext] arg1_name L[eft] R[ight]
  12.                 call %*
  13.         )
  14. @goto :eof

  15. :test_20230424_102850
  16.         @echo off&setlocal enabledelayedexpansion
  17.         set "str=   ""test.txt  "".  .. ...   "
  18.         echo [!str!]
  19.         call :Trim str
  20.         echo [!str!]
  21.        
  22.         set "str=   ""test.txt  "".  .. ...   "
  23.         echo [!str!]
  24.         call :Trim str "" ""
  25.         echo [!str!]
  26.        
  27.         set "str=   ""test.txt  "".  .. ...   "
  28.         echo [!str!]
  29.         call :Trim str L, R
  30.         echo [!str!]

  31.         set "str=   ""test.txt  "".  .. ...   "
  32.         echo [!str!]
  33.         call :TrimExt str Right
  34.         echo [!str!]
  35. @goto :eof

  36. @rem Usage:
  37. rem                需要调用者提供“延迟变量扩展环境”。  
  38. :Trim  <arg1_name [,L[eft] ,R[ight]]>
  39.         @echo off&setlocal enabledelayedexpansion
  40.                 set fst=0
  41.                 set lst=0
  42.                 set "input=!%~1!"
  43.                 if /i not "%~2"=="L" if /i not "%~2"=="Left" if /i not "%~2"=="" goto :Trim.lTrim.EOF
  44.                 :Trim.lTrim
  45.                         set ch=!input:~0,1!
  46.                         @rem 如果 ch 是双引号,需要凑成对组成引用闭环。
  47.                         if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="                " goto :Trim.lTrim.EOF
  48.                         set "input=!input:~1!"
  49.                         set /a fst+=1
  50.                 goto :Trim.lTrim
  51.                 :Trim.lTrim.EOF
  52.                 if /i not "%~2"=="R" if /i not "%~2"=="Right" if /i not "%~2"=="" if /i not "%~3"=="R" if /i not "%~3"=="Right" if /i not "%~3"=="" goto :Trim.EOF
  53.                 :Trim.rTrim
  54.                         set ch=!input:~-1!
  55.                         if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="                " goto :Trim.EOF
  56.                         set "input=!input:~0,-1!"
  57.                         set /a lst-=1
  58.                 goto :Trim.rTrim
  59.                 :Trim.EOF
  60.                 if !lst!==0 (set "lst=") else (set "lst=,!lst!")
  61.         endlocal & set "%~1=!%~1:~%fst%%lst%!"
  62. @goto :EOF

  63. @rem Usage:
  64. rem                需要调用者提供“延迟变量扩展环境”。  
  65. rem                增加功能: 去除右边 '.'
  66. :TrimExt  <arg1_name [,L[eft] ,R[ight]]>
  67.         @echo off&setlocal enabledelayedexpansion
  68.                 set fst=0
  69.                 set lst=0
  70.                 set "input=!%~1!"
  71.                 if /i not "%~2"=="L" if /i not "%~2"=="Left" if /i not "%~2"=="" goto :TrimExt.lTrimExt.EOF
  72.                 :TrimExt.lTrimExt
  73.                         set ch=!input:~0,1!
  74.                         @rem 如果 ch 是双引号,需要凑成对组成引用闭环。
  75.                         if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="                " goto :TrimExt.lTrimExt.EOF
  76.                         set "input=!input:~1!"
  77.                         set /a fst+=1
  78.                 goto :TrimExt.lTrimExt
  79.                 :TrimExt.lTrimExt.EOF
  80.                 if /i not "%~2"=="R" if /i not "%~2"=="Right" if /i not "%~2"=="" if /i not "%~3"=="R" if /i not "%~3"=="Right" if /i not "%~3"=="" goto :TrimExt.EOF
  81.                 :TrimExt.rTrimExt
  82.                         set ch=!input:~-1!
  83.                         if not "!ch!!ch!"=="  " if not "!ch!!ch!"=="                " if not "!ch!!ch!"==".." goto :TrimExt.EOF
  84.                         set "input=!input:~0,-1!"
  85.                         set /a lst-=1
  86.                 goto :TrimExt.rTrimExt
  87.                 :TrimExt.EOF
  88.                 if !lst!==0 (set "lst=") else (set "lst=,!lst!")
  89.         endlocal & set "%~1=!%~1:~%fst%%lst%!"
  90. @goto :EOF

  91. @rem Usage:
  92. rem                需要调用者提供“延迟变量扩展环境”。  
  93. rem                功能: 去除指定字符左右两边的其它字符。
  94. rem                方法:
  95. rem                        使用 Set 替换英文双引号为中文双引号,替换指定字符等为双引号,
  96. rem                        再使用 Set 去除引号两边字符,最后还原回英文双引号。
  97. rem         %var:*"=set "var=%
  98. :TrimBesideAssigner  <arg1_name [,%Assigner% ,L[eft] ,R[ight]]>
  99.         REM 未完成
  100. @goto :EOF
复制代码

  1. 【测试用例】

  2. "转义字符.txt"
  3. 以下测试空行:


  4.        
  5.          
  6.        
  7.    
  8. 下面是 中国DOS联盟 @bjsh 的例子:

  9. "aou"eo
  10. 下面一行需要 findstr /n
  11. ;euou%^>
  12. ::::aeui
  13.    
  14. :::E2uo alejou 3<o2io|
  15. ^aue||%ou

  16. aoue eou 2
  17. euo 8
  18. ege#6758!7^^9098!98%$&^
  19. ueyi^^^^aueuo2
  20. ~ ! @ # $ % ^ & * ( () " ok " No " <>nul
  21. ege#6758^^^!7^^^^9098^!98%$&^^

  22. ~ ! @ # $ %"  ^  "& * ( () " ok " No " <>nul
  23. sdsdg:sadgs

  24. kl&>jl^k!tsd!21%mk%gd
  25. kl&>jlk^!tsd^!21%mk%gd
  26. dgssdgdg^gds^gdsa

  27. 下面是 CSDN @tiandyoin 的例子:

  28. @REM cmd [/c, /k] 命令中需要双引号的特殊字符是:
  29. @REM      <white space>
  30. @rem      & < > [ ] { } ^ = ; ! ' + , ` ~
  31. @REM 详见:
  32. @REM "批处理之家\ntcmds.chs.chm"

  33. @REM      &()[]{}^=;!'+,`~


  34. %~
  35. %~:
  36. %~:=%

  37. :123
  38. :
  39. %a%
  40. !b!
  41. "%a%"
  42. "!b!"
  43. %%a%%
  44. ^!b^!
  45. %a!%b!
  46. !b%a!%
  47. %%a%
  48. ^!b!
  49. a"%b"%c"
  50. a"!b"!c"
  51. "a b " c"
  52. "a b "" c"
  53. ""a b "" c"
  54. ""a b "" c""
  55. """a b "" c"""
  56. (()((
  57. [\s\t         ]

  58. :test"^!~:^=!" % ~:=% ^0&((()||><>> `@#$*-_+{{}[]]\;',.?/ %~0 !~0 %空变量% !空变量! %空变量:空值1=空值2% !空变量:空值1=空值2! "
  59. :test"^!~:^=!" % ~:=% ^0&((()||><>>^^ ^^^ ^^^^  `@#$*-_+{{}[]]\;',.?/ %~0 !~0 %空变量% !"空变量! %"空变量:空值1=空值2% !空变量:空值1=空值2! "

  60.   1:c:\3/%5:\%1 %* 2<1.bug "!a!>" & !b!<"!c!"|!d! "?4*6?%e%"%f%" && (( %!g!% || !%h%! )  

  61.                   1:c:\3/%5:\%1 %* 2<1.bug "!a!>" & !b^!<"=^^!c!"|!d^! "?4*6?%e%"+%f%" && (( %!g!% || !%h%! )         
  62.                   1:c:\3/%5:\%1 %* 2<1.bug !i! "!a!>" & !b^!<"=^^!c!"|!d^! "?4*6?%e%"+%f%" && (( %!g!% || !%h%! )          

  63.    " 1:c:\3/%5:\%1 %* 2<1.bug !i! "!a!>" & !b^!<"=^^!c!"|!d^! "?4*6?%e%"+%f%" && (( %!g!% || !%h%! ) ;euou%^>1.txt  "   
  64.    
  65. "update-content": " <p>1.优化程序,提高稳定性,提升用户体验。</p><p>2.订阅即将到期增加提醒。</p><p>3.“我的套餐”中增加显示激活码和邮箱的信息。</p><p>4.优化激活失败提示信息。</p><p>5.程序多开问题优化处理。</p><p>6.windows平台网络诊断数据异常问题。</p><p>7.其它问题优化。</p>",
复制代码
没找到完美的方法,这是我自己写的,尽可能解决问题:
1. 单数个双引号
2. :: 开头的注释
3. ; 开头的隔行作用
4. !! 被误当成取值
5. %% 被误当成取值
6. %~ 闪退
7. ^^^^ 多个的折叠问题

使用 set 去除空格前需要转义特殊字符,因此我没采用这些方法,而使用循环逐一处理每一个字符
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 19:34 , Processed in 0.016851 second(s), 7 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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