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

[转贴] 批处理命令set变量字符截取点点通

原文由 小楼春雨 发表
注意:本教程是一批处理程序。请将下面代码保存为一批处理文件,然后运行。

教程: 变量字符的截取
  1. @echo off
  2. rem code by 小楼一夜听春雨
  3. color 1f&rem num=15  nums=30
  4. mode con: cols=80 lines=35
  5. set "var=1234567890"
  6. set "num=               "
  7. set "nums=                              "
  8. rem 第一屏 舍弃
  9. cls&echo 第一屏 舍弃
  10. echo\&echo %num% var=%var%&echo\
  11. for /l %%i in (1 1 3) do call :dan %%i
  12. echo\
  13. for /l %%i in (0 1 1) do call :yi %%i
  14. echo\
  15. for /l %%i in (0 1 2) do call :_yi %%i
  16. echo  ~ 波浪号后面的数字:为正数表示舍弃变量的前位;
  17. echo  , 逗号后面的数字为正数表示取变量的舍弃以后的前几位;
  18. echo  , 逗号后面为负数表示舍弃变量的后几位
  19. echo\&echo\&pause&color 1e
  20. rem 第二屏 取
  21. cls&echo 第二屏 取
  22. echo\&echo %num% var=%var%&echo\
  23. for /l %%i in (-1 -1 -3) do call :dan %%i
  24. echo\
  25. for /l %%i in (-4 -1 -6) do call :er %%i
  26. echo\
  27. call :_er -8
  28. echo  ~ 波浪号后面直接跟负数表示取变量的后几位
  29. echo  , 逗号后面的数字为正数表示取变量的舍弃以后的前几位;
  30. echo  , 逗号后面为负数表示舍弃变量的后几位  
  31. echo\&echo\&pause&color 1f
  32. rem 第三屏 替换 删除
  33. cls&echo 第三屏 替换 删除
  34. set "var=abc:\123\ef:g\123\456"hij"789"klm\"
  35. echo\&echo %num%var=%var%
  36. echo\
  37. call :san
  38. echo  = 号左边为要替换掉的字符, 右边为需替换来的字符
  39. echo  = 号左边要替换掉的字符前面加个*号,代表第一次出现的字符及其前面的所有字符
  40. echo\
  41. echo  ========= 演示完毕 ========= 按任意键退出 ==========
  42. pause>nul
  43. exit
  44. :dan
  45. set shu=%1
  46. if %shu% lss 0 (set aa=取&set ji=最后) else set aa=舍弃&set ji=第
  47. if "%shu:~0,1%"=="-" set shu=%shu:~1%
  48. set str1=%%var:~%1%%%num%
  49. set str2=%aa%var的%ji%%shu%位%nums%
  50. call set str3=%%var:~%1%%%num%
  51. call echo %%str1:~0,15%% %%str2:~0,30%% %%str3:~0,15%%
  52. goto :eof
  53. :yi
  54. set shu=%1
  55. if %shu% lss 0 (set aa=取&set ji=最后) else set aa=舍弃&set ji=第
  56. for /l %%i in (1 1 3) do (
  57. set str1=%%var:~%1,%%i%%%num%
  58. set str2=%aa%var的%ji%%shu%位之后取%%i位%nums%
  59. call set str3=%%var:~%1,%%i%%%num%
  60. call echo %%str1:~0,15%% %%str2:~0,26%% %%str3:~0,15%%
  61. if %%i==3 echo\
  62. )
  63. goto :eof
  64. :_yi
  65. for /l %%i in (-1 -1 -3) do (
  66. set str1=%%var:~%1,%%i%%%num%
  67. set str2=舍弃var的前%1位和%%i位%nums%
  68. call set str3=%%var:~%1,%%i%%%num%
  69. call echo %%str1:~0,15%% %%str2:~0,28%% %%str3:~0,15%%
  70. if %%i==-3 echo\
  71. )
  72. goto :eof
  73. :er
  74. set sss=%1
  75. set sss=%sss:~1%
  76. for /l %%i in (1 1 3) do (
  77. set str1=%%var:~%1,%%i%%%num%
  78. set str2=从var的倒数%sss%位开始取%%i位%nums%
  79. call set str3=%%var:~%1,%%i%%%num%
  80. call echo %%str1:~0,15%% %%str2:~0,24%%   %%str3:~0,15%%
  81. if %%i==3 echo\
  82. )
  83. goto :eof
  84. :_er
  85. set sss=%1
  86. set sss=%sss:~1%
  87. for /l %%i in (-1 -1 -3) do (
  88. set str1=%%var:~%1,%%i%%%num%
  89. set str2=从var的倒数%sss%位开始并舍弃最后%%i位%nums%
  90. call set str3=%%var:~%1,%%i%%%num%
  91. call echo %%str1:~0,15%% %%str2:~0,20%%   %%str3:~0,15%%
  92. if %%i==-3 echo\
  93. )
  94. goto :eof
  95. :san
  96. set str1=%%var:"=%%%num%
  97. set str2=删除var中的所有^"号%num%
  98. set str3=%var:"=%%nums%
  99. call echo %%str1:~0,13%% %%str2:~0,23%% %%str3:~0,30%%
  100. set str1=%%var:\=%%%num%
  101. set str2=删除var中的所有\号%num%
  102. set str3=%var:\=%%nums%
  103. call echo %%str1:~0,13%% %%str2:~0,23%% %%str3:~0,30%%
  104. set str1=%%var::=%%%num%
  105. set str2=删除var中的所有:号%num%
  106. set str3=%var::=%%nums%
  107. call echo %%str1:~0,13%% %%str2:~0,23%% %%str3:~0,30%%
  108. echo\
  109. set str1=%%var:"=好%%%num%
  110. set str2=替换var中的所有^"号为好字%num%
  111. set str3=%var:"=好%%nums%
  112. call echo %%str1:~0,12%% %%str2:~0,20%% %%str3:~0,30%%
  113. set str1=%%var:\=好%%%num%
  114. set str2=替换var中的所有\号为好字%num%
  115. set str3=%var:\=好%%nums%
  116. call echo %%str1:~0,12%% %%str2:~0,20%% %%str3:~0,30%%
  117. set str1=%%var::=好%%%num%
  118. set str2=替换var中的所有:号为好字%num%
  119. set str3=%var::=好%%nums%
  120. call echo %%str1:~0,12%% %%str2:~0,20%% %%str3:~0,30%%
  121. echo\
  122. set str1=%%var:*"=%%%num%
  123. set str3=%var:*"=%%nums%
  124. echo 删除var中的第一次出现的^"号及其前面的所有字符
  125. call echo %%str1:~0,15%%  %%str3:~0,30%%
  126. echo.
  127. set str1=%%var:*\=%%%num%
  128. set str3=%var:*\=%%nums%
  129. echo 删除var中的第一次出现的\号及其前面的所有字符
  130. call echo %%str1:~0,15%%  %%str3:~0,30%%
  131. echo.
  132. set str1=%%var:*:=%%%num%
  133. set str3=%var:*:=%%nums%
  134. echo 删除var中的第一次出现的:号及其前面的所有字符
  135. call echo %%str1:~0,15%%  %%str3:~0,30%%
  136. echo\
  137. set str1=%%var:*"=ppp%%%num%
  138. set str3=%var:*"=ppp%%nums%
  139. echo 替换var中的第一次出现的^"号及其前面的所有字符为ppp
  140. call echo %%str1:~0,15%%  %%str3:~0,30%%
  141. echo.
  142. set str1=%%var:*\=ppp%%%num%
  143. set str3=%var:*\=ppp%%nums%
  144. echo 替换var中的第一次出现的\号及其前面的所有字符为ppp
  145. call echo %%str1:~0,15%%  %%str3:~0,30%%
  146. echo.
  147. set str1=%%var:*:=ppp%%%num%
  148. set str3=%var:*:=ppp%%nums%
  149. echo 替换var中的第一次出现的:号及其前面的所有字符为ppp
  150. call echo %%str1:~0,15%%  %%str3:~0,30%%
  151. echo\
  152. goto :eof
复制代码
2

评分人数

返回列表