批处理中字符串分割实现循环输出批处理字符串分割实例
使⽤for命令可以对字符串进⾏分段处理。
分割字符串
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15@echo off
::定义⼀个以分号作为分隔的字符串
set str=AAA;BBB;CCC;DDD;EEE;FFF
::str的副本
nodejs字符串转数组
set remain=%str%
:loop
for /f "tokens=1* delims=;"%%a in ("%remain%") do (
:
:输出第⼀个分段(令牌)
echo%%a
rem 将截取剩下的部分赋给变量remain,其实这⾥可以使⽤延迟变量开关    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause
主要解释for语句:
delims=;表⽰以分号作为分隔符,对remain字符串进⾏分割处理。
tokens=1*,tokens表⽰分段的⽅式,tokens=1*表⽰第⼀个分隔符;之前的作为⼀部分,剩下的(*表⽰)作为⼀部分。这两部分在循环体总可以⽤%%a表⽰第⼀部分,%%b表⽰第⼆部分。批处理遍历path环境变量
我们知道path环境变量也是以分号作为分隔符的,批处理中,所以同样可以⽤上⾯的代码来遍历path环境变量。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16@echo off
setlocal enabledelayedexpansion
::定义⼀个以分号作为分隔的字符串
set str=%path%
::str的副本
set remain=%str%
:loop
for /f "tokens=1* delims=;"%%a in ("%remain%") do (
::输出第⼀个分段(令牌)
echo%%a
rem 将截取剩下的部分赋给变量remain,其实这⾥可以使⽤延迟变量开关    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause
运⾏结果:
D:\dev\workspace\MarkdownTools
......
C:\windows\system32
D:\dev\java\jdk1.8.0_91\bin
F:\Program Files\nodejs\node_global
F:\Program Files\Git\bin
D:\dev\apache-maven-3.5.4\bin
......
请按任意键继续. . .
批处理判断path环境变量中是否有某个⽬录
例如查系统path环境变量中是否存在D:\dev\workspace\MarkdownTools这个⽬录: 1
2 3 4 5 6 7 8@echo off
setlocal enabledelayedexpansion
::定义⼀个以分号作为分隔的字符串
::set str=AAA;BBB;CCC;DDD;EEE;FFF
set str=%path%
::str的副本
set remain=%str%
set toFind=D:\dev\workspace\MarkdownTools
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25set toFind=D:\dev\workspace\MarkdownTools
set isFind=false
:loop
for /f "tokens=1* delims=;"%%a in ("%remain%") do (
if "%toFind%"=="%%a"(
::设置标记,以便后续使⽤
set isFind=true
:
:到了就不了
goto :finded
)
rem 将截取剩下的部分赋给变量remain,其实这⾥可以使⽤延迟变量开关    set remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
:finded
echo%isFind%
pause
运⾏结果:
true
请按任意键继续. . .
参考资料
最近有有个⼩需求需要将shell 脚本的功能挪到windows中,但发现shell中有数组概念,但windows中却没有,同时shell中有很多⽅式处理字符串分割,但bat中就显得⽐较鸡肋,经过⼀番查,终于有了⽅案(Stack Overflow:stackoverflow/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):
⽅案:通过for循环处理,⽽处理的⽅式⼜可以分两种,⼀种是普通for,⼀种是for的⽂件处理⽅式:
⽅案⼀:
1 2 3 4 5@echo off & setlocal
rem 注意这⾥的s定义,其值不是使⽤双引号引起来的
rem also works for comma-separated lists, e.g. ABC,DEF,GHI,JKL set s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do echo%%a
⽅案⼆:is the best for (most) arbitrary delimiter characters.
1 2 3 4 5 6 7 8 9 10@echo off & setlocal
set s=AAA BBB CCC DDD EEE FFF
set t=%s%
:loop
for /f "tokens=1*"%%a in ("%t%") do (
echo%%a
rem 将截取剩下的部分赋给t,其实这⾥可以使⽤延迟变量开关 set t=%%b
)
if defined t goto :loop
有个⽼兄给了个更完整的(⽤到了延迟变量):1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Set a string with an arbitrary number of substrings separated by semi colons set teststring=The;rain;in;spain
REM Do something with each substring
:stringLOOP
REM Stop when the string is empty
if "!teststring!"EQU ""goto END
for /f "delims=;"%%a in ("!teststring!") do set substring=%%a
REM Do something with the substring -
REM we just echo it for the purposes of demo
echo!substring!
REM Now strip off the leading substring
18 19 20 21 22 23 24 25 26 27 28 29 30 31REM Now strip off the leading substring :striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1!
if "!teststring!"EQU ""goto stringloop  if "!stripchar!"NEQ ";"goto striploop  goto stringloop
)
:END
endlocal
还有这样的:
1 2 3 4set input=AAA BBB CCC DDD EEE FFF
set nth=4
for /F "tokens=%nth% delims= "%%a in ("%input%") do set nthstring=%%a echo%nthstring%
其实Powershell⾥可能有更多的内置函数可以使⽤:PS C:\> "AAA BBB CCC DDD EEE FFF".Split()还有⼈提出⽤vbscrip代替bat:
1 2 3 4 5 6 7 8 9 10Set objFS = CreateObject("Scripting.FileSystemObject") Set objArgs = WScript.Arguments
str1 = objArgs(0)
s=Split(str1," ")
For i=LBound(s) To UBound(s)
WScript.Echo s(i)
WScript.Echo s(9) ' get the 10th element
Next
usage:
c:\test> cscript /nologo test.vbs "AAA BBB CCC"
最后来⼀个bat中的⼩难点:变量延迟(⾃上⽽下,逐条(简单语句、复合语句(for、if 语句块只算⼀条))执⾏,⽽⾮逐⾏执⾏)以上就是批处理中字符串分割实现代码的详细内容,更多关于批处理字符串分割的资料请关注脚本之家其它相关⽂章!
出处:www.jb51/article/193244.htm

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。